All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.rx.io.IOStream Maven / Gradle / Ivy

package org.rx.io;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.SneakyThrows;
import org.rx.core.App;
import org.rx.core.Disposable;
import org.rx.annotation.ErrorCode;
import org.rx.core.SystemException;
import org.rx.core.Contract;
import org.rx.core.StringBuilder;

import java.io.*;

import static org.rx.core.Contract.*;

@AllArgsConstructor
@Getter
public class IOStream extends Disposable implements Closeable, Flushable {
    public static String readString(InputStream stream) {
        return readString(stream, Contract.Utf8);
    }

    @SneakyThrows
    public static String readString(InputStream stream, String charset) {
        require(stream, charset);

        StringBuilder result = new StringBuilder();
        try (DataInputStream reader = new DataInputStream(stream)) {
            byte[] buffer = new byte[App.Config.getBufferSize()];
            int read;
            while ((read = reader.read(buffer)) > 0) {
                result.append(new String(buffer, 0, read, charset));
            }
        }
        return result.toString();
    }

    public static void writeString(OutputStream stream, String value) {
        writeString(stream, value, Contract.Utf8);
    }

    @SneakyThrows
    public static void writeString(OutputStream stream, String value, String charset) {
        require(stream, charset);

        try (DataOutputStream writer = new DataOutputStream(stream)) {
            byte[] data = value.getBytes(charset);
            writer.write(data);
        }
    }

    @SneakyThrows
    public static void copyTo(InputStream from, OutputStream to) {
        require(from, to);

        byte[] buffer = new byte[App.Config.getBufferSize() * 2];
        int read;
        while ((read = from.read(buffer, 0, buffer.length)) > 0) {
            to.write(buffer, 0, read);
            to.flush();
        }
    }

    protected TI reader;
    protected TO writer;

    public boolean canRead() {
        return !isClosed() && available() > 0;
    }

    public boolean canWrite() {
        return !isClosed();
    }

    public boolean canSeek() {
        return false;
    }

    @ErrorCode
    public int getPosition() {
        throw new SystemException(values());
    }

    @ErrorCode
    public void setPosition(int position) {
        throw new SystemException(values());
    }

    @ErrorCode(messageKeys = {"$type"})
    public int getLength() {
        throw new SystemException(values(this.getClass().getSimpleName()));
    }

    @SneakyThrows
    @Override
    protected void freeObjects() {
        catchCall(this::flush);

        writer.close();
        reader.close();
    }

    @SneakyThrows
    public int available() {
        checkNotClosed();

        return reader.available();
    }

    @SneakyThrows
    public int read() {
        checkNotClosed();

        return reader.read();
    }

    public int read(byte[] data) {
        checkNotClosed();
        require(data);

        return read(data, 0, data.length);
    }

    @SneakyThrows
    public int read(byte[] buffer, int offset, int count) {
        checkNotClosed();
        require(buffer);
        require(offset, offset >= 0);//ignore count 4 BytesSegment

        return reader.read(buffer, offset, count);
    }

    @SneakyThrows
    public void write(int b) {
        checkNotClosed();

        writer.write(b);
    }

    public void write(byte[] data) {
        checkNotClosed();
        require(data);

        write(data, 0, data.length);
    }

    @SneakyThrows
    public void write(byte[] buffer, int offset, int count) {
        checkNotClosed();
        require(buffer);
        require(offset, offset >= 0);

        writer.write(buffer, offset, count);
    }

    @SneakyThrows
    @Override
    public void flush() {
        checkNotClosed();

        writer.flush();
    }

    public void copyTo(IOStream to) {
        checkNotClosed();
        require(to);

        copyTo(this.reader, to.writer);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy