diff --git a/src/main/java/org/elasticsearch/common/io/stream/BytesStreamInput.java b/src/main/java/org/elasticsearch/common/io/stream/BytesStreamInput.java index 7e1b3fed37d..65071087356 100644 --- a/src/main/java/org/elasticsearch/common/io/stream/BytesStreamInput.java +++ b/src/main/java/org/elasticsearch/common/io/stream/BytesStreamInput.java @@ -19,6 +19,7 @@ package org.elasticsearch.common.io.stream; +import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; @@ -69,6 +70,16 @@ public class BytesStreamInput extends StreamInput { return bytes; } + @Override + public BytesRef readBytesRef(int length) throws IOException { + if (unsafe) { + return super.readBytesRef(length); + } + BytesRef bytes = new BytesRef(buf, pos, length); + pos += length; + return bytes; + } + @Override public long skip(long n) throws IOException { if (pos + n > count) { diff --git a/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java b/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java index dfbdb528eee..b993c9a55e0 100644 --- a/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java +++ b/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java @@ -87,10 +87,16 @@ public abstract class StreamInput extends InputStream { public BytesRef readBytesRef() throws IOException { int length = readVInt(); - int offset = readVInt(); + return readBytesRef(length); + } + + public BytesRef readBytesRef(int length) throws IOException { + if (length == 0) { + return new BytesRef(); + } byte[] bytes = new byte[length]; - readBytes(bytes, offset, length); - return new BytesRef(bytes, offset, length); + readBytes(bytes, 0, length); + return new BytesRef(bytes, 0, length); } public void readFully(byte[] b) throws IOException { diff --git a/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java b/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java index e414c0d1cba..5af27273970 100644 --- a/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java +++ b/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java @@ -113,7 +113,6 @@ public abstract class StreamOutput extends OutputStream { return; } writeVInt(bytes.length); - writeVInt(bytes.offset); write(bytes.bytes, bytes.offset, bytes.length); } diff --git a/src/main/java/org/elasticsearch/transport/netty/ChannelBufferStreamInput.java b/src/main/java/org/elasticsearch/transport/netty/ChannelBufferStreamInput.java index 37b834b255a..1a8064af949 100644 --- a/src/main/java/org/elasticsearch/transport/netty/ChannelBufferStreamInput.java +++ b/src/main/java/org/elasticsearch/transport/netty/ChannelBufferStreamInput.java @@ -19,6 +19,7 @@ package org.elasticsearch.transport.netty; +import org.apache.lucene.util.BytesRef; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.ChannelBufferBytesReference; import org.elasticsearch.common.io.stream.StreamInput; @@ -57,6 +58,16 @@ public class ChannelBufferStreamInput extends StreamInput { return ref; } + @Override + public BytesRef readBytesRef(int length) throws IOException { + if (!buffer.hasArray()) { + return super.readBytesRef(length); + } + BytesRef bytesRef = new BytesRef(buffer.array(), buffer.arrayOffset() + buffer.readerIndex(), length); + buffer.skipBytes(length); + return bytesRef; + } + @Override public int available() throws IOException { return endIndex - buffer.readerIndex();