This module allows you to read and write to streams, such as files, sockets and pipes.
See the ceylon.io
package for usage examples.
Packages | |
ceylon.io | This package lets you create |
ceylon.io.readers |
Dependencies | ||
ceylon.buffer | 1.3.0 | |
ceylon.collection | 1.3.0 | |
ceylon.file | 1.3.0 | |
ceylon.interop.java | 1.3.0 | |
java.base | 8 | |
java.tls | 8 |
This package lets you create FileDescriptor
objects,
which represent open streams, such as files, sockets, or
pipes. You can read and write bytes from and to those
streams in synchronous or asynchronous mode, using
Buffer objects, and you can
convert bytes to String
objects using
Charset objects.
Here's how you can get a Socket
to a remote host in a
blocking way:
// connect to example.com on port 80 value connector = newSocketConnector(SocketAddress("example.com", 80)); value socket = connector.connect();
Sample usage for reading all the available bytes from a remote socket in a blocking way:
void readResponse(Socket socket) { // create a new decoder from ASCII bytes Decoder decoder = ascii.newDecoder(); // read,decode it all, blocking socket.readFully((ByteBuffer buffer) => decoder.decode(buffer)); // print it all process.write(decoder.done()); }
Here's the same code, but in an asynchronous way, using a
Selector
object to treat read
events:
void readResponseAsync(Socket socket) { // create a new selector for reading from this socket Selector select = newSelector(); // read, decode, print as we get data socket.readAsync(select, byteConsumerToStringConsumer(utf8, (String string) => process.write(string))); // run the event loop select.process(); }
Here's how you could write a request to a Socket
in a
blocking way:
void writeRequest(String data, Socket socket) { // encode it in one go value requestBuffer = ascii.encode(data); // write it all, blocking socket.writeFully(requestBuffer); }
Here's how you would write a request to a Socket
in an
asynchronous way:
void writeRequestAsync(String request, Socket socket) { Selector select = newSelector(); // encode and write as we can socket.writeAsync(select, stringToByteProducer(ascii, request)); // run the event loop select.process(); }
Finally, here's how you can read and write asynchronously to the same socket:
void readAndWriteAsync(String request, Socket socket) { Selector select = newSelector(); // encode and write as we can socket.writeAsync(select, stringToByteProducer(ascii, request)); // read, decode and print as we can socket.readAsync(select, byteConsumerToStringConsumer(utf8, (String string) => process.write(string))); // run the event loop select.process(); }
Values | |
standardError | Source Codeshared WritableFileDescriptor standardError A Aliases: stderr |
standardInput | Source Codeshared ReadableFileDescriptor standardInput A Aliases: stdin |
standardOutput | Source Codeshared WritableFileDescriptor standardOutput A Aliases: stdout |
stderr | See standardError |
stdin | See standardInput |
stdout | See standardOutput |
Functions | |
byteConsumerToStringConsumer | Source Codeshared Anything(ByteBuffer) byteConsumerToStringConsumer(Charset charset, void consumer(String buffer)) Transforms a [consumer] method that accepts Another way to see it is that this method returns a method which accepts
By: Stéphane Épardaud |
newOpenFile | Source Codeshared OpenFile newOpenFile(Resource resource) |
newSelector | Source Codeshared Selector newSelector() |
newServerSocket | Source Codeshared ServerSocket newServerSocket(SocketAddress? address = null, Integer backlog = 0) Instantiates and binds a new server socket. Parameters:
See also ServerSocket |
newSocketConnector | Source Codeshared SocketConnector newSocketConnector(SocketAddress address) Creates a new See also SocketConnector |
newSslSocketConnector | Source Codeshared SslSocketConnector newSslSocketConnector(SocketAddress address) |
stringToByteProducer | Source Codeshared Anything(ByteBuffer) stringToByteProducer(Charset charset, String string) Transforms a Another way to see it is that this method returns a method which accepts
By: Stéphane Épardaud |
Interfaces | |
Closeable | Source Codeshared Closeable |
FileDescriptor | Source Codeshared FileDescriptor Represents anything that you can read/write to, much like the UNIX notion of file descriptor. This supports synchronous and asynchronous reading. |
OpenFile | Source Codeshared OpenFile Represents a file object we can read from and write to. |
ReadableFileDescriptor | Source Codeshared ReadableFileDescriptor Represents anything that you can read from, much like the UNIX notion of file descriptor. |
SelectableFileDescriptor | Source Codeshared SelectableFileDescriptor Represents a |
Selector | Source Codeshared Selector Supports registration of listeners that are notified of events raised directly by the operating system, of the following kinds:
A listener may be registered for:
The listener will then be notified by this selector when
you invoke Every listener can signal the selector object that it is no
longer interested in notifications by returning Listeners should be registered with:
A new instance of |
ServerSocket | Source Codeshared ServerSocket Represents a server socket: a socket open on the current host that accepts incoming connections from other hosts. This supports synchronous and asynchronous modes of operations. The server socket is bound immediately to the specified
address (or to every local network interface if not set),
but it will only accept incoming connections when you
call New server sockets are created with |
Socket | Source Codeshared Socket Represents a network socket. |
SocketConnector | Source Codeshared SocketConnector An object that connects to a remote host, returning a
|
SslSocket | Source Codeshared SslSocket Represents an SSL network socket. |
SslSocketConnector | Source Codeshared SslSocketConnector An object that connects to a remote host, returning an
|
WritableFileDescriptor | Source Codeshared WritableFileDescriptor Represents anything that you can write to, much like the UNIX notion of file descriptor. |
Classes | |
SocketAddress | Source Codeshared SocketAddress Represents an Internet socket address, consisting of an IP address or host name together with a TCP port. |