Skip to content

Commit

Permalink
Merge pull request #450 from bgould/io-enhancements
Browse files Browse the repository at this point in the history
Closeable extends AutoCloseable; RandomAccessFile implements DataInput
  • Loading branch information
joshuawarner32 committed Jul 29, 2015
2 parents 1c039c5 + 7013dba commit 7e4bd18
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 24 deletions.
2 changes: 1 addition & 1 deletion classpath/java/io/Closeable.java
Expand Up @@ -10,7 +10,7 @@

package java.io;

public interface Closeable {
public interface Closeable extends AutoCloseable {
void close()
throws IOException;
}
126 changes: 103 additions & 23 deletions classpath/java/io/RandomAccessFile.java
Expand Up @@ -14,7 +14,8 @@
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class RandomAccessFile {
public class RandomAccessFile implements DataInput, Closeable {

private long peer;
private File file;
private long position = 0;
Expand Down Expand Up @@ -77,44 +78,44 @@ public int skipBytes(int count) throws IOException {

public int read(byte b[], int off, int len) throws IOException {
if(b == null)
throw new IllegalArgumentException();
throw new IllegalArgumentException();
if (peer == 0)
throw new IOException();
if(len == 0)
return 0;
if (position + len > this.length)
throw new IOException();
if(len == 0)
return 0;
if (position + len > this.length)
throw new EOFException();
if (off < 0 || off + len > b.length)
if (off < 0 || off + len > b.length)
throw new ArrayIndexOutOfBoundsException();
int bytesRead = readBytes(peer, position, b, off, len);
position += bytesRead;
return bytesRead;
position += bytesRead;
return bytesRead;
}

public int read(byte b[]) throws IOException {
if(b == null)
throw new IllegalArgumentException();
throw new IllegalArgumentException();
if (peer == 0)
throw new IOException();
if(b.length == 0)
return 0;
if (position + b.length > this.length)
throw new IOException();
if(b.length == 0)
return 0;
if (position + b.length > this.length)
throw new EOFException();
int bytesRead = readBytes(peer, position, b, 0, b.length);
position += bytesRead;
return bytesRead;
position += bytesRead;
return bytesRead;
}

public void readFully(byte b[], int off, int len) throws IOException {
if(b == null)
throw new IllegalArgumentException();
if (b == null)
throw new IllegalArgumentException();
if (peer == 0)
throw new IOException();
if(len == 0)
return;
if (position + len > this.length)
throw new IOException();
if(len == 0)
return;
if (position + len > this.length)
throw new EOFException();
if (off < 0 || off + len > b.length)
if (off < 0 || off + len > b.length)
throw new ArrayIndexOutOfBoundsException();
int n = 0;
do {
Expand All @@ -133,6 +134,85 @@ public void readFully(byte b[]) throws IOException {
private static native int readBytes(long peer, long position, byte[] buffer,
int offset, int length);

public boolean readBoolean() throws IOException {
return readByte() != 0;
}

public int read() throws IOException {
try {
return readByte() & 0xff;
} catch (final EOFException e) {
return -1;
}
}

public byte readByte() throws IOException {
final byte[] buffer = new byte[1];
readFully(buffer);
return buffer[0];
}

public short readShort() throws IOException {
final byte[] buffer = new byte[2];
readFully(buffer);
return (short)((buffer[0] << 8) | buffer[1]);
}

public int readInt() throws IOException {
byte[] buf = new byte[4];
readFully(buf);
return ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
}

public float readFloat() throws IOException {
return Float.floatToIntBits(readInt());
}

public double readDouble() throws IOException {
return Double.doubleToLongBits(readLong());
}

public long readLong() throws IOException {
return ((readInt() & 0xffffffffl) << 32) | (readInt() & 0xffffffffl);
}

public char readChar() throws IOException {
return (char)readShort();
}

public int readUnsignedByte() throws IOException {
return readByte() & 0xff;
}

public int readUnsignedShort() throws IOException {
return readShort() & 0xffff;
}

public String readUTF() throws IOException {
int length = readUnsignedShort();
byte[] bytes = new byte[length];
readFully(bytes);
return new String(bytes, "UTF-8");
}

@Deprecated
public String readLine() throws IOException {
int c = read();
if (c < 0) {
return null;
} else if (c == '\n') {
return "";
}
StringBuilder builder = new StringBuilder();
for (;;) {
builder.append((char)c);
c = read();
if (c < 0 || c == '\n') {
return builder.toString();
}
}
}

public void write(int b) throws IOException {
int count = writeBytes(peer, position, new byte[] { (byte)b }, 0, 1);
if (count > 0) position += count;
Expand Down

0 comments on commit 7e4bd18

Please sign in to comment.