Skip to content
This repository
Browse code

add byte buffer backed bytes reference

  • Loading branch information...
commit 5f5458fd56bae07ba44554a61ba3e117dfd4a2ef 1 parent 3523356
Shay Banon authored
123  src/main/java/org/elasticsearch/common/bytes/ByteBufferBytesReference.java
... ...
@@ -0,0 +1,123 @@
  1
+/*
  2
+ * Licensed to ElasticSearch and Shay Banon under one
  3
+ * or more contributor license agreements.  See the NOTICE file
  4
+ * distributed with this work for additional information
  5
+ * regarding copyright ownership. ElasticSearch licenses this
  6
+ * file to you under the Apache License, Version 2.0 (the
  7
+ * "License"); you may not use this file except in compliance
  8
+ * with the License.  You may obtain a copy of the License at
  9
+ *
  10
+ *    http://www.apache.org/licenses/LICENSE-2.0
  11
+ *
  12
+ * Unless required by applicable law or agreed to in writing,
  13
+ * software distributed under the License is distributed on an
  14
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15
+ * KIND, either express or implied.  See the License for the
  16
+ * specific language governing permissions and limitations
  17
+ * under the License.
  18
+ */
  19
+
  20
+package org.elasticsearch.common.bytes;
  21
+
  22
+import org.elasticsearch.common.Bytes;
  23
+import org.elasticsearch.common.io.stream.ByteBufferStreamInput;
  24
+import org.elasticsearch.common.io.stream.StreamInput;
  25
+import org.elasticsearch.common.io.stream.StreamOutput;
  26
+
  27
+import java.io.IOException;
  28
+import java.io.OutputStream;
  29
+import java.nio.ByteBuffer;
  30
+
  31
+/**
  32
+ */
  33
+public class ByteBufferBytesReference implements BytesReference {
  34
+
  35
+    private final ByteBuffer buffer;
  36
+
  37
+    public ByteBufferBytesReference(ByteBuffer buffer) {
  38
+        this.buffer = buffer;
  39
+    }
  40
+
  41
+    @Override
  42
+    public byte get(int index) {
  43
+        return buffer.get(buffer.position() + index);
  44
+    }
  45
+
  46
+    @Override
  47
+    public int length() {
  48
+        return buffer.remaining();
  49
+    }
  50
+
  51
+    @Override
  52
+    public BytesReference slice(int from, int length) {
  53
+        ByteBuffer dup = buffer.duplicate();
  54
+        dup.position(buffer.position() + from);
  55
+        dup.limit(buffer.position() + from + length);
  56
+        return new ByteBufferBytesReference(dup);
  57
+    }
  58
+
  59
+    @Override
  60
+    public StreamInput streamInput() {
  61
+        return new ByteBufferStreamInput(buffer);
  62
+    }
  63
+
  64
+    @Override
  65
+    public void writeTo(StreamOutput out, boolean withLength) throws IOException {
  66
+        if (withLength) {
  67
+            out.writeVInt(length());
  68
+        }
  69
+        writeTo(out);
  70
+    }
  71
+
  72
+    @Override
  73
+    public void writeTo(OutputStream os) throws IOException {
  74
+        if (buffer.hasArray()) {
  75
+            os.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
  76
+        } else {
  77
+            byte[] tmp = new byte[8192];
  78
+            ByteBuffer buf = buffer.duplicate();
  79
+            while (buf.hasRemaining()) {
  80
+                buf.get(tmp, 0, Math.min(tmp.length, buf.remaining()));
  81
+                os.write(tmp);
  82
+            }
  83
+        }
  84
+    }
  85
+
  86
+    @Override
  87
+    public byte[] toBytes() {
  88
+        if (!buffer.hasRemaining()) {
  89
+            return Bytes.EMPTY_ARRAY;
  90
+        }
  91
+        byte[] tmp = new byte[buffer.remaining()];
  92
+        buffer.duplicate().get(tmp);
  93
+        return tmp;
  94
+    }
  95
+
  96
+    @Override
  97
+    public BytesArray toBytesArray() {
  98
+        if (buffer.hasArray()) {
  99
+            return new BytesArray(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
  100
+        }
  101
+        return new BytesArray(toBytes());
  102
+    }
  103
+
  104
+    @Override
  105
+    public BytesArray copyBytesArray() {
  106
+        return new BytesArray(toBytes());
  107
+    }
  108
+
  109
+    @Override
  110
+    public boolean hasArray() {
  111
+        return buffer.hasArray();
  112
+    }
  113
+
  114
+    @Override
  115
+    public byte[] array() {
  116
+        return buffer.array();
  117
+    }
  118
+
  119
+    @Override
  120
+    public int arrayOffset() {
  121
+        return buffer.arrayOffset() + buffer.position();
  122
+    }
  123
+}
86  src/main/java/org/elasticsearch/common/io/stream/ByteBufferStreamInput.java
... ...
@@ -0,0 +1,86 @@
  1
+package org.elasticsearch.common.io.stream;
  2
+
  3
+import java.io.EOFException;
  4
+import java.io.IOException;
  5
+import java.nio.ByteBuffer;
  6
+
  7
+/**
  8
+ */
  9
+public class ByteBufferStreamInput extends StreamInput {
  10
+
  11
+    private final ByteBuffer buffer;
  12
+
  13
+    public ByteBufferStreamInput(ByteBuffer buffer) {
  14
+        this.buffer = buffer;
  15
+    }
  16
+
  17
+    @Override
  18
+    public int read() throws IOException {
  19
+        if (!buffer.hasRemaining()) {
  20
+            return -1;
  21
+        }
  22
+        return buffer.get() & 0xFF;
  23
+    }
  24
+
  25
+    @Override
  26
+    public byte readByte() throws IOException {
  27
+        if (!buffer.hasRemaining()) {
  28
+            throw new EOFException();
  29
+        }
  30
+        return buffer.get();
  31
+    }
  32
+
  33
+    @Override
  34
+    public int read(byte[] b, int off, int len) throws IOException {
  35
+        if (!buffer.hasRemaining()) {
  36
+            return -1;
  37
+        }
  38
+
  39
+        len = Math.min(len, buffer.remaining());
  40
+        buffer.get(b, off, len);
  41
+        return len;
  42
+    }
  43
+
  44
+    @Override
  45
+    public long skip(long n) throws IOException {
  46
+        if (n > buffer.remaining()) {
  47
+            int ret = buffer.position();
  48
+            buffer.position(buffer.limit());
  49
+            return ret;
  50
+        }
  51
+        buffer.position((int) (buffer.position() + n));
  52
+        return n;
  53
+    }
  54
+
  55
+    @Override
  56
+    public void readBytes(byte[] b, int offset, int len) throws IOException {
  57
+        if (buffer.remaining() > len) {
  58
+            throw new EOFException();
  59
+        }
  60
+        buffer.get(b, offset, len);
  61
+    }
  62
+
  63
+    @Override
  64
+    public void reset() throws IOException {
  65
+        buffer.reset();
  66
+    }
  67
+
  68
+    @Override
  69
+    public int available() throws IOException {
  70
+        return buffer.remaining();
  71
+    }
  72
+
  73
+    @Override
  74
+    public void mark(int readlimit) {
  75
+        buffer.mark();
  76
+    }
  77
+
  78
+    @Override
  79
+    public boolean markSupported() {
  80
+        return true;
  81
+    }
  82
+
  83
+    @Override
  84
+    public void close() throws IOException {
  85
+    }
  86
+}

0 notes on commit 5f5458f

Please sign in to comment.
Something went wrong with that request. Please try again.