Skip to content
Permalink
Browse files
add available in RandomAccessInput (#22)
  • Loading branch information
houzhizhen committed Mar 17, 2021
1 parent 1b60c55 commit 9d33923ecba0a0b6083417f5fc8223b22f531c2f
Showing 7 changed files with 85 additions and 0 deletions.
@@ -136,6 +136,11 @@ protected void require(int size) throws IOException {
}
}

@Override
public long available() throws IOException {
return this.file.length() - this.position();
}

private void shiftAndFillBuffer() throws IOException {
this.shiftBuffer();
this.fillBuffer();
@@ -158,6 +158,14 @@ protected void require(int size) throws IOException {
}
}

/**
* The stream has no limit.
*/
@Override
public long available() throws IOException {
return Long.MAX_VALUE;
}

private void shiftAndFillBuffer() throws IOException {
this.shiftBuffer();
this.fillBuffer();
@@ -24,8 +24,16 @@

public interface RandomAccessInput extends DataInput {

/**
* @return The current position.
*/
long position();

/**
* Set current position to specified position, measured from the beginning
* of input.
* @throws IOException If can't seek to specified position.
*/
void seek(long position) throws IOException;

/**
@@ -35,4 +43,10 @@ public interface RandomAccessInput extends DataInput {
* return the number of bytes actually skipped.
*/
long skip(long n) throws IOException;

/**
* @return The total bytes size unread.
* @throws IOException
*/
long available() throws IOException;
}
@@ -201,6 +201,11 @@ public long skip(long bytesToSkip) throws IOException {
return positionBeforeSkip;
}

@Override
public long available() throws IOException {
return this.limit - this.position;
}

public int remaining() {
return this.limit - this.position;
}
@@ -375,6 +375,27 @@ public void testPosition() throws IOException {
}
}

@Test
public void testAvailable() throws IOException {
int size = 1024;
File file = this.createTempFile();
try {
try (BufferedFileOutput output = this.createOutput(file)) {
for (int i = 0; i < size; i++) {
output.writeInt(i);
}
}
try (BufferedFileInput input = this.createInput(file)) {
for (int i = 0; i < size; i++) {
Assert.assertEquals(4096 - i * 4, input.available());
Assert.assertEquals(i, input.readInt());
}
}
} finally {
FileUtils.deleteQuietly(file);
}
}

@Test
public void testLongPerformanceUnsafe() throws IOException {
long size = 1024;
@@ -377,6 +377,24 @@ public void testPosition() throws IOException {
}
}

@Test
public void testAvailable() throws IOException {
int size = 1;
File file = this.createTempFile();
try {
try (BufferedStreamOutput output = this.createOutput(file)) {
for (int i = 0; i < size; i++) {
output.writeInt(i);
}
}
try (BufferedStreamInput input = this.createInput(file)) {
Assert.assertEquals(Long.MAX_VALUE, input.available());
}
} finally {
FileUtils.deleteQuietly(file);
}
}

private static File createTempFile() throws IOException {
return File.createTempFile(UUID.randomUUID().toString(), null);
}
@@ -399,4 +399,18 @@ public void testSeek() throws IOException {
Assert.assertEquals(Integer.MAX_VALUE, input.readInt());
Assert.assertEquals(Integer.MIN_VALUE, input.readInt());
}

@Test
public void testAvailable() throws IOException {
UnsafeByteArrayOutput output = new UnsafeByteArrayOutput();
for (int i = -128; i <= 127; i++) {
output.write(i);
}
UnsafeByteArrayInput input = new UnsafeByteArrayInput(
output.toByteArray());
for (int i = 0; i < 256; i++) {
Assert.assertEquals(256 - i, input.available());
input.readByte();
}
}
}

0 comments on commit 9d33923

Please sign in to comment.