/** * Reads all the data from the given file. * * @param file File to read. * * @return Byte array of data read from file. * * @throws StreamException on stream IO errors. */ public static byte[] readAll(final File file) throws StreamException { final InputStream input = makeStream(file); try { return readAll(input, (int) file.length()); } finally { closeStream(input); } }
/** * Reads all data from the given reader. * * @param reader Reader over character data. * * @return Data read from reader. * * @throws StreamException on stream IO errors. */ public static String readAll(final Reader reader) throws StreamException { return readAll(reader, 1024); }
/** * Reads all data from the given reader. * * @param reader Reader over character data. * @param sizeHint Estimate of amount of data to be read in number of characters. * * @return Data read from reader. * * @throws StreamException on stream IO errors. */ public static String readAll(final Reader reader, final int sizeHint) throws StreamException { final CharArrayWriter writer = new CharArrayWriter(sizeHint); final char[] buffer = new char[CHUNK_SIZE]; int len; try { while ((len = reader.read(buffer)) > 0) { writer.write(buffer, 0, len); } } catch (IOException e) { throw new StreamException(e); } finally { closeReader(reader); closeWriter(writer); } return writer.toString(); }
/** * Reads an X.509 certificate from ASN.1 encoded format from the given file. * * @param file File containing an DER or PEM encoded X.509 certificate. * * @return Certificate. * * @throws EncodingException on cert parsing errors. * @throws StreamException on IO errors. */ public static X509Certificate readCertificate(final File file) throws EncodingException, StreamException { return readCertificate(StreamUtil.makeStream(file)); }
/** * Reads all the data from the given input stream. * * @param input Input stream to read. * @param sizeHint Estimate of amount of data to be read in bytes. * * @return Byte array of data read from stream. * * @throws StreamException on stream IO errors. */ public static byte[] readAll(final InputStream input, final int sizeHint) throws StreamException { final ByteArrayOutputStream output = new ByteArrayOutputStream(sizeHint); try { Streams.pipeAll(input, output); } catch (IOException e) { throw new StreamException(e); } finally { closeStream(input); closeStream(output); } return output.toByteArray(); }
@Override protected void process( final CiphertextHeader header, final boolean mode, final InputStream input, final OutputStream output) { final BlockCipherAdapter cipher = newCipher(header, mode); final int outSize = cipher.getOutputSize(StreamUtil.CHUNK_SIZE); final byte[] outBuf = new byte[outSize > StreamUtil.CHUNK_SIZE ? outSize : StreamUtil.CHUNK_SIZE]; StreamUtil.pipeAll( input, output, (in, inOff, len, out) -> { final int n = cipher.processBytes(in, inOff, len, outBuf, 0); out.write(outBuf, 0, n); }); final int n = cipher.doFinal(outBuf, 0); try { output.write(outBuf, 0, n); } catch (IOException e) { throw new StreamException(e); } }
/** * Reads all the data from the given input stream. * * @param input Input stream to read. * * @return Byte array of data read from stream. * * @throws StreamException on stream IO errors. */ public static byte[] readAll(final InputStream input) throws StreamException { return readAll(input, 1024); }
/** * Reads an X.509 certificate chain from ASN.1 encoded format in the file at the given location. * * @param path Path to file containing a sequence of PEM or DER encoded certificates or PKCS#7 certificate chain. * * @return Certificate. * * @throws EncodingException on cert parsing errors. * @throws StreamException on IO errors. */ public static X509Certificate[] readCertificateChain(final String path) throws EncodingException, StreamException { return readCertificateChain(StreamUtil.makeStream(new File(path))); }
/** * Reads all the data from the file at the given path. * * @param path Path to file. * * @return Byte array of data read from file. * * @throws StreamException on stream IO errors. */ public static byte[] readAll(final String path) throws StreamException { return readAll(new File(path)); }
/** * Reads an X.509 certificate from ASN.1 encoded format in the file at the given location. * * @param path Path to file containing an DER or PEM encoded X.509 certificate. * * @return Certificate. * * @throws EncodingException on cert parsing errors. * @throws StreamException on IO errors. */ public static X509Certificate readCertificate(final String path) throws EncodingException, StreamException { return readCertificate(StreamUtil.makeStream(new File(path))); }
/** * Reads a DER or PEM-encoded public key from data in the given stream. The {@link InputStream} parameter is closed by * this method. * * @param in Input stream containing an encoded key. * * @return Public key. * * @throws EncodingException on key encoding errors. * @throws StreamException on IO errors. */ public static PublicKey readPublicKey(final InputStream in) throws EncodingException, StreamException { return decodePublicKey(StreamUtil.readAll(in)); }
/** * Reads an X.509 certificate chain from ASN.1 encoded format from the given file. * * @param file File containing a sequence of PEM or DER encoded certificates or PKCS#7 certificate chain. * * @return Certificate. * * @throws EncodingException on cert parsing errors. * @throws StreamException on IO errors. */ public static X509Certificate[] readCertificateChain(final File file) throws EncodingException, StreamException { return readCertificateChain(StreamUtil.makeStream(file)); }
/** * Reads an encoded private key from an input stream. Both PKCS#8 and OpenSSL "traditional" formats are supported in * DER or PEM encoding. See {@link #decodePrivateKey(byte[])} for supported asymmetric algorithms. The {@link * InputStream} parameter is closed by this method. * * @param in Input stream containing private key data. * * @return Private key. * * @throws EncodingException on key encoding errors. * @throws StreamException on IO errors reading data from file. */ public static PrivateKey readPrivateKey(final InputStream in) throws EncodingException, StreamException { return decodePrivateKey(StreamUtil.readAll(in)); }
/** * Reads an encrypted private key from an input stream. Both PKCS#8 and OpenSSL "traditional" formats are supported in * DER or PEM encoding. See {@link #decodePrivateKey(byte[])} for supported asymmetric algorithms. The {@link * InputStream} parameter is closed by this method. * * @param in Input stream containing private key data. * @param password Password used to encrypt private key. * * @return Private key. * * @throws EncodingException on key encoding errors. * @throws StreamException on IO errors. */ public static PrivateKey readPrivateKey(final InputStream in, final char[] password) throws EncodingException, StreamException { return decodePrivateKey(StreamUtil.readAll(in), password); }
@Override public SecretKey newInstance() throws StreamException { try { return new SecretKeySpec(StreamUtil.readAll(resource.getInputStream()), algorithm); } catch (IOException e) { throw new StreamException(e); } } }