Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
jaymarvels committed Sep 23, 2014
1 parent 20221d7 commit a1dc9bc
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion BinaryReaderDotNet
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import java.nio.ByteOrder;
* A java class that is capable for parsing .net BinaryWriter encoded files.
* Some methods are not implemented as I have not need for them. Enjoy!
* @author robbiev
* fork @author jaymarvels
*
*/
public class BinaryReaderDotNet extends FilterInputStream {

public BinaryReaderDotNet(InputStream in) {
super(in);
setPosition(0);
}

/**
Expand All @@ -22,10 +24,12 @@ public class BinaryReaderDotNet extends FilterInputStream {
* @throws IOException
*/
public int readInt32() throws IOException {
incrementPosition(4);
return ByteBuffer.wrap(this.readBytes(4))
.order(ByteOrder.LITTLE_ENDIAN)
.getInt()
;

}

/**
Expand All @@ -34,6 +38,7 @@ public class BinaryReaderDotNet extends FilterInputStream {
* @throws IOException
*/
public long readUInt32() throws IOException {
incrementPosition(4);
return this.readInt32() & 0xFFFFFFFFL;
}

Expand All @@ -43,6 +48,7 @@ public class BinaryReaderDotNet extends FilterInputStream {
* @throws IOException
*/
public int readInt16() throws IOException {
incrementPosition(2);
return ByteBuffer.wrap(this.readBytes(2))
.order(ByteOrder.LITTLE_ENDIAN)
.getShort()
Expand All @@ -55,15 +61,30 @@ public class BinaryReaderDotNet extends FilterInputStream {
* @throws IOException
*/
public int readUInt16() throws IOException {
incrementPosition(2);
return this.readInt16() & 0xFFFF;
}

/**
* Reads a 2-byte signed integer from the current stream and advances the current position of the stream by two bytes. Returns a 32-bit int as there are not 16-bit ints in java.
* @return
* @throws IOException
*/
public short readShort() throws IOException {
incrementPosition(2);
return ByteBuffer.wrap(this.readBytes(2))
.order(ByteOrder.LITTLE_ENDIAN)
.getShort()
;
}

/**
* Reads a string from the current stream. The string is prefixed with the length, encoded as an integer seven bits at a time.
* @return
* @throws IOException
*/
public String readString() throws IOException {
incrementPosition(this.getStringLength());
return new String(this.readBytes(this.getStringLength()));
}

Expand All @@ -73,6 +94,7 @@ public class BinaryReaderDotNet extends FilterInputStream {
* @throws IOException
*/
public boolean readBoolean() throws IOException {
incrementPosition(1);
return this.readBytes(1)[0] != 0;
}

Expand All @@ -82,6 +104,7 @@ public class BinaryReaderDotNet extends FilterInputStream {
* @throws IOException
*/
public float readSingle() throws IOException {
incrementPosition(4);
return ByteBuffer.wrap(this.readBytes(4))
.order(ByteOrder.LITTLE_ENDIAN)
.getFloat()
Expand Down Expand Up @@ -112,6 +135,20 @@ public class BinaryReaderDotNet extends FilterInputStream {
}
return count;
}

/**
* Read a single byte.
* @return
* @throws IOException
*/
public byte readByte() throws IOException {
incrementPosition(1);
return ByteBuffer.wrap(this.readBytes(1))
.order(ByteOrder.LITTLE_ENDIAN)
.get()
;
}


/**
* Read an arbitrary number of bytes.
Expand All @@ -122,6 +159,25 @@ public class BinaryReaderDotNet extends FilterInputStream {
public byte[] readBytes(int length) throws IOException {
byte[] bytes = new byte[length];
this.read(bytes);
incrementPosition(length);
return bytes;
}
}

private void incrementPosition(int increment)
{
setPosition(getPosition()+ increment);
}

private int position;

public int getPosition(){
return position;
}
private void setPosition(int position){
this.position = position;
}




}

0 comments on commit a1dc9bc

Please sign in to comment.