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

com.github.isaichkindanila.crypt.lib.util.IOUtils Maven / Gradle / Ivy

The newest version!
package com.github.isaichkindanila.crypt.lib.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Class that handles reading/writing primitives and arrays to streams.
 */
public class IOUtils {
    private IOUtils() {
    }

    /**
     * Writes integer to output stream.
     *
     * @param x   integer
     * @param out output stream
     * @throws IOException if IOException occurs
     * @see #readInt(InputStream)
     */
    public static void writeInt(int x, OutputStream out) throws IOException {
        out.write(ConversionUtils.intToBytes(x));
    }

    /**
     * Reads integer from input stream.
     *
     * @param in input stream
     * @return integer
     * @throws IOException if IOException occurs
     * @see #writeInt(int, OutputStream)
     */
    public static int readInt(InputStream in) throws IOException {
        byte[] intBytes = new byte[4];
        if (in.read(intBytes) != 4) {
            throw new IOException("unexpected InputStream end reached");
        }

        return ConversionUtils.bytesToInt(intBytes, 0);
    }

    /**
     * Writes long to output stream.
     *
     * @param x   long
     * @param out output stream
     * @throws IOException if IOException occurs
     * @see #readLong(InputStream)
     */
    public static void writeLong(long x, OutputStream out) throws IOException {
        out.write(ConversionUtils.longToBytes(x));
    }

    /**
     * Reads long from input stream.
     *
     * @param in input stream
     * @return long
     * @throws IOException if IOException occurs
     * @see #writeLong(long, OutputStream)
     */
    public static long readLong(InputStream in) throws IOException {
        byte[] longBytes = new byte[8];
        if (in.read(longBytes) != 8) {
            throw new IOException("unexpected InputStream end reached");
        }

        return ConversionUtils.bytesToLong(longBytes, 0);
    }

    /**
     * Writes byte array to output stream.
     *
     * @param array byte array
     * @param out   output stream
     * @throws IOException if IOException occurs
     * @see #readByteArray(InputStream)
     */
    public static void writeByteArray(byte[] array, OutputStream out) throws IOException {
        writeInt(array.length, out);
        out.write(array);
    }

    /**
     * Reads byte array from input stream.
     *
     * @param in input stream
     * @return byte array
     * @throws IOException if IOException occurs
     * @see #writeByteArray(byte[], OutputStream)
     */
    public static byte[] readByteArray(InputStream in) throws IOException {
        byte[] array = new byte[readInt(in)];

        if (in.read(array) != array.length) {
            throw new IOException("unexpected InputStream end reached");
        }

        return array;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy