/hazelcast/src/main/java/com/hazelcast/impl/ascii/memcache/MemcacheEntry.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 89 lines · 62 code · 12 blank · 15 comment · 0 complexity · 7dcca93dbf41a029879c374e01b5e2c8 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.hazelcast.impl.ascii.memcache;
  17. import com.hazelcast.impl.ascii.TextCommandConstants;
  18. import com.hazelcast.nio.DataSerializable;
  19. import java.io.DataInput;
  20. import java.io.DataOutput;
  21. import java.io.IOException;
  22. import java.nio.ByteBuffer;
  23. public class MemcacheEntry implements DataSerializable, TextCommandConstants {
  24. byte[] bytes;
  25. int flag;
  26. public MemcacheEntry(String key, byte[] value, int flag) {
  27. byte[] flagBytes = new String(" " + flag + " ").getBytes();
  28. byte[] valueLen = String.valueOf(value.length).getBytes();
  29. byte[] keyBytes = key.getBytes();
  30. int size = VALUE_SPACE.length
  31. + keyBytes.length
  32. + flagBytes.length
  33. + valueLen.length
  34. + RETURN.length
  35. + value.length
  36. + RETURN.length;
  37. ByteBuffer entryBuffer = ByteBuffer.allocate(size);
  38. entryBuffer.put(VALUE_SPACE);
  39. entryBuffer.put(keyBytes);
  40. entryBuffer.put(flagBytes);
  41. entryBuffer.put(valueLen);
  42. entryBuffer.put(RETURN);
  43. entryBuffer.put(value);
  44. entryBuffer.put(RETURN);
  45. this.bytes = entryBuffer.array();
  46. this.flag = flag;
  47. }
  48. public MemcacheEntry() {
  49. }
  50. public void readData(DataInput in) throws IOException {
  51. int size = in.readInt();
  52. bytes = new byte[size];
  53. in.readFully(bytes);
  54. flag = in.readInt();
  55. }
  56. public void writeData(DataOutput out) throws IOException {
  57. out.writeInt(bytes.length);
  58. out.write(bytes);
  59. out.writeInt(flag);
  60. }
  61. public ByteBuffer toNewBuffer() {
  62. return ByteBuffer.wrap(bytes);
  63. }
  64. public int getFlag() {
  65. return flag;
  66. }
  67. public byte[] getBytes() {
  68. return bytes;
  69. }
  70. @Override
  71. public String toString() {
  72. return "MemcacheEntry{" +
  73. "bytes=" + new String(bytes) +
  74. ", flag=" + flag +
  75. '}';
  76. }
  77. }