PageRenderTime 31ms CodeModel.GetById 10ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/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
17package com.hazelcast.impl.ascii.memcache;
18
19import com.hazelcast.impl.ascii.TextCommandConstants;
20import com.hazelcast.nio.DataSerializable;
21
22import java.io.DataInput;
23import java.io.DataOutput;
24import java.io.IOException;
25import java.nio.ByteBuffer;
26
27public class MemcacheEntry implements DataSerializable, TextCommandConstants {
28    byte[] bytes;
29    int flag;
30
31    public MemcacheEntry(String key, byte[] value, int flag) {
32        byte[] flagBytes = new String(" " + flag + " ").getBytes();
33        byte[] valueLen = String.valueOf(value.length).getBytes();
34        byte[] keyBytes = key.getBytes();
35        int size = VALUE_SPACE.length
36                + keyBytes.length
37                + flagBytes.length
38                + valueLen.length
39                + RETURN.length
40                + value.length
41                + RETURN.length;
42        ByteBuffer entryBuffer = ByteBuffer.allocate(size);
43        entryBuffer.put(VALUE_SPACE);
44        entryBuffer.put(keyBytes);
45        entryBuffer.put(flagBytes);
46        entryBuffer.put(valueLen);
47        entryBuffer.put(RETURN);
48        entryBuffer.put(value);
49        entryBuffer.put(RETURN);
50        this.bytes = entryBuffer.array();
51        this.flag = flag;
52    }
53
54    public MemcacheEntry() {
55    }
56
57    public void readData(DataInput in) throws IOException {
58        int size = in.readInt();
59        bytes = new byte[size];
60        in.readFully(bytes);
61        flag = in.readInt();
62    }
63
64    public void writeData(DataOutput out) throws IOException {
65        out.writeInt(bytes.length);
66        out.write(bytes);
67        out.writeInt(flag);
68    }
69
70    public ByteBuffer toNewBuffer() {
71        return ByteBuffer.wrap(bytes);
72    }
73
74    public int getFlag() {
75        return flag;
76    }
77
78    public byte[] getBytes() {
79        return bytes;
80    }
81
82    @Override
83    public String toString() {
84        return "MemcacheEntry{" +
85                "bytes=" + new String(bytes) +
86                ", flag=" + flag +
87                '}';
88    }
89}