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
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+28 −14
Diff settings

Always

Just for now

@@ -35,7 +35,7 @@

protected int pos;

protected int count;
protected int end;

private final boolean unsafe;

@@ -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;
}

@@ -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;
}

@@ -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;
@@ -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
@@ -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;
@@ -128,7 +128,7 @@ public int read(byte[] b, int off, int len) throws IOException {

@Override
public byte readByte() throws IOException {
if (pos >= count) {
if (pos >= end) {
throw new EOFException();
}
return buf[pos++];
@@ -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();
@@ -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;

/**
@@ -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();
}

}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.