Streams
Overview
Module
- package:
org.eclipse.dirigible.sdk.io - source: io/Streams.java
Stream-shaped I/O — read text / bytes from an InputStream, write to an OutputStream, copy between them, build in-memory buffers via ByteArrayInputStream / ByteArrayOutputStream. Useful when wiring together sources and sinks supplied by different APIs (a multipart upload feeding a ZIP entry, the platform repository feeding an HTTP response).
copyLarge(InputStream, OutputStream) should be preferred over plain copy(InputStream, OutputStream) for files larger than a few MB — it uses a bigger internal buffer and reports a long byte count via the underlying facade.
Key Features:
- Read primitives:
read,readBytes,readTextover anyInputStream. - Write primitives:
writea single byte,writeBytes(string-encoded),writeTextover anyOutputStream. - Copying:
copyfor small payloads,copyLargefor multi-MB streams. - In-memory buffers: Factory methods for
ByteArrayInputStream/ByteArrayOutputStream. - Resource access: Load a classpath resource as a
ByteArrayInputStream.
Example Usage:
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.eclipse.dirigible.sdk.io.Streams;
try (InputStream in = Streams.getResourceAsByteArrayInputStream("/img/logo.png")) {
ByteArrayOutputStream out = Streams.createByteArrayOutputStream();
Streams.copy(in, out);
byte[] data = Streams.getBytes(out);
}Methods
read()
Reads a single byte from the stream and returns it as an int, or -1 on end-of-stream.
javapublic static int read(InputStream input) throws IOException;
Parameter Type Description inputInputStreamThe stream to read from. Returns
- Type:
int- Description: The byte read (0–255), or
-1at end-of-stream.
readBytes()
Reads all remaining bytes from the stream into a byte[].
javapublic static byte[] readBytes(InputStream input) throws IOException;
Parameter Type Description inputInputStreamThe stream to drain. Returns
- Type:
byte[]- Description: All bytes read from the stream.
readText()
Reads the stream contents fully as text using the platform default encoding.
javapublic static String readText(InputStream input) throws IOException;
Parameter Type Description inputInputStreamThe stream to read. Returns
- Type:
String- Description: The decoded text content.
close()
Closes the given stream. Overloads cover both InputStream and OutputStream.
javapublic static void close(InputStream input) throws IOException; public static void close(OutputStream output) throws IOException;
Parameter Type Description inputInputStreamThe input stream to close. outputOutputStreamThe output stream to close. Returns
- Type:
void
write()
Writes a single byte (low 8 bits of value) to the output stream.
javapublic static void write(OutputStream output, int value) throws IOException;
Parameter Type Description outputOutputStreamThe output stream. valueintThe byte value to write (low 8 bits used). Returns
- Type:
void
writeBytes()
Writes the bytes encoded by the given string input to the output stream.
javapublic static void writeBytes(OutputStream output, String input) throws IOException;
Parameter Type Description outputOutputStreamThe output stream. inputStringThe string-encoded byte input. Returns
- Type:
void
writeText()
Writes a string of text to the output stream using the platform default encoding.
javapublic static void writeText(OutputStream output, String value) throws IOException;
Parameter Type Description outputOutputStreamThe output stream. valueStringThe text to write. Returns
- Type:
void
copy()
Copies all bytes from the input stream to the output stream. Suitable for small payloads — prefer copyLarge for multi-MB transfers.
javapublic static void copy(InputStream input, OutputStream output) throws IOException;
Parameter Type Description inputInputStreamThe source stream. outputOutputStreamThe sink stream. Returns
- Type:
void
copyLarge()
Copies all bytes from the input stream to the output stream using a larger internal buffer. Preferred for files above a few MB.
javapublic static void copyLarge(InputStream input, OutputStream output) throws IOException;
Parameter Type Description inputInputStreamThe source stream. outputOutputStreamThe sink stream. Returns
- Type:
void
createByteArrayInputStream()
Creates a new in-memory ByteArrayInputStream. Overloads cover an explicit byte[], a String-encoded buffer, and an empty stream.
javapublic static ByteArrayInputStream createByteArrayInputStream(byte[] input) throws IOException; public static ByteArrayInputStream createByteArrayInputStream(String input) throws IOException; public static ByteArrayInputStream createByteArrayInputStream() throws IOException;
Parameter Type Description inputbyte[]The bytes to wrap. Optional. inputStringThe string-encoded bytes to wrap. Optional. Returns
- Type:
ByteArrayInputStream- Description: A new in-memory input stream.
createByteArrayOutputStream()
Creates a new in-memory ByteArrayOutputStream to accumulate bytes.
javapublic static ByteArrayOutputStream createByteArrayOutputStream() throws IOException;Returns
- Type:
ByteArrayOutputStream- Description: A new in-memory output stream.
getBytes()
Returns the bytes accumulated in a ByteArrayOutputStream.
javapublic static byte[] getBytes(ByteArrayOutputStream output) throws IOException;
Parameter Type Description outputByteArrayOutputStreamThe buffer to drain. Returns
- Type:
byte[]- Description: The accumulated bytes.
getText()
Returns the accumulated bytes of a ByteArrayOutputStream decoded as text using the platform default encoding.
javapublic static String getText(ByteArrayOutputStream output) throws IOException;
Parameter Type Description outputByteArrayOutputStreamThe buffer to drain. Returns
- Type:
String- Description: The accumulated bytes decoded as text.
getResourceAsByteArrayInputStream()
Loads a classpath resource at the given path and returns its contents as a ByteArrayInputStream.
javapublic static ByteArrayInputStream getResourceAsByteArrayInputStream(String path) throws IOException;
Parameter Type Description pathStringThe classpath resource path. Returns
- Type:
ByteArrayInputStream- Description: A stream over the resource contents.