Search in sources :

Example 46 with BufferedInputStream

use of java.io.BufferedInputStream in project grpc-java by grpc.

the class TestUtils method loadCert.

/**
   * Saves a file from the classpath resources in src/main/resources/certs as a file on the
   * filesystem.
   *
   * @param name  name of a file in src/main/resources/certs.
   */
public static File loadCert(String name) throws IOException {
    InputStream in = new BufferedInputStream(TestUtils.class.getResourceAsStream("/certs/" + name));
    File tmpFile = File.createTempFile(name, "");
    tmpFile.deleteOnExit();
    OutputStream os = new BufferedOutputStream(new FileOutputStream(tmpFile));
    try {
        int b;
        while ((b = in.read()) != -1) {
            os.write(b);
        }
        os.flush();
    } finally {
        in.close();
        os.close();
    }
    return tmpFile;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 47 with BufferedInputStream

use of java.io.BufferedInputStream in project grpc-java by grpc.

the class TestUtils method newSslSocketFactoryForCa.

/**
   * Creates an SSLSocketFactory which contains {@code certChainFile} as its only root certificate.
   */
public static SSLSocketFactory newSslSocketFactoryForCa(Provider provider, File certChainFile) throws Exception {
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(null, null);
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate cert = (X509Certificate) cf.generateCertificate(new BufferedInputStream(new FileInputStream(certChainFile)));
    X500Principal principal = cert.getSubjectX500Principal();
    ks.setCertificateEntry(principal.getName("RFC2253"), cert);
    // Set up trust manager factory to use our key store.
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(ks);
    SSLContext context = SSLContext.getInstance("TLS", provider);
    context.init(null, trustManagerFactory.getTrustManagers(), null);
    return context.getSocketFactory();
}
Also used : BufferedInputStream(java.io.BufferedInputStream) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) X500Principal(javax.security.auth.x500.X500Principal) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) FileInputStream(java.io.FileInputStream)

Example 48 with BufferedInputStream

use of java.io.BufferedInputStream in project translationstudio8 by heartsome.

the class FileManager method createZip.

/**
	 * 创建压缩包
	 * @param baseDir
	 *            所要压缩的根目录(包含绝对路径)
	 * @param zos
	 * @param lstRemoveFolderPath
	 *            所排除的目录名或文件名集合(目录为 baseDir 的子目录)
	 * @throws Exception
	 *             ;
	 */
public void createZip(String baseDir, ZipOutputStream zos, List<String> lstRemoveFolderPath) throws Exception {
    if (zos == null) {
        return;
    }
    File folderObject = new File(baseDir);
    if (folderObject.exists()) {
        List<File> fileList = getSubFiles(new File(baseDir), lstRemoveFolderPath);
        ZipEntry ze = null;
        byte[] buf = new byte[1024];
        int readLen = 0;
        for (int i = 0; i < fileList.size(); i++) {
            File f = (File) fileList.get(i);
            // 创建一个ZipEntry,并设置Name和其它的一些属性
            ze = new ZipEntry(getAbsFileName(baseDir, f));
            ze.setSize(f.length());
            ze.setTime(f.lastModified());
            // 将ZipEntry加到zos中,再写入实际的文件内容
            zos.putNextEntry(ze);
            InputStream is = new BufferedInputStream(new FileInputStream(f));
            while ((readLen = is.read(buf, 0, 1024)) != -1) {
                zos.write(buf, 0, readLen);
            }
            is.close();
        }
    } else {
        throw new Exception(MessageFormat.format(Messages.getString("file.FileManager.msg1"), folderObject.getAbsolutePath()));
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) ZipFile(java.util.zip.ZipFile) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 49 with BufferedInputStream

use of java.io.BufferedInputStream in project translationstudio8 by heartsome.

the class FileManager method releaseZipToFile.

/**
	 * 对压缩包解压
	 * @param sourceZip
	 *            压缩包路径
	 * @param outFileName
	 *            解压路径
	 * @throws IOException
	 *             ;
	 */
public void releaseZipToFile(String sourceZip, String outFileName) throws IOException {
    ZipFile zfile = new ZipFile(sourceZip);
    @SuppressWarnings("rawtypes") Enumeration zList = zfile.entries();
    ZipEntry ze = null;
    byte[] buf = new byte[1024];
    while (zList.hasMoreElements()) {
        // 从ZipFile中得到一个ZipEntry
        ze = (ZipEntry) zList.nextElement();
        if (ze.isDirectory()) {
            continue;
        }
        // 以ZipEntry为参数得到一个InputStream,并写到OutputStream中
        OutputStream os = new BufferedOutputStream(new FileOutputStream(getRealFileName(outFileName, ze.getName())));
        InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
        int readLen = 0;
        while ((readLen = is.read(buf, 0, 1024)) != -1) {
            os.write(buf, 0, readLen);
        }
        is.close();
        os.close();
    }
    zfile.close();
}
Also used : Enumeration(java.util.Enumeration) ZipFile(java.util.zip.ZipFile) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) OutputStream(java.io.OutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 50 with BufferedInputStream

use of java.io.BufferedInputStream in project hibernate-orm by hibernate.

the class BlobTest method toBytes.

private byte[] toBytes(InputStream inputStream) throws IOException {
    BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    int result = bufferedInputStream.read();
    while (result != -1) {
        byteArrayOutputStream.write((byte) result);
        result = bufferedInputStream.read();
    }
    return byteArrayOutputStream.toByteArray();
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1700 FileInputStream (java.io.FileInputStream)854 IOException (java.io.IOException)836 InputStream (java.io.InputStream)707 File (java.io.File)449 BufferedOutputStream (java.io.BufferedOutputStream)228 FileOutputStream (java.io.FileOutputStream)218 DataInputStream (java.io.DataInputStream)168 ByteArrayInputStream (java.io.ByteArrayInputStream)159 FileNotFoundException (java.io.FileNotFoundException)147 URL (java.net.URL)147 ZipEntry (java.util.zip.ZipEntry)123 ByteArrayOutputStream (java.io.ByteArrayOutputStream)112 OutputStream (java.io.OutputStream)100 ZipInputStream (java.util.zip.ZipInputStream)72 GZIPInputStream (java.util.zip.GZIPInputStream)68 ArrayList (java.util.ArrayList)62 HashMap (java.util.HashMap)62 HttpURLConnection (java.net.HttpURLConnection)61 ObjectInputStream (java.io.ObjectInputStream)56