Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BytesStreamInput(BytesReference) ctor with nonzero offset #7197

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class BytesStreamInput extends StreamInput {

protected int pos;

protected int count;
protected int end;

private final boolean unsafe;

Expand All @@ -45,7 +45,7 @@ public BytesStreamInput(BytesReference bytes) {
}
this.buf = bytes.array();
this.pos = bytes.arrayOffset();
this.count = bytes.length();
this.end = pos + bytes.length();
this.unsafe = false;
}

Expand All @@ -56,7 +56,7 @@ public BytesStreamInput(byte buf[], boolean unsafe) {
public BytesStreamInput(byte buf[], int offset, int length, boolean unsafe) {
this.buf = buf;
this.pos = offset;
this.count = Math.min(offset + length, buf.length);
this.end = offset + length;
this.unsafe = unsafe;
}

Expand All @@ -82,8 +82,8 @@ public BytesRef readBytesRef(int length) throws IOException {

@Override
public long skip(long n) throws IOException {
if (pos + n > count) {
n = count - pos;
if (pos + n > end) {
n = end - pos;
}
if (n < 0) {
return 0;
Expand All @@ -98,7 +98,7 @@ public int position() {

@Override
public int read() throws IOException {
return (pos < count) ? (buf[pos++] & 0xff) : -1;
return (pos < end) ? (buf[pos++] & 0xff) : -1;
}

@Override
Expand All @@ -108,11 +108,11 @@ public int read(byte[] b, int off, int len) throws IOException {
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
}
if (pos >= count) {
if (pos >= end) {
return -1;
}
if (pos + len > count) {
len = count - pos;
if (pos + len > end) {
len = end - pos;
}
if (len <= 0) {
return 0;
Expand All @@ -128,7 +128,7 @@ public byte[] underlyingBuffer() {

@Override
public byte readByte() throws IOException {
if (pos >= count) {
if (pos >= end) {
throw new EOFException();
}
return buf[pos++];
Expand All @@ -139,11 +139,11 @@ public void readBytes(byte[] b, int offset, int len) throws IOException {
if (len == 0) {
return;
}
if (pos >= count) {
if (pos >= end) {
throw new EOFException();
}
if (pos + len > count) {
len = count - pos;
if (pos + len > end) {
len = end - pos;
}
if (len <= 0) {
throw new EOFException();
Expand Down
16 changes: 15 additions & 1 deletion src/test/java/org/elasticsearch/common/io/StreamsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
package org.elasticsearch.common.io;

import com.google.common.base.Charsets;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.io.stream.BytesStreamInput;
import org.elasticsearch.test.ElasticsearchTestCase;
import org.junit.Test;

import java.io.*;
import java.util.Arrays;

import static org.elasticsearch.common.io.Streams.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

/**
Expand Down Expand Up @@ -87,5 +89,17 @@ public void testCopyToString() throws IOException {
String result = copyToString(in);
assertThat(result, equalTo(content));
}

@Test
public void testBytesStreamInput() throws IOException {
byte stuff[] = new byte[] { 0, 1, 2, 3 };
BytesRef stuffRef = new BytesRef(stuff, 2, 2);
BytesArray stuffArray = new BytesArray(stuffRef);
BytesStreamInput input = new BytesStreamInput(stuffArray);
assertEquals(2, input.read());
assertEquals(3, input.read());
assertEquals(-1, input.read());
input.close();
}

}