private static void fileCopyUsingApacheCommons() throws IOException
{
File fileToCopy = new File("c:/temp/testoriginal.txt");
File newFile = new File("c:/temp/testcopied.txt");
FileUtils.copyFile(fileToCopy, newFile);
// OR
IOUtils.copy(new FileInputStream(fileToCopy), new FileOutputStream(newFile));
}
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, encoding);
String theString = writer.toString();
File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2, true); // appending output stream
try {
IOUtils.copy(in, out);
}
finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
public void givenUsingCommonsIoWithCopy_whenConvertingAnInputStreamToAString_thenCorrect()
throws IOException {
String originalString = randomAlphabetic(8);
InputStream inputStream = new ByteArrayInputStream(originalString.getBytes());
StringWriter writer = new StringWriter();
String encoding = StandardCharsets.UTF_8.name();
IOUtils.copy(inputStream, writer, encoding);
assertThat(writer.toString(), equalTo(originalString));
}
public class GzipApache {
private GzipApache() {}
public static void compressGZIP(File input, File output) throws IOException {
try (GzipCompressorOutputStream out = new GzipCompressorOutputStream(new FileOutputStream(output))){
IOUtils.copy(new FileInputStream(input), out);
}
}
public static void decompressGZIP(File input, File output) throws IOException {
try (GzipCompressorInputStream in = new GzipCompressorInputStream(new FileInputStream(input))){
IOUtils.copy(in, new FileOutputStream(output));
}
}
}
public void setUp() throws Exception {
// resource file
this.jsonResource = "org/apache/geode/security/templates/security.json";
InputStream inputStream = ClassLoader.getSystemResourceAsStream(this.jsonResource);
assertThat(inputStream).isNotNull();
// non-resource file
this.jsonFile = new File(temporaryFolder.getRoot(), "security.json");
IOUtils.copy(inputStream, new FileOutputStream(this.jsonFile));
// string
this.json = FileUtils.readFileToString(this.jsonFile, "UTF-8");
this.exampleSecurityManager = new ExampleSecurityManager();
}
private OpenPgpDataSink<MimeBodyPart> getDataSinkForOpenPgpDecryptedInlineData() {
return new OpenPgpDataSink<MimeBodyPart>() {
@Override
public MimeBodyPart processData(InputStream is) throws IOException {
try {
ByteArrayOutputStream decryptedByteOutputStream = new ByteArrayOutputStream();
IOUtils.copy(is, decryptedByteOutputStream);
TextBody body = new TextBody(new String(decryptedByteOutputStream.toByteArray()));
return new MimeBodyPart(body, "text/plain");
} catch (MessagingException e) {
Timber.e(e, "MessagingException");
}
return null;
}
};
}
public void getAuthorizedPhoto(@PathVariable String eppn, HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
List<Card> validCards = Card.findCardsByEppnAndEtatNotEquals(eppn, Etat.REJECTED, "dateEtat", "desc").getResultList();
if(!validCards.isEmpty()) {
Card card = validCards.get(0);
User user = User.findUser(eppn);
if(user.getDifPhoto()) {
PhotoFile photoFile = card.getPhotoFile();
Long size = photoFile.getFileSize();
String contentType = photoFile.getContentType();
response.setContentType(contentType);
response.setContentLength(size.intValue());
IOUtils.copy(photoFile.getBigFile().getBinaryFile().getBinaryStream(), response.getOutputStream());
}else{
ClassPathResource noImg = new ClassPathResource(ManagerCardController.IMG_INTERDIT);
IOUtils.copy(noImg.getInputStream(), response.getOutputStream());
}
}
}
private static void processFolder(final File folder, final ZipOutputStream zipOutputStream, final int prefixLength)
throws IOException {
for (final File file : folder.listFiles()) {
if (file.isFile()) {
final ZipEntry zipEntry = new ZipEntry(file.getPath().substring(prefixLength));
zipOutputStream.putNextEntry(zipEntry);
try (FileInputStream inputStream = new FileInputStream(file)) {
IOUtils.copy(inputStream, zipOutputStream);
}
zipOutputStream.closeEntry();
} else if (file.isDirectory()) {
processFolder(file, zipOutputStream, prefixLength);
}
}
}
private String responseBody(RequestContext context) throws IOException {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream(STREAM_BUFFER_SIZE.get())) {
IOUtils.copy(context.getResponseDataStream(), outputStream);
context.setResponseBody(outputStream.toString());
return outputStream.toString();
}
}