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);
}
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);
}
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());
}
}
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;
}
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));
}
Aggregations