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;
}
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();
}
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()));
}
}
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();
}
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();
}
Aggregations