public class PacketLoader
extends java.lang.Object
IPacket
to InputStream
and OutputStream
.
Packets can have contents checking support using DigestProvider
.Constructor and Description |
---|
PacketLoader()
Constructs a new Packet loader instance.
|
PacketLoader(com.captainalm.lib.stdcrypt.digest.DigestProvider provider)
Constructs a new Packet loader instance with the specified
DigestProvider . |
PacketLoader(com.captainalm.lib.stdcrypt.digest.DigestProvider provider,
boolean oldPacketFormat)
Constructs a new Packet loader instance with the specified
DigestProvider
and if the old packet format should be used. |
Modifier and Type | Method and Description |
---|---|
static int |
addMostSignificantFlag(int value)
Adds the most significant flag to the given integer.
|
boolean |
areInvalidPacketsAllowed()
Gets whether invalid packets are allowed to be read and written.
|
static byte[] |
getByteArrayFromInteger(int i)
Saves an Integer into a byte array.
|
com.captainalm.lib.stdcrypt.digest.DigestProvider |
getHashProvider()
This field provides the
DigestProvider to use for the payload of the packets on the trailer. |
static int |
getIntegerFromByteArray(byte[] bytes)
Loads an Integer from a byte array.
|
int |
getPacketSize(IPacket packet,
boolean includeInformation,
boolean ignoreDigest)
Gets the total size of a written packet in bytes.
|
boolean |
isOldPacketFormatInUse()
Is the old packet format in use (No explicit hash indication nor length).
|
static byte[] |
readArrayFromInputStream(java.io.InputStream inputStream,
int length)
Reads in a byte array of a specified length from an
InputStream . |
static byte |
readByteFromInputStream(java.io.InputStream inputStream)
Reads a byte from an
InputStream . |
static int |
readByteIntegerFromInputStream(java.io.InputStream inputStream)
Reads a byte (In int form) from an
InputStream . |
static int |
readInteger(java.io.InputStream inputStream)
Reads an Integer from an
InputStream . |
IPacket |
readPacket(java.io.InputStream inputStream,
IPacketFactory factory,
PacketProtocolInformation information)
Reads a
IPacket from an input stream. |
IPacket |
readPacketNoDigest(byte[] arrayIn,
IPacketFactory factory,
PacketProtocolInformation information)
Reads a
IPacket from a byte array (No digest support). |
IPacket |
readPacketNoDigest(java.io.InputStream inputStream,
IPacketFactory factory,
PacketProtocolInformation information)
Reads a
IPacket from an input stream (No digest support). |
IPacket |
readStreamedPacket(java.io.InputStream inputStream,
IPacketFactory factory,
PacketProtocolInformation information)
Reads a
IStreamedPacket from an input stream. |
IPacket |
readStreamedPacketNoDigest(java.io.InputStream inputStream,
IPacketFactory factory,
PacketProtocolInformation information)
Reads a
IStreamedPacket from an input stream (No digest support). |
void |
setAllowInvalidPackets(boolean allowInvalidPackets)
This sets whether invalid packets are allowed to be read and written.
|
void |
setOldPacketFormatUsage(boolean useOldFormat)
Sets if the old packet format should be used (No explicit hash indication nor length).
|
static int |
subtractMostSignificantFlag(int value)
Subtracts the most significant flag from the given integer.
|
static void |
writeInteger(java.io.OutputStream outputStream,
int i)
Writes an Integer to the
OutputStream using 4 bytes. |
void |
writePacket(java.io.OutputStream outputStream,
IPacket packet,
boolean writeInformation)
Writes a
IPacket to an output stream (No digest support). |
byte[] |
writePacketNoDigest(IPacket packet,
boolean writeInformation)
Returns a
IPacket to a byte array (No digest support). |
void |
writePacketNoDigest(java.io.OutputStream outputStream,
IPacket packet,
boolean writeInformation)
Writes a
IPacket to an output stream. |
public PacketLoader()
PacketLoader(DigestProvider)
public PacketLoader(com.captainalm.lib.stdcrypt.digest.DigestProvider provider)
DigestProvider
.
If using a digest provider, make sure all endpoints use the same algorithm;
if null, no trailer is created;
this is ignored if saving / loading packets from byte arrays.provider
- The digest provider or null.public PacketLoader(com.captainalm.lib.stdcrypt.digest.DigestProvider provider, boolean oldPacketFormat)
DigestProvider
and if the old packet format should be used.
If using a digest provider, make sure all endpoints use the same algorithm;
if null, no trailer is created;
this is ignored if saving / loading packets from byte arrays.provider
- The digest provider or null.oldPacketFormat
- If the old packet format should be used (No explicit hash indication nor length).public com.captainalm.lib.stdcrypt.digest.DigestProvider getHashProvider()
DigestProvider
to use for the payload of the packets on the trailer.public boolean areInvalidPacketsAllowed()
public void setAllowInvalidPackets(boolean allowInvalidPackets)
allowInvalidPackets
- If invalid packets can be processed.public boolean isOldPacketFormatInUse()
public void setOldPacketFormatUsage(boolean useOldFormat)
useOldFormat
- If the old packet format should be used.public static int addMostSignificantFlag(int value)
value
- The integer to add the flag to.public static int subtractMostSignificantFlag(int value)
value
- The integer to subtract the flag from.public IPacket readPacketNoDigest(byte[] arrayIn, IPacketFactory factory, PacketProtocolInformation information) throws PacketException
IPacket
from a byte array (No digest support).
If the information parameter is null, this is obtained as part of the reading.
NOTE: The getHashProvider()
for digests is NOT supported and no digest is expected for these packets.arrayIn
- The byte array for reading.factory
- The IPacketFactory
to use to generate packets.information
- The protocol information or null.java.lang.NullPointerException
- The arrayIn or the factory is null.PacketException
- An Exception has occurred.public IPacket readPacket(java.io.InputStream inputStream, IPacketFactory factory, PacketProtocolInformation information) throws java.io.IOException, PacketException
IPacket
from an input stream.
If the information parameter is null, this is obtained as part of the reading.inputStream
- The input stream for reading.factory
- The IPacketFactory
to use to generate packets.information
- The protocol information or null.java.lang.NullPointerException
- The inputStream or the factory is null.java.io.IOException
- A stream exception occurs.PacketException
- An Exception has occurred.public IPacket readPacketNoDigest(java.io.InputStream inputStream, IPacketFactory factory, PacketProtocolInformation information) throws java.io.IOException, PacketException
IPacket
from an input stream (No digest support).
If the information parameter is null, this is obtained as part of the reading.
NOTE: The getHashProvider()
for digests is NOT supported and no digest is expected for these packets.inputStream
- The input stream for reading.factory
- The IPacketFactory
to use to generate packets.information
- The protocol information or null.java.lang.NullPointerException
- The inputStream or the factory is null.java.io.IOException
- A stream exception occurs.PacketException
- An Exception has occurred.public IPacket readStreamedPacket(java.io.InputStream inputStream, IPacketFactory factory, PacketProtocolInformation information) throws java.io.IOException, PacketException
IStreamedPacket
from an input stream.
If the information parameter is null, this is obtained as part of the reading.
NOTE: The packet may be an IPacket
if no stream packet is available for that protocol.inputStream
- The input stream for reading.factory
- The IPacketFactory
to use to generate packets.information
- The protocol information or null.java.lang.NullPointerException
- The inputStream or the factory is null.java.io.IOException
- A stream exception occurs.PacketException
- An Exception has occurred.public IPacket readStreamedPacketNoDigest(java.io.InputStream inputStream, IPacketFactory factory, PacketProtocolInformation information) throws java.io.IOException, PacketException
IStreamedPacket
from an input stream (No digest support).
If the information parameter is null, this is obtained as part of the reading.
NOTE: The packet may be an IPacket
if no stream packet is available for that protocol.
NOTE: The getHashProvider()
for digests is NOT supported and no digest is expected for these packets.inputStream
- The input stream for reading.factory
- The IPacketFactory
to use to generate packets.information
- The protocol information or null.java.lang.NullPointerException
- The inputStream or the factory is null.java.io.IOException
- A stream exception occurs.PacketException
- An Exception has occurred.public byte[] writePacketNoDigest(IPacket packet, boolean writeInformation) throws PacketException
IPacket
to a byte array (No digest support).
NOTE: The getHashProvider()
for digests is NOT supported and no digest is expected for these packets.packet
- The packet to save.writeInformation
- Write the PacketProtocolInformation
to the beginning of the array.java.lang.NullPointerException
- A parameter is null.PacketException
- An Exception has occurred.public void writePacket(java.io.OutputStream outputStream, IPacket packet, boolean writeInformation) throws java.io.IOException, PacketException
IPacket
to an output stream (No digest support).outputStream
- The output stream for writing.packet
- The packet to save.writeInformation
- Write the PacketProtocolInformation
to the stream.java.lang.NullPointerException
- A parameter is null.java.io.IOException
- A stream exception occurs.PacketException
- An Exception has occurred.public void writePacketNoDigest(java.io.OutputStream outputStream, IPacket packet, boolean writeInformation) throws java.io.IOException, PacketException
IPacket
to an output stream.
NOTE: The getHashProvider()
for digests is NOT supported and no digest is expected for these packets.outputStream
- The output stream for writing.packet
- The packet to save.writeInformation
- Write the PacketProtocolInformation
to the stream.java.lang.NullPointerException
- A parameter is null.java.io.IOException
- A stream exception occurs.PacketException
- An Exception has occurred.public static int readInteger(java.io.InputStream inputStream) throws java.io.IOException
InputStream
.inputStream
- The input stream to use.java.lang.NullPointerException
- inputStream is null.java.io.IOException
- An I/O error has occurred.public static void writeInteger(java.io.OutputStream outputStream, int i) throws java.io.IOException
OutputStream
using 4 bytes.outputStream
- The output stream to use.i
- The integer to store.java.lang.NullPointerException
- outputStream is null.java.io.IOException
- An I/O error has occurred.public static byte readByteFromInputStream(java.io.InputStream inputStream) throws java.io.IOException
InputStream
.
See also: readByteIntegerFromInputStream(InputStream)
.inputStream
- The input stream to read from.java.lang.NullPointerException
- inputStream is null.java.io.IOException
- An I/O error has occurred or end of stream has been reached.public static int readByteIntegerFromInputStream(java.io.InputStream inputStream) throws java.io.IOException
InputStream
.
See also: readByteFromInputStream(InputStream)
.inputStream
- The input stream to read from.java.lang.NullPointerException
- inputStream is null.java.io.IOException
- An I/O error has occurred or end of stream has been reached.public static byte[] readArrayFromInputStream(java.io.InputStream inputStream, int length) throws java.io.IOException
InputStream
.inputStream
- The input stream to read from.length
- The length of the stream.java.lang.NullPointerException
- inputStream is null.java.lang.IllegalArgumentException
- length is less than 0.java.io.IOException
- An I/O error occurs or end of stream has been reached.public static byte[] getByteArrayFromInteger(int i)
i
- The integer to save.public static int getIntegerFromByteArray(byte[] bytes)
bytes
- The byte array.java.lang.NullPointerException
- bytes is null.java.lang.IllegalArgumentException
- bytes length is not 4.public int getPacketSize(IPacket packet, boolean includeInformation, boolean ignoreDigest) throws PacketException
packet
- The packet to check.includeInformation
- If the 2 byte information header is included.ignoreDigest
- If the digest length should be ignored if available.java.lang.NullPointerException
- packet is null.PacketException
- A Packet Exception has occurred.