Example usage for org.bouncycastle.util.io Streams readFully

List of usage examples for org.bouncycastle.util.io Streams readFully

Introduction

In this page you can find the example usage for org.bouncycastle.util.io Streams readFully.

Prototype

public static int readFully(InputStream inStr, byte[] buf) throws IOException 

Source Link

Document

Fully read in buf's length in data, or up to EOF, whichever occurs first,

Usage

From source file:de.afarber.tlspskserver.TlsPSKProtocolTest.java

License:Open Source License

public void testClientServer() throws Exception {
    SecureRandom secureRandom = new SecureRandom();

    PipedInputStream clientRead = new PipedInputStream();
    PipedInputStream serverRead = new PipedInputStream();
    PipedOutputStream clientWrite = new PipedOutputStream(serverRead);
    PipedOutputStream serverWrite = new PipedOutputStream(clientRead);

    TlsClientProtocol clientProtocol = new TlsClientProtocol(clientRead, clientWrite, secureRandom);
    TlsServerProtocol serverProtocol = new TlsServerProtocol(serverRead, serverWrite, secureRandom);

    ServerThread serverThread = new ServerThread(serverProtocol);
    serverThread.start();//from  w  w  w.ja v  a 2s.co  m

    MockPSKTlsClient client = new MockPSKTlsClient(null);
    clientProtocol.connect(client);

    // NOTE: Because we write-all before we read-any, this length can't be more than the pipe capacity
    int length = 1000;

    byte[] data = new byte[length];
    secureRandom.nextBytes(data);

    try (OutputStream output = clientProtocol.getOutputStream()) {
        output.write(data);
        byte[] echo = new byte[data.length];
        int count = Streams.readFully(clientProtocol.getInputStream(), echo);
        System.out.println("count: " + count);
        System.out.println("data.length: " + data.length);
        //assertTrue(Arrays.areEqual(data, echo));
    }

    serverThread.join();
}

From source file:org.candlepin.util.DERUtil.java

License:Open Source License

/**
 * Read exactly the number of bytes equal to the length of the bytes parameter and
 * increase the counter accordingly.//from   w w  w  .ja va  2s. c o m
 *
 * @param s the stream to read from
 * @param bytes the byte array to fill
 * @param count the counter to modify.  Can be null.
 * @throws IOException if the stream cannot provide the number of required bytes
 */
public static void readFullyAndTrack(InputStream s, byte[] bytes, AtomicInteger count) throws IOException {
    if (Streams.readFully(s, bytes) != bytes.length) {
        throw new EOFException("EOF encountered in middle of object");
    }

    if (count != null) {
        count.addAndGet(bytes.length);
    }
}

From source file:org.candlepin.util.X509CRLEntryStreamTest.java

License:Open Source License

@Test
public void testPemReadThroughBase64Stream() throws Exception {
    /* NB: Base64InputStream only takes base64.  The "-----BEGIN X509 CRL-----" and
     * corresponding footer must be removed.  Luckily in Base64InputStream stops the
     * minute it sees a padding character and our test file has some padding.  Thus,
     * we don't need to worry about removing the footer.  If the Base64 file didn't
     * require padding, I'm not sure what happens so the footer should be removed
     * somehow for real uses *///from w  w  w  .ja v  a 2  s  .  c  o  m

    InputStream referenceStream = new BufferedInputStream(new FileInputStream(pemFile));
    byte[] header = "-----BEGIN X509 CRL-----".getBytes("ASCII");
    Streams.readFully(referenceStream, header);

    referenceStream = new Base64InputStream(referenceStream);
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509CRL referenceCrl = (X509CRL) cf.generateCRL(referenceStream);

    Set<BigInteger> referenceSerials = new HashSet<BigInteger>();

    for (X509CRLEntry entry : referenceCrl.getRevokedCertificates()) {
        referenceSerials.add(entry.getSerialNumber());
    }

    X509CRLEntryStream stream = new X509CRLEntryStream(derFile);
    try {
        Set<BigInteger> streamedSerials = new HashSet<BigInteger>();
        while (stream.hasNext()) {
            streamedSerials.add(stream.next().getSerialNumber());
        }

        assertEquals(referenceSerials, streamedSerials);
    } finally {
        referenceStream.close();
        stream.close();
    }
}