Example usage for com.google.common.io ByteStreams readFully

List of usage examples for com.google.common.io ByteStreams readFully

Introduction

In this page you can find the example usage for com.google.common.io ByteStreams readFully.

Prototype

public static void readFully(InputStream in, byte[] b) throws IOException 

Source Link

Document

Attempts to read enough bytes from the stream to fill the given byte array, with the same behavior as DataInput#readFully(byte[]) .

Usage

From source file:org.jpmml.sklearn.CompressedInputStreamStorage.java

static private InputStream init(PushbackInputStream is) throws IOException {
    byte[] magic = new byte[2];

    ByteStreams.readFully(is, magic);

    is.unread(magic);/*ww  w.  ja va 2  s .c o m*/

    // Joblib 0.10.0 and newer
    if (magic[0] == 'x') {
        return initZlib(is);
    } else

    // Joblib 0.9.4 and earlier
    if (magic[0] == 'Z' && magic[1] == 'F') {
        return initCompat(is);
    } else

    {
        throw new IOException();
    }
}

From source file:org.jpmml.sklearn.CompressedInputStreamStorage.java

static private InputStream initCompat(PushbackInputStream is) throws IOException {
    byte[] headerBytes = new byte[2 + 19];

    ByteStreams.readFully(is, headerBytes);

    String header = new String(headerBytes);

    if (!header.startsWith("ZF0x")) {
        throw new IOException();
    }//  w  w w . j a v  a2 s . c om

    // Remove trailing whitespace
    header = header.trim();

    final long expectedSize = Long.parseLong(header.substring(4), 16);

    // Consume the first byte
    int firstByte = is.read();
    if (firstByte < 0) {
        return is;
    } // End if

    // If the first byte is not a space character, then make it available for reading again
    if (firstByte != '\u0020') {
        is.unread(firstByte);
    }

    InflaterInputStream zlibIs = new InflaterInputStream(is);

    InputStream result = new FilterInputStream(new CountingInputStream(zlibIs)) {

        private boolean closed = false;

        @Override
        public void close() throws IOException {

            if (this.closed) {
                return;
            }

            this.closed = true;

            long size = ((CountingInputStream) super.in).getCount();

            super.close();

            if (size != expectedSize) {
                throw new IOException(
                        "Expected " + expectedSize + " bytes of uncompressed data, got " + size + " bytes");
            }
        }
    };

    return result;
}

From source file:org.jpmml.xgboost.XGBoostDataInput.java

static private InputStream init(PushbackInputStream is) throws IOException {
    byte[] header = new byte[4];

    ByteStreams.readFully(is, header);

    if (!Arrays.equals(XGBoostDataInput.BINF_MAGIC, header)) {
        is.unread(header);/*from  w  w  w.  j a  v  a 2  s  .  com*/
    }

    return is;
}

From source file:com.turn.ttorrent.protocol.bcodec.StreamBDecoder.java

@Override
protected byte[] readBytes(int length) throws IOException {
    byte[] bytes = new byte[length];
    ByteStreams.readFully(in, bytes);
    return bytes;
}

From source file:uk.co.danielrendall.metaphor.parsers.FUTUREParser.java

@Override
protected FUTURE doParse(PushbackInputStream in) throws ParseException {
    int length = readUnsignedInt(in);
    byte[] data = new byte[length];
    try {/*from  w  ww  . ja v  a  2 s.  c  om*/
        ByteStreams.readFully(in, data);
    } catch (IOException e) {
        throw new ParseException("failed to read " + length + " bytes for FUTURE", e);
    }
    return new FUTURE(type, data);
}

From source file:org.b1.pack.standard.reader.RecordHeader.java

private static String readText(InputStream stream) throws IOException {
    Long size = Numbers.readLong(stream);
    if (size == null) {
        return null;
    }//from  w  ww . jav a2  s  . co m
    byte[] bytes = new byte[Ints.checkedCast(size)];
    ByteStreams.readFully(stream, bytes);
    return new String(bytes, Charsets.UTF_8.name());
}

From source file:org.gaul.s3proxy.ChunkedInputStream.java

@Override
public int read() throws IOException {
    while (currentIndex == currentLength) {
        String line = readLine(in);
        if (line.equals("")) {
            return -1;
        }//from www .j av a2 s.com
        String[] parts = line.split(";", 2);
        currentLength = Integer.parseInt(parts[0], 16);
        currentSignature = parts[1];
        chunk = new byte[currentLength];
        currentIndex = 0;
        ByteStreams.readFully(in, chunk);
        // TODO: check currentSignature
        if (currentLength == 0) {
            return -1;
        }
        readLine(in);
    }
    return chunk[currentIndex++] & 0xFF;
}

From source file:org.jpmml.xgboost.XGBoostDataInput.java

public String readString() throws IOException {
    int length = (int) this.dis.readLong();

    byte[] buffer = new byte[length];

    ByteStreams.readFully(this.dis, buffer);

    return new String(buffer);
}

From source file:com.complexible.common.io.ByteStreams2.java

public static int readInt(final InputStream theStream) throws IOException {
    byte[] array = new byte[Ints.BYTES];
    ByteStreams.readFully(theStream, array);
    return Ints.fromByteArray(array);
}

From source file:org.apache.druid.common.utils.SerializerUtils.java

String readString(InputStream in) throws IOException {
    final int length = readInt(in);
    byte[] stringBytes = new byte[length];
    ByteStreams.readFully(in, stringBytes);
    return StringUtils.fromUtf8(stringBytes);
}