Class StreamOutput
- java.lang.Object
-
- java.io.OutputStream
-
- cn.sliew.milky.common.io.stream.StreamOutput
-
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
- Direct Known Subclasses:
OutputStreamStreamOutput
public abstract class StreamOutput extends OutputStream
A stream from another node to this node. Technically, it can also be streamed from a byte array but that is mostly for testing.This class's methods are optimized so you can put the methods that read and write a class next to each other and you can scan them visually for differences. That means that most variables should be read and written in a single line so even large objects fit both reading and writing on the screen. It also means that the methods on this class are named very similarly to
StreamInput. Finally it means that the "barrier to entry" for adding new methods to this class is relatively low even though it is a shared class with code everywhere. That being said, this class deals primarily withLists rather than Arrays. For the most part calls should adapt to lists, either by storingLists internally or just converting to and from aListwhen calling. This comment is repeated onStreamInput.
-
-
Constructor Summary
Constructors Constructor Description StreamOutput()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract voidclose()Closes this stream to further operations.abstract voidflush()Forces any buffered output to be written.abstract voidreset()voidwrite(byte[] b, int off, int len)voidwrite(int b)voidwriteBoolean(boolean b)abstract voidwriteByte(byte b)Writes a single byte.voidwriteBytes(byte[] b)Writes an array of bytes.voidwriteBytes(byte[] b, int length)Writes an array of bytes.abstract voidwriteBytes(byte[] b, int offset, int length)Writes an array of bytes.voidwriteDouble(double v)voidwriteFloat(float v)voidwriteGenericValue(Object value)Notice: when serialization a map, the stream out map with the stream in map maybe have the different key-value orders, they will maybe have different stream order.voidwriteInt(int i)Writes an int as four bytes.voidwriteLong(long i)Writes a long as eight bytes.voidwriteOptionalBoolean(Boolean b)voidwriteOptionalDouble(Double v)voidwriteOptionalFloat(Float floatValue)voidwriteOptionalInt(Integer integer)Writes an optionalInteger.voidwriteOptionalLong(Long l)voidwriteOptionalVInt(Integer integer)voidwriteOptionalVLong(Long l)voidwriteShort(short v)voidwriteVInt(int i)Writes an int in a variable-length format.voidwriteVLong(long i)Writes a non-negative long in a variable-length format.voidwriteZLong(long i)Writes a long in a variable-length format.-
Methods inherited from class java.io.OutputStream
nullOutputStream, write
-
-
-
-
Method Detail
-
close
public abstract void close() throws IOExceptionCloses this stream to further operations.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException
-
flush
public abstract void flush() throws IOExceptionForces any buffered output to be written.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException
-
reset
public abstract void reset() throws IOException- Throws:
IOException
-
writeByte
public abstract void writeByte(byte b) throws IOExceptionWrites a single byte.- Throws:
IOException
-
writeBytes
public void writeBytes(byte[] b) throws IOExceptionWrites an array of bytes.- Parameters:
b- the bytes to write- Throws:
IOException
-
writeBytes
public void writeBytes(byte[] b, int length) throws IOExceptionWrites an array of bytes.- Parameters:
b- the bytes to writelength- the number of bytes to write- Throws:
IOException
-
writeBytes
public abstract void writeBytes(byte[] b, int offset, int length) throws IOExceptionWrites an array of bytes.- Parameters:
b- the bytes to writeoffset- the offset in the byte arraylength- the number of bytes to write- Throws:
IOException
-
writeShort
public final void writeShort(short v) throws IOException- Throws:
IOException
-
writeInt
public void writeInt(int i) throws IOExceptionWrites an int as four bytes.- Throws:
IOException
-
writeOptionalInt
public void writeOptionalInt(Integer integer) throws IOException
Writes an optionalInteger.- Throws:
IOException
-
writeVInt
public void writeVInt(int i) throws IOExceptionWrites an int in a variable-length format. Writes between one and five bytes. Smaller values take fewer bytes. Negative numbers will always use all 5 bytes and are therefore better serialized usingwriteInt(int)- Throws:
IOException
-
writeOptionalVInt
public void writeOptionalVInt(Integer integer) throws IOException
- Throws:
IOException
-
writeLong
public void writeLong(long i) throws IOExceptionWrites a long as eight bytes.- Throws:
IOException
-
writeOptionalLong
public void writeOptionalLong(Long l) throws IOException
- Throws:
IOException
-
writeVLong
public void writeVLong(long i) throws IOExceptionWrites a non-negative long in a variable-length format. Writes between one and ten bytes. Smaller values take fewer bytes. Negative numbers use ten bytes and trip assertions (if running in tests) so preferwriteLong(long)orwriteZLong(long)for negative numbers.- Throws:
IOException
-
writeOptionalVLong
public void writeOptionalVLong(Long l) throws IOException
- Throws:
IOException
-
writeZLong
public void writeZLong(long i) throws IOExceptionWrites a long in a variable-length format. Writes between one and ten bytes. Values are remapped by sliding the sign bit into the lsb and then encoded as an unsigned number e.g., 0 -;> 0, -1 -;> 1, 1 -;> 2, ..., Long.MIN_VALUE -;> -1, Long.MAX_VALUE -;> -2 Numbers with small absolute value will have a small encoding If the numbers are known to be non-negative, usewriteVLong(long)- Throws:
IOException
-
writeFloat
public void writeFloat(float v) throws IOException- Throws:
IOException
-
writeOptionalFloat
public void writeOptionalFloat(Float floatValue) throws IOException
- Throws:
IOException
-
writeDouble
public void writeDouble(double v) throws IOException- Throws:
IOException
-
writeOptionalDouble
public void writeOptionalDouble(Double v) throws IOException
- Throws:
IOException
-
writeBoolean
public void writeBoolean(boolean b) throws IOException- Throws:
IOException
-
writeOptionalBoolean
public void writeOptionalBoolean(Boolean b) throws IOException
- Throws:
IOException
-
write
public void write(int b) throws IOException- Specified by:
writein classOutputStream- Throws:
IOException
-
write
public void write(byte[] b, int off, int len) throws IOException- Overrides:
writein classOutputStream- Throws:
IOException
-
writeGenericValue
public void writeGenericValue(Object value) throws IOException
Notice: when serialization a map, the stream out map with the stream in map maybe have the different key-value orders, they will maybe have different stream order. If want to keep stream out map and stream in map have the same stream order when stream, can usewriteMapWithConsistentOrder- Throws:
IOException
-
-