Search in sources :

Example 86 with PushbackInputStream

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
    }
}
Also used : PushbackInputStream(java.io.PushbackInputStream) IOException(java.io.IOException)

Example 87 with PushbackInputStream

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);
    }
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) PushbackInputStream(java.io.PushbackInputStream) CertificateException(java.security.cert.CertificateException) CertificateParsingException(java.security.cert.CertificateParsingException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) CRLException(java.security.cert.CRLException)

Example 88 with PushbackInputStream

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);
        }
    }
}
Also used : PushbackInputStream(java.io.PushbackInputStream)

Example 89 with PushbackInputStream

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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream)

Example 90 with PushbackInputStream

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 &gt;0 but &lt;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;
}
Also used : PushbackInputStream(java.io.PushbackInputStream) EmptyFileException(org.apache.poi.EmptyFileException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

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