Search in sources :

Example 51 with PushbackInputStream

use of java.io.PushbackInputStream in project neo4j by neo4j.

the class Readables method wrap.

/**
 * Wraps a {@link InputStream} in a {@link CharReadable}.
 *
 * @param stream {@link Reader} to wrap.
 * @param sourceName name or description of the source of the stream.
 * @param charset {@link Charset} to use for reading.
 * @param length total number of bytes provided by the reader.
 * @return a {@link CharReadable} for the {@link Reader}.
 * @throws IOException on I/O error.
 */
public static CharReadable wrap(final InputStream stream, final String sourceName, Charset charset, long length) throws IOException {
    byte[] bytes = new byte[Magic.longest()];
    PushbackInputStream pushbackStream = new PushbackInputStream(stream, bytes.length);
    Charset usedCharset = charset;
    int read = stream.read(bytes);
    if (read >= 0) {
        bytes = read < bytes.length ? Arrays.copyOf(bytes, read) : bytes;
        Magic magic = Magic.of(bytes);
        int excessiveBytes = read;
        if (magic.impliesEncoding()) {
            // Unread the diff between the BOM and the longest magic we gathered bytes for
            excessiveBytes -= magic.length();
            usedCharset = magic.encoding();
        }
        pushbackStream.unread(bytes, read - excessiveBytes, excessiveBytes);
    }
    return wrap(new InputStreamReader(pushbackStream, usedCharset) {

        @Override
        public String toString() {
            return sourceName;
        }
    }, length);
}
Also used : InputStreamReader(java.io.InputStreamReader) PushbackInputStream(java.io.PushbackInputStream) Charset(java.nio.charset.Charset)

Example 52 with PushbackInputStream

use of java.io.PushbackInputStream in project commons by twitter.

the class CompatibilityCodec method deserialize.

@Override
public T deserialize(InputStream source) throws IOException {
    final PushbackInputStream in = new PushbackInputStream(source, prefixLength);
    final byte[] prefix = readAtMostPrefix(in);
    in.unread(prefix);
    return (discriminator.apply(prefix) ? primaryCodec : secondaryCodec).deserialize(in);
}
Also used : PushbackInputStream(java.io.PushbackInputStream)

Example 53 with PushbackInputStream

use of java.io.PushbackInputStream in project beam by apache.

the class AvroSourceTest method testAdvancePastNextSyncMarkerAt.

/**
 * Asserts that advancePastNextSyncMarker advances an input stream past a sync marker and
 * correctly returns the number of bytes consumed from the stream. Creates a haystack of size
 * bytes and places a 16-byte sync marker at the position specified.
 */
private void testAdvancePastNextSyncMarkerAt(int position, int size) throws IOException {
    byte sentinel = (byte) 0xFF;
    byte[] marker = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
    byte[] haystack = createHaystack(marker, position, size);
    PushbackInputStream stream = new PushbackInputStream(new ByteArrayInputStream(haystack), marker.length);
    if (position + marker.length < size) {
        haystack[position + marker.length] = sentinel;
        assertEquals(position + marker.length, AvroReader.advancePastNextSyncMarker(stream, marker));
        assertEquals(sentinel, (byte) stream.read());
    } else {
        assertEquals(size, AvroReader.advancePastNextSyncMarker(stream, marker));
        assertEquals(-1, stream.read());
    }
}
Also used : PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 54 with PushbackInputStream

use of java.io.PushbackInputStream in project OpenRefine by OpenRefine.

the class XmlImporter method wrapPrefixRemovingInputStream.

private static final InputStream wrapPrefixRemovingInputStream(InputStream inputStream) throws XMLStreamException, IOException {
    PushbackInputStream pis = new PushbackInputStream(inputStream);
    int b;
    int count = 0;
    while (count < 100 && (b = pis.read()) >= 0) {
        if (++count > 100) {
            throw new XMLStreamException("File starts with too much non-XML content to skip over");
        } else if (b == '<') {
            pis.unread(b);
            break;
        }
    }
    return pis;
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) PushbackInputStream(java.io.PushbackInputStream)

Example 55 with PushbackInputStream

use of java.io.PushbackInputStream in project j2objc by google.

the class FilterInputStreamNullSourceTest method testPushbackInputStream.

public void testPushbackInputStream() throws IOException {
    assertReadsFailWithIoException(new PushbackInputStream(null));
    assertReadsFailWithIoException(new PushbackInputStream(null, 1024));
}
Also used : PushbackInputStream(java.io.PushbackInputStream)

Aggregations

PushbackInputStream (java.io.PushbackInputStream)130 IOException (java.io.IOException)61 InputStream (java.io.InputStream)34 ByteArrayInputStream (java.io.ByteArrayInputStream)21 FileInputStream (java.io.FileInputStream)10 GZIPInputStream (java.util.zip.GZIPInputStream)10 InputStreamReader (java.io.InputStreamReader)8 File (java.io.File)5 CertificateException (java.security.cert.CertificateException)5 Test (org.junit.Test)5 OutputStream (java.io.OutputStream)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 CRLException (java.security.cert.CRLException)4 CertificateParsingException (java.security.cert.CertificateParsingException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 BufferedInputStream (java.io.BufferedInputStream)3 BufferedReader (java.io.BufferedReader)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Charset (java.nio.charset.Charset)3