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

uk.autores.handling.ByteHackReader Maven / Gradle / Ivy

There is a newer version: 11.0.35-beta
Show newest version
// Copyright 2023 https://github.com/autores-uk/autores/blob/main/LICENSE.txt
// SPDX-License-Identifier: Apache-2.0
package uk.autores.handling;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

import static java.util.Objects.requireNonNull;

/**
 * Specialized {@link Reader} for putting bytes into char arrays.
 * Each pair of bytes becomes one char.
 * See {@link #lastByteOdd()} for byte streams that are odd numbers
 */
final class ByteHackReader extends Reader {

    private final InputStream in;
    private int oddByte = -1;
    private boolean closed = false;

    ByteHackReader(final InputStream in) {
        this.in = requireNonNull(in);
    }

    @Override
    public int read(char[] cbuf, int off, int len) throws IOException {
        int count = 0;
        for (int i = off, x = off + len; i < x; i++) {
            int r = read();
            if (r < 0) {
                break;
            }
            cbuf[i] = (char) r;
            count++;
        }

        return count == 0 ? -1 : count;
    }

    @Override
    public int read(char[] cbuf) throws IOException {
        return read(cbuf, 0, cbuf.length);
    }

    @Override
    public int read() throws IOException {
        if (closed) {
            throw new IOException("Stream closed");
        }

        int high = in.read();
        if (high < 0) {
            return high;
        }
        int low = in.read();
        if (low < 0) {
            oddByte = high;
            return low;
        }
        return (high << 8) | low;
    }

    /**
     * A char is two bytes.
     * Need to handle odd stream lengths.
     *
     * @return true if the stream had an odd byte
     */
    public boolean lastByteOdd() {
        return oddByte != -1;
    }

    public byte getOddByte() {
        if (!closed) {
            throw new AssertionError();
        }
        return (byte) oddByte;
    }

    @Override
    public void close() throws IOException {
        if (closed) {
            return;
        }
        closed = true;
        in.close();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy