Package org.johnnei.javatorrent.network
Class InStream
- java.lang.Object
-
- org.johnnei.javatorrent.network.InStream
-
public class InStream extends Object
-
-
Constructor Summary
Constructors Constructor Description InStream(byte[] data)Creates a new buffered input stream based on the given byte array.InStream(byte[] data, int offset, int length)Creates a new buffered input stream based on the section in the given byte array.InStream(byte[] data, int offset, int length, Duration readDuration)Creates a new buffered input stream based on the section in the given byte array.InStream(byte[] data, Duration readDuration)Creates a new buffered input stream based on the given byte array.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Calculates the amount of bytes still remaining to read on the stream.Optional<Duration>getReadDuration()Gets the duration it took to read this message.voidmark()Marks the current position.voidmoveBack(int n)Moves the read pointer back withnposition.booleanreadBoolean()Reads a boolean value from the stream.bytereadByte()Reads a signed byte from the stream.charreadChar()Reads a character from the stream.voidreadFully(byte[] b)Fills the given byte array with data read from the stream.voidreadFully(byte[] b, int off, int len)Fills the given section of the byte array with data read from the stream.byte[]readFully(int length)Readslengthbytes into a new byte array.intreadInt()Reads an integer from the stream.longreadLong()Reads a long from the stream.shortreadShort()Reads a short from the stream.StringreadString(int length)Reads a string from the stream.intreadUnsignedByte()Reads an unsigned byte from the stream.intreadUnsignedShort()Reads an unsigned short from the stream.voidresetToMark()Returns to the last marked position withmark()or0if no mark has happened before.intskipBytes(int n)Skips the nextnbytes on the stream.
-
-
-
Constructor Detail
-
InStream
public InStream(byte[] data)
Creates a new buffered input stream based on the given byte array. CallsInStream(data, 0, data.length, null).- Parameters:
data- The byte buffer- See Also:
InStream(byte[], int, int)
-
InStream
public InStream(byte[] data, Duration readDuration)Creates a new buffered input stream based on the given byte array. CallsInStream(data, 0, data.length, readDuration).- Parameters:
data- The byte bufferreadDuration- The duration it took to read the given buffer- See Also:
InStream(byte[], int, int, Duration)
-
InStream
public InStream(byte[] data, int offset, int length)Creates a new buffered input stream based on the section in the given byte array.- Parameters:
data- The byte bufferoffset- The starting offsetlength- The amount of bytes available- See Also:
InStream(byte[], int, int, Duration)
-
InStream
public InStream(byte[] data, int offset, int length, Duration readDuration)Creates a new buffered input stream based on the section in the given byte array.- Parameters:
data- The byte bufferoffset- The starting offsetlength- The amount of bytes availablereadDuration- The duration it took to read the given buffer
-
-
Method Detail
-
readBoolean
public boolean readBoolean()
Reads a boolean value from the stream. The boolean istruewhen the read byte is not zero.- Returns:
- The read boolean
-
readByte
public byte readByte()
Reads a signed byte from the stream.- Returns:
- The read byte.
-
readChar
public char readChar()
Reads a character from the stream.- Returns:
- The read character.
-
readFully
public void readFully(byte[] b)
Fills the given byte array with data read from the stream.- Parameters:
b- The byte buffer to copy the data to.
-
readFully
public void readFully(byte[] b, int off, int len)Fills the given section of the byte array with data read from the stream.- Parameters:
b- The byte buffer to copy the data to.off- The starting offset.len- The amount of bytes to copy.
-
readFully
public byte[] readFully(int length)
Readslengthbytes into a new byte array.- Parameters:
length- The amount of bytes to read- Returns:
- The read bytes.
-
readInt
public int readInt()
Reads an integer from the stream.- Returns:
- The read integer.
-
readLong
public long readLong()
Reads a long from the stream.- Returns:
- The read long.
-
readShort
public short readShort()
Reads a short from the stream.- Returns:
- The read short.
-
readUnsignedByte
public int readUnsignedByte()
Reads an unsigned byte from the stream.- Returns:
- The read byte.
-
readUnsignedShort
public int readUnsignedShort()
Reads an unsigned short from the stream.- Returns:
- The read short.
-
skipBytes
public int skipBytes(int n)
Skips the nextnbytes on the stream.- Parameters:
n- The amount of bytes to skip- Returns:
- The actual amount of bytes skipped.
-
moveBack
public void moveBack(int n)
Moves the read pointer back withnposition. This method cannot be combined withmark()on the same stream as this method assumes the marked position to be 0.- Parameters:
n- The amount of bytes to 'unread'- See Also:
mark()
-
available
public int available()
Calculates the amount of bytes still remaining to read on the stream.- Returns:
- The amount of readable bytes.
-
readString
public String readString(int length)
Reads a string from the stream.- Parameters:
length- The amount of characters in the string- Returns:
- The read string.
-
mark
public void mark()
Marks the current position.
-
resetToMark
public void resetToMark()
Returns to the last marked position withmark()or0if no mark has happened before.
-
getReadDuration
public Optional<Duration> getReadDuration()
Gets the duration it took to read this message.- Returns:
- The duration it took or
Optional.empty()if unknown
-
-