use of java.io.PushbackInputStream in project robovm by robovm.
the class OldPushbackInputStreamTest method test_read$BII.
public void test_read$BII() throws IOException {
PushbackInputStream tobj;
byte[] buf = ("01234567890123456789").getBytes();
tobj = new PushbackInputStream(underlying);
tobj.read(buf, 6, 5);
assertEquals("Wrong value read!", "BEGIN", new String(buf, 6, 5));
assertEquals("Too much read!", "012345BEGIN123456789", new String(buf));
underlying.throwExceptionOnNextUse = true;
try {
tobj.read(buf, 6, 5);
fail("IOException not thrown.");
} catch (IOException e) {
// expected
}
}
use of java.io.PushbackInputStream in project XobotOS by xamarin.
the class JDKX509CertificateFactory method engineGenerateCertificate.
/**
* Generates a certificate object and initializes it with the data
* read from the input stream inStream.
*/
public Certificate engineGenerateCertificate(InputStream in) throws CertificateException {
if (currentStream == null) {
currentStream = in;
sData = null;
sDataObjectCount = 0;
} else if (// reset if input stream has changed
currentStream != in) {
currentStream = in;
sData = null;
sDataObjectCount = 0;
}
try {
if (sData != null) {
if (sDataObjectCount != sData.size()) {
return getCertificate();
} else {
sData = null;
sDataObjectCount = 0;
return null;
}
}
int limit = ProviderUtil.getReadLimit(in);
PushbackInputStream pis = new PushbackInputStream(in);
int tag = pis.read();
if (tag == -1) {
return null;
}
pis.unread(tag);
if (// assume ascii PEM encoded.
tag != 0x30) {
return readPEMCertificate(pis);
} else {
return readDERCertificate(new ASN1InputStream(pis, limit));
}
} catch (Exception e) {
throw new CertificateException(e);
}
}
use of java.io.PushbackInputStream in project asterixdb by apache.
the class NonSyncDataInputBuffer method readLine.
/**
* Answers a <code>String</code> representing the next line of text available
* in this BufferedReader. A line is represented by 0 or more characters
* followed by <code>'\n'</code>, <code>'\r'</code>, <code>"\n\r"</code> or
* end of stream. The <code>String</code> does not include the newline
* sequence.
*
* @return the contents of the line or null if no characters were read before
* end of stream.
*
* @throws IOException
* If the DataInputStream is already closed or some other IO error
* occurs.
*
* @deprecated Use BufferedReader
*/
@Deprecated
@Override
public final String readLine() throws IOException {
// Typical line length
StringBuilder line = new StringBuilder(80);
boolean foundTerminator = false;
while (true) {
int nextByte = in.read();
switch(nextByte) {
case -1:
if (line.length() == 0 && !foundTerminator) {
return null;
}
return line.toString();
case (byte) '\r':
if (foundTerminator) {
((PushbackInputStream) in).unread(nextByte);
return line.toString();
}
foundTerminator = true;
/* Have to be able to peek ahead one byte */
if (!(in.getClass() == PushbackInputStream.class)) {
in = new PushbackInputStream(in);
}
break;
case (byte) '\n':
return line.toString();
default:
if (foundTerminator) {
((PushbackInputStream) in).unread(nextByte);
return line.toString();
}
line.append((char) nextByte);
}
}
}
use of java.io.PushbackInputStream in project poi by apache.
the class TestDetectAsOOXML method testFileCorruption.
public void testFileCorruption() throws Exception {
// create test InputStream
byte[] testData = { (byte) 1, (byte) 2, (byte) 3 };
ByteArrayInputStream testInput = new ByteArrayInputStream(testData);
// detect header
InputStream in = new PushbackInputStream(testInput, 10);
assertFalse(DocumentFactoryHelper.hasOOXMLHeader(in));
//noinspection deprecation
assertFalse(POIXMLDocument.hasOOXMLHeader(in));
// check if InputStream is still intact
byte[] test = new byte[3];
assertEquals(3, in.read(test));
assertTrue(Arrays.equals(testData, test));
assertEquals(-1, in.read());
}
use of java.io.PushbackInputStream in project poi by apache.
the class IOUtils method peekFirstNBytes.
/**
* Peeks at the first N bytes of the stream. Returns those bytes, but
* with the stream unaffected. Requires a stream that supports mark/reset,
* or a PushbackInputStream. If the stream has >0 but <N bytes,
* remaining bytes will be zero.
* @throws EmptyFileException if the stream is empty
*/
public static byte[] peekFirstNBytes(InputStream stream, int limit) throws IOException, EmptyFileException {
stream.mark(limit);
ByteArrayOutputStream bos = new ByteArrayOutputStream(limit);
copy(new BoundedInputStream(stream, limit), bos);
int readBytes = bos.size();
if (readBytes == 0) {
throw new EmptyFileException();
}
if (readBytes < limit) {
bos.write(new byte[limit - readBytes]);
}
byte[] peekedBytes = bos.toByteArray();
if (stream instanceof PushbackInputStream) {
PushbackInputStream pin = (PushbackInputStream) stream;
pin.unread(peekedBytes, 0, readBytes);
} else {
stream.reset();
}
return peekedBytes;
}
Aggregations