Package com.ericsson.otp.erlang
Class OtpInputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.ByteArrayInputStream
-
- com.ericsson.otp.erlang.OtpInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class OtpInputStream extends java.io.ByteArrayInputStreamProvides a stream for decoding Erlang terms from external format.Note that this class is not synchronized, if you need synchronization you must provide it yourself.
-
-
Field Summary
Fields Modifier and Type Field Description static intDECODE_INT_LISTS_AS_STRINGS
-
Constructor Summary
Constructors Constructor Description OtpInputStream(byte[] buf)OtpInputStream(byte[] buf, int flags)Create a stream from a buffer containing encoded Erlang terms.OtpInputStream(byte[] buf, int offset, int length, int flags)Create a stream from a buffer containing encoded Erlang terms at the given offset and length.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static longbyte_array_to_long(byte[] b, boolean unsigned)intgetPos()Get the current position in the stream.intpeek()Alias for peek1()intpeek1()Look ahead one position in the stream without consuming the byte found there.intpeek1skip_version()OtpErlangObjectread_any()Read an arbitrary Erlang term from the stream.java.lang.Stringread_atom()Read an Erlang atom from the stream.byte[]read_binary()Read an Erlang binary from the stream.byte[]read_bitstr(int[] pad_bits)Read an Erlang bitstr from the stream.booleanread_boolean()Read an Erlang atom from the stream and interpret the value as a boolean.byteread_byte()Read one byte from the stream.charread_char()Read a character from the stream.OtpErlangObjectread_compressed()Read a compressed term from the streamdoubleread_double()Read an Erlang float from the stream.OtpErlangExternalFunread_external_fun()floatread_float()Read an Erlang float from the stream.OtpErlangFunread_fun()intread_int()Read an integer from the stream.byte[]read_integer_byte_array()Read an integer from the stream.intread_list_head()Read a list header from the stream.longread_long()Read a long from the stream.longread_long(boolean unsigned)intread_map_head()intread_nil()Read an empty list from the stream.OtpErlangPidread_pid()Read an Erlang PID from the stream.OtpErlangPortread_port()Read an Erlang port from the stream.OtpErlangRefread_ref()Read an Erlang reference from the stream.shortread_short()Read a short from the stream.java.lang.Stringread_string()Read a string from the stream.intread_tuple_head()Read a tuple header from the stream.intread_uint()Read an unsigned integer from the stream.longread_ulong()Read an unsigned long from the stream.shortread_ushort()Read an unsigned short from the stream.intread1()Read a one byte integer from the stream.intread1skip_version()intread2BE()Read a two byte big endian integer from the stream.intread2LE()Read a two byte little endian integer from the stream.intread4BE()Read a four byte big endian integer from the stream.intread4LE()Read a four byte little endian integer from the stream.longread8BE()Read a eight byte big endian integer from the stream.longreadBE(int n)Read a bigendian integer from the stream.longreadLE(int n)Read a little endian integer from the stream.intreadN(byte[] abuf)Read an array of bytes from the stream.intreadN(byte[] abuf, int off, int len)Read an array of bytes from the stream.intsetPos(int pos)Set the current position in the stream.
-
-
-
Constructor Detail
-
OtpInputStream
public OtpInputStream(byte[] buf)
- Parameters:
buf-
-
OtpInputStream
public OtpInputStream(byte[] buf, int flags)Create a stream from a buffer containing encoded Erlang terms.- Parameters:
flags-
-
OtpInputStream
public OtpInputStream(byte[] buf, int offset, int length, int flags)Create a stream from a buffer containing encoded Erlang terms at the given offset and length.- Parameters:
flags-
-
-
Method Detail
-
getPos
public int getPos()
Get the current position in the stream.- Returns:
- the current position in the stream.
-
setPos
public int setPos(int pos)
Set the current position in the stream.- Parameters:
pos- the position to move to in the stream. If pos indicates a position beyond the end of the stream, the position is move to the end of the stream instead. If pos is negative, the position is moved to the beginning of the stream instead.- Returns:
- the previous position in the stream.
-
readN
public int readN(byte[] abuf) throws OtpErlangDecodeExceptionRead an array of bytes from the stream. The method reads at most buf.length bytes from the input stream.- Returns:
- the number of bytes read.
- Throws:
OtpErlangDecodeException- if the next byte cannot be read.
-
readN
public int readN(byte[] abuf, int off, int len) throws OtpErlangDecodeExceptionRead an array of bytes from the stream. The method reads at most len bytes from the input stream into offset off of the buffer.- Returns:
- the number of bytes read.
- Throws:
OtpErlangDecodeException- if the next byte cannot be read.
-
peek
public int peek() throws OtpErlangDecodeExceptionAlias for peek1()- Throws:
OtpErlangDecodeException
-
peek1
public int peek1() throws OtpErlangDecodeExceptionLook ahead one position in the stream without consuming the byte found there.- Returns:
- the next byte in the stream, as an integer.
- Throws:
OtpErlangDecodeException- if the next byte cannot be read.
-
peek1skip_version
public int peek1skip_version() throws OtpErlangDecodeException- Throws:
OtpErlangDecodeException
-
read1
public int read1() throws OtpErlangDecodeExceptionRead a one byte integer from the stream.- Returns:
- the byte read, as an integer.
- Throws:
OtpErlangDecodeException- if the next byte cannot be read.
-
read1skip_version
public int read1skip_version() throws OtpErlangDecodeException- Throws:
OtpErlangDecodeException
-
read2BE
public int read2BE() throws OtpErlangDecodeExceptionRead a two byte big endian integer from the stream.- Returns:
- the bytes read, converted from big endian to an integer.
- Throws:
OtpErlangDecodeException- if the next byte cannot be read.
-
read4BE
public int read4BE() throws OtpErlangDecodeExceptionRead a four byte big endian integer from the stream.- Returns:
- the bytes read, converted from big endian to an integer.
- Throws:
OtpErlangDecodeException- if the next byte cannot be read.
-
read8BE
public long read8BE() throws OtpErlangDecodeExceptionRead a eight byte big endian integer from the stream.- Returns:
- the bytes read, converted from big endian to a long integer.
- Throws:
OtpErlangDecodeException- if the next byte cannot be read.
-
read2LE
public int read2LE() throws OtpErlangDecodeExceptionRead a two byte little endian integer from the stream.- Returns:
- the bytes read, converted from little endian to an integer.
- Throws:
OtpErlangDecodeException- if the next byte cannot be read.
-
read4LE
public int read4LE() throws OtpErlangDecodeExceptionRead a four byte little endian integer from the stream.- Returns:
- the bytes read, converted from little endian to an integer.
- Throws:
OtpErlangDecodeException- if the next byte cannot be read.
-
readLE
public long readLE(int n) throws OtpErlangDecodeExceptionRead a little endian integer from the stream.- Parameters:
n- the number of bytes to read- Returns:
- the bytes read, converted from little endian to an integer.
- Throws:
OtpErlangDecodeException- if the next byte cannot be read.
-
readBE
public long readBE(int n) throws OtpErlangDecodeExceptionRead a bigendian integer from the stream.- Parameters:
n- the number of bytes to read- Returns:
- the bytes read, converted from big endian to an integer.
- Throws:
OtpErlangDecodeException- if the next byte cannot be read.
-
read_boolean
public boolean read_boolean() throws OtpErlangDecodeExceptionRead an Erlang atom from the stream and interpret the value as a boolean.- Returns:
- true if the atom at the current position in the stream contains the value 'true' (ignoring case), false otherwise.
- Throws:
OtpErlangDecodeException- if the next term in the stream is not an atom.
-
read_atom
public java.lang.String read_atom() throws OtpErlangDecodeExceptionRead an Erlang atom from the stream.- Returns:
- a String containing the value of the atom.
- Throws:
OtpErlangDecodeException- if the next term in the stream is not an atom.
-
read_binary
public byte[] read_binary() throws OtpErlangDecodeExceptionRead an Erlang binary from the stream.- Returns:
- a byte array containing the value of the binary.
- Throws:
OtpErlangDecodeException- if the next term in the stream is not a binary.
-
read_bitstr
public byte[] read_bitstr(int[] pad_bits) throws OtpErlangDecodeExceptionRead an Erlang bitstr from the stream.- Parameters:
pad_bits- an int array whose first element will be set to the number of pad bits in the last byte.- Returns:
- a byte array containing the value of the bitstr.
- Throws:
OtpErlangDecodeException- if the next term in the stream is not a bitstr.
-
read_float
public float read_float() throws OtpErlangDecodeExceptionRead an Erlang float from the stream.- Returns:
- the float value.
- Throws:
OtpErlangDecodeException- if the next term in the stream is not a float.
-
read_double
public double read_double() throws OtpErlangDecodeExceptionRead an Erlang float from the stream.- Returns:
- the float value, as a double.
- Throws:
OtpErlangDecodeException- if the next term in the stream is not a float.
-
read_byte
public byte read_byte() throws OtpErlangDecodeExceptionRead one byte from the stream.- Returns:
- the byte read.
- Throws:
OtpErlangDecodeException- if the next byte cannot be read.
-
read_char
public char read_char() throws OtpErlangDecodeExceptionRead a character from the stream.- Returns:
- the character value.
- Throws:
OtpErlangDecodeException- if the next term in the stream is not an integer that can be represented as a char.
-
read_uint
public int read_uint() throws OtpErlangDecodeExceptionRead an unsigned integer from the stream.- Returns:
- the integer value.
- Throws:
OtpErlangDecodeException- if the next term in the stream cannot be represented as a positive integer.
-
read_int
public int read_int() throws OtpErlangDecodeExceptionRead an integer from the stream.- Returns:
- the integer value.
- Throws:
OtpErlangDecodeException- if the next term in the stream cannot be represented as an integer.
-
read_ushort
public short read_ushort() throws OtpErlangDecodeExceptionRead an unsigned short from the stream.- Returns:
- the short value.
- Throws:
OtpErlangDecodeException- if the next term in the stream cannot be represented as a positive short.
-
read_short
public short read_short() throws OtpErlangDecodeExceptionRead a short from the stream.- Returns:
- the short value.
- Throws:
OtpErlangDecodeException- if the next term in the stream cannot be represented as a short.
-
read_ulong
public long read_ulong() throws OtpErlangDecodeExceptionRead an unsigned long from the stream.- Returns:
- the long value.
- Throws:
OtpErlangDecodeException- if the next term in the stream cannot be represented as a positive long.
-
read_long
public long read_long() throws OtpErlangDecodeExceptionRead a long from the stream.- Returns:
- the long value.
- Throws:
OtpErlangDecodeException- if the next term in the stream cannot be represented as a long.
-
read_long
public long read_long(boolean unsigned) throws OtpErlangDecodeException- Throws:
OtpErlangDecodeException
-
read_integer_byte_array
public byte[] read_integer_byte_array() throws OtpErlangDecodeExceptionRead an integer from the stream.- Returns:
- the value as a big endian 2's complement byte array.
- Throws:
OtpErlangDecodeException- if the next term in the stream is not an integer.
-
byte_array_to_long
public static long byte_array_to_long(byte[] b, boolean unsigned) throws OtpErlangDecodeException- Throws:
OtpErlangDecodeException
-
read_list_head
public int read_list_head() throws OtpErlangDecodeExceptionRead a list header from the stream.- Returns:
- the arity of the list.
- Throws:
OtpErlangDecodeException- if the next term in the stream is not a list.
-
read_tuple_head
public int read_tuple_head() throws OtpErlangDecodeExceptionRead a tuple header from the stream.- Returns:
- the arity of the tuple.
- Throws:
OtpErlangDecodeException- if the next term in the stream is not a tuple.
-
read_nil
public int read_nil() throws OtpErlangDecodeExceptionRead an empty list from the stream.- Returns:
- zero (the arity of the list).
- Throws:
OtpErlangDecodeException- if the next term in the stream is not an empty list.
-
read_pid
public OtpErlangPid read_pid() throws OtpErlangDecodeException
Read an Erlang PID from the stream.- Returns:
- the value of the PID.
- Throws:
OtpErlangDecodeException- if the next term in the stream is not an Erlang PID.
-
read_port
public OtpErlangPort read_port() throws OtpErlangDecodeException
Read an Erlang port from the stream.- Returns:
- the value of the port.
- Throws:
OtpErlangDecodeException- if the next term in the stream is not an Erlang port.
-
read_ref
public OtpErlangRef read_ref() throws OtpErlangDecodeException
Read an Erlang reference from the stream.- Returns:
- the value of the reference
- Throws:
OtpErlangDecodeException- if the next term in the stream is not an Erlang reference.
-
read_fun
public OtpErlangFun read_fun() throws OtpErlangDecodeException
- Throws:
OtpErlangDecodeException
-
read_external_fun
public OtpErlangExternalFun read_external_fun() throws OtpErlangDecodeException
- Throws:
OtpErlangDecodeException
-
read_string
public java.lang.String read_string() throws OtpErlangDecodeExceptionRead a string from the stream.- Returns:
- the value of the string.
- Throws:
OtpErlangDecodeException- if the next term in the stream is not a string.
-
read_compressed
public OtpErlangObject read_compressed() throws OtpErlangDecodeException
Read a compressed term from the stream- Returns:
- the resulting uncompressed term.
- Throws:
OtpErlangDecodeException- if the next term in the stream is not a compressed term.
-
read_any
public OtpErlangObject read_any() throws OtpErlangDecodeException
Read an arbitrary Erlang term from the stream.- Returns:
- the Erlang term.
- Throws:
OtpErlangDecodeException- if the stream does not contain a known Erlang type at the next position.
-
read_map_head
public int read_map_head() throws OtpErlangDecodeException- Throws:
OtpErlangDecodeException
-
-