mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
5e3ae40d03
- Added unit tests - Added documentation for discovery classes - Improved error handling Tags: #story[1281]
51 lines
1.1 KiB
Java
51 lines
1.1 KiB
Java
package seng302.discoveryServer.util;
|
|
|
|
import java.io.InputStream;
|
|
|
|
public class ReadableByteInputStream {
|
|
private InputStream is;
|
|
|
|
public ReadableByteInputStream(InputStream is){
|
|
this.is = is;
|
|
}
|
|
|
|
/**
|
|
* Get n bytes from the input stream
|
|
* @param n number of bytes
|
|
* @return the bytes read
|
|
* @throws Exception .
|
|
*/
|
|
public byte[] getBytes(int n) throws Exception {
|
|
byte[] bytes = new byte[n];
|
|
for (int i = 0; i < n; i++) {
|
|
bytes[i] = (byte) readByte();
|
|
}
|
|
return bytes;
|
|
}
|
|
|
|
/**
|
|
* Skip n bytes
|
|
* @param n number of bytes to skip
|
|
* @throws Exception
|
|
*/
|
|
public void skipBytes(long n) throws Exception {
|
|
for (int i = 0; i < n; i++) {
|
|
readByte();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Read the next byte from the stream
|
|
* @return The byte that was read
|
|
* @throws Exception .
|
|
*/
|
|
public int readByte() throws Exception {
|
|
int currentByte = is.read();
|
|
|
|
if (currentByte == -1) {
|
|
throw new Exception();
|
|
}
|
|
return currentByte;
|
|
}
|
|
}
|