Interface Pipe
- All Superinterfaces:
AutoCloseable
,ByteChannel
,Channel
,Closeable
,DataInput
,DataOutput
,Flushable
,Link
,ObjectInput
,ObjectOutput
,ReadableByteChannel
,WritableByteChannel
enabled
.
Pipes are only partially thread-safe. Reading and writing is concurrent, but at most one thread can be reading, and at most one thread can be writing.
Pipes are fully buffered, and closing the pipe directly discards any buffered
writes. Closing the OutputStream
will attempt to flush the buffer first, although it
can only be called from the thread which is allowed to perform writes. Closing either stream
has the side effect of also fully closing the pipe.
Here's an example remote method declaration which uses a pipe:
Pipe uploadFile(String name, Pipe pipe) throws RemoteException;
Pipe pipe = server.uploadFile("notes.txt", null);
byte[] notes = ...
pipe.writeInt(notes.length);
pipe.write(notes);
pipe.flush();
pipe.readByte(); // read ack
pipe.recycle();
@Override
public Pipe uploadFile(String name, Pipe pipe) {
byte[] notes = new byte[pipe.readInt()];
pipe.readFully(notes);
pipe.writeByte(1); // ack
pipe.flush();
pipe.recycle();
...
return null;
}
Note: The pipe implementation isn't strictly compatible with DataInput
and
DataOutput
. The contract for those interfaces specifies a modified UTF-8 encoding, but
pipes adhere to the standard UTF-8 format. Also, floating point values are written in their
"raw" form, preserving non-canonical NaN values.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
Support for efficiently reading complex objects from a pipe.static interface
Support for efficiently writing complex objects to a pipe. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Disables tracking of object references.void
Enables tracking of object references as they are written, for correctly serializing object graphs, and to potentially reduce the overall encoding size.Returns the pipe'sInputStream
, which also implementsObjectInput
.Returns the pipe'sOutputStream
, which also implementsObjectOutput
.<T> T
readDecode
(T object, int length, Pipe.Decoder<T> decoder) Read a complex object from the pipe by invoking a decoder, which can be more efficient than reading from the pipe multiple times.Read and return an object.Read and return an object, and if it's aThrowable
instance, a local stack trace is stitched in.void
recycle()
Attempt to recycle the connection instead of closing it.void
skipNBytes
(long n) Skips over and discards exactly n bytes of data from this pipe.void
skipObject
(Consumer<Object> remoteConsumer) Skip an object instead of reading it.long
transferTo
(OutputStream out, long n) Reads up to n bytes from this pipe and writes them into the given stream.<T> void
writeEncode
(T object, int length, Pipe.Encoder<T> encoder) Write a complex object to the pipe by invoking an encoder, which can be more efficient than writing to the pipe multiple times.void
Write a null object reference.void
writeObject
(Object obj) Write an object (or null) to the pipe.Methods inherited from interface java.io.DataInput
readBoolean, readByte, readChar, readDouble, readFloat, readFully, readFully, readInt, readLine, readLong, readShort, readUnsignedByte, readUnsignedShort, readUTF, skipBytes
Methods inherited from interface java.io.DataOutput
writeBoolean, writeByte, writeBytes, writeChar, writeChars, writeDouble, writeFloat, writeInt, writeLong, writeShort, writeUTF
Methods inherited from interface org.cojen.dirmi.Link
localAddress, remoteAddress
Methods inherited from interface java.nio.channels.ReadableByteChannel
read
Methods inherited from interface java.nio.channels.WritableByteChannel
write
-
Method Details
-
enableReferences
void enableReferences()Enables tracking of object references as they are written, for correctly serializing object graphs, and to potentially reduce the overall encoding size. This mode has higher memory overhead because each object flowing through the pipe must be remembered.This method counts the number of times it's invoked, and a matching number of calls to
disableReferences()
is required to fully disable the mode. -
disableReferences
boolean disableReferences()Disables tracking of object references. Memory isn't freed on the remote side until it reads another object.- Returns:
- true if fully disabled
- Throws:
IllegalStateException
- if not currently enabled
-
inputStream
InputStream inputStream()Returns the pipe'sInputStream
, which also implementsObjectInput
. Closing the stream is equivalent to closing the pipe. -
outputStream
OutputStream outputStream()Returns the pipe'sOutputStream
, which also implementsObjectOutput
. Closing the stream is equivalent to closing the pipe. -
recycle
Attempt to recycle the connection instead of closing it. The caller must ensure that the pipe has no pending input or unflushed output.When the
org.cojen.dirmi.Pipe.RECYCLE_CLOSE
system propery is set to true, calling recycle on a client-side pipe closes it instead. This feature is intended to help diagnose issues caused by incorrect pipe recycling.- Throws:
IllegalStateException
- if it's detected that the pipe isn't in a recyclable stateIOException
-
readObject
Read and return an object. Unlike the inherited method, reading from a pipe never throws aClassNotFoundException
.- Specified by:
readObject
in interfaceObjectInput
- Throws:
IOException
-
readThrowable
Read and return an object, and if it's aThrowable
instance, a local stack trace is stitched in.- Throws:
IOException
-
skipNBytes
Skips over and discards exactly n bytes of data from this pipe. If n is less than or equal to zero, then no bytes are skipped.- Throws:
ClosedException
- if the pipe is closed by the remote endpoint before n bytes are skippedIOException
-
skipObject
Skip an object instead of reading it. If references are enabled, the object is still read and remembered, in case it's referenced later.- Parameters:
remoteConsumer
- receives all client-side remote objects, which aren't truly skipped; can pass null to do nothing with them- Throws:
IOException
-
writeObject
Write an object (or null) to the pipe.- Specified by:
writeObject
in interfaceObjectOutput
- Throws:
IOException
-
writeNull
-
transferTo
Reads up to n bytes from this pipe and writes them into the given stream. Fewer than n bytes are written if the pipe has no more input to read.- Returns:
- the number of bytes transferred
- Throws:
IOException
-
readDecode
Read a complex object from the pipe by invoking a decoder, which can be more efficient than reading from the pipe multiple times.- Parameters:
object
- passed directly to the decoderlength
- exact buffer length to pass to the decoderdecoder
- called to perform decoding against a buffer- Throws:
IOException
-
writeEncode
Write a complex object to the pipe by invoking an encoder, which can be more efficient than writing to the pipe multiple times.- Parameters:
object
- passed directly to the encoderlength
- minimum buffer length to pass to the encoderencoder
- called to perform encoding against a buffer- Throws:
IOException
-