Skip to content

Fix single-byte read() in various InputStream implementations #1058

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

Open
wants to merge 6 commits into
base: next
Choose a base branch
from
Open
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
13 changes: 7 additions & 6 deletions src/freenet/crypt/AEADInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,19 @@ public AEADInputStream(InputStream is, byte[] key, BlockCipher hashCipher,
public final int getIVSize() {
return cipher.getUnderlyingCipher().getBlockSize() / 8;
}

private final byte[] onebyte = new byte[1];


private final byte[] excess;
private int excessEnd;
private int excessPtr;

@Override
public int read() throws IOException {
int length = read(onebyte);
if(length <= 0) return -1;
else return onebyte[0];
byte[] buf = new byte[1];
int length = read(buf, 0, 1);
if (length > 0) {
return Byte.toUnsignedInt(buf[0]);
}
return -1;
}

@Override
Expand Down
18 changes: 8 additions & 10 deletions src/freenet/crypt/EncryptedRandomAccessBucket.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,9 @@ public MyOutputStream(OutputStream out, SkippingStreamCipher cipher) {
this.cipherWrite = cipher;
}

private final byte[] one = new byte[1];

@Override
public void write(int x) throws IOException {
one[0] = (byte)x;
write(one);
write(new byte[]{(byte) x}, 0, 1);
}

@Override
Expand Down Expand Up @@ -265,14 +262,15 @@ public MyInputStream(InputStream in, SkippingStreamCipher cipher) {
super(in);
this.cipherRead = cipher;
}

private byte[] one = new byte[1];


@Override
public int read() throws IOException {
int readBytes = read(one);
if(readBytes <= 0) return readBytes;
return one[0] & 0xFF;
byte[] buf = new byte[1];
int length = read(buf, 0, 1);
if (length > 0) {
return Byte.toUnsignedInt(buf[0]);
}
return -1;
}

@Override
Expand Down
19 changes: 13 additions & 6 deletions src/freenet/support/ByteBufferInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,21 @@ public ByteBufferInputStream(ByteBuffer buf) {

@Override
public int read() throws IOException {
try {
return buf.get() & Integer.MAX_VALUE;
} catch (BufferUnderflowException e) {
return -1;
byte[] buf = new byte[1];
int length = read(buf, 0, 1);
if (length > 0) {
return Byte.toUnsignedInt(buf[0]);
}
return -1;
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
int read = Math.min(len, buf.remaining());
buf.get(b, off, read);
return read;
}



public int remaining() {
return buf.remaining();
}
Expand Down
12 changes: 5 additions & 7 deletions src/freenet/support/io/PaddedBucket.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,12 @@ public MyInputStream(InputStream is) {

@Override
public int read() throws IOException {
synchronized(PaddedBucket.this) {
if(counter >= size) return -1;
byte[] buf = new byte[1];
int length = read(buf, 0, 1);
if (length > 0) {
return Byte.toUnsignedInt(buf[0]);
}
int ret = in.read();
synchronized(PaddedBucket.this) {
counter++;
}
return ret;
return -1;
}

@Override
Expand Down
12 changes: 5 additions & 7 deletions src/freenet/support/io/PaddedRandomAccessBucket.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,12 @@ public MyInputStream(InputStream is) {

@Override
public int read() throws IOException {
synchronized(PaddedRandomAccessBucket.this) {
if(counter >= size) return -1;
byte[] buf = new byte[1];
int length = read(buf, 0, 1);
if (length > 0) {
return Byte.toUnsignedInt(buf[0]);
}
int ret = in.read();
synchronized(PaddedRandomAccessBucket.this) {
counter++;
}
return ret;
return -1;
}

@Override
Expand Down
12 changes: 8 additions & 4 deletions src/freenet/support/io/RAFInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
public class RAFInputStream extends InputStream {

private final RandomAccessBuffer underlying;
private final long rafLength;

private long rafOffset;
private long rafLength;
private final byte[] oneByte = new byte[1];

public RAFInputStream(RandomAccessBuffer data, long offset, long size) {
this.underlying = data;
Expand All @@ -21,8 +21,12 @@ public RAFInputStream(RandomAccessBuffer data, long offset, long size) {

@Override
public int read() throws IOException {
read(oneByte);
return oneByte[0];
byte[] buf = new byte[1];
int length = read(buf, 0, 1);
if (length > 0) {
return Byte.toUnsignedInt(buf[0]);
}
return -1;
}

@Override
Expand Down