@Override public void transferTo(File dest) throws IOException, IllegalStateException { FileCopyUtils.copy(this.content, dest); }
/** * Transfer the received file to the given destination file. * <p>The default implementation simply copies the file input stream. * @since 5.1 * @see #getInputStream() * @see #transferTo(File) */ default void transferTo(Path dest) throws IOException, IllegalStateException { FileCopyUtils.copy(getInputStream(), Files.newOutputStream(dest)); }
/** * Copy the contents of the given byte array to the given output File. * @param in the byte array to copy from * @param out the file to copy to * @throws IOException in case of I/O errors */ public static void copy(byte[] in, File out) throws IOException { Assert.notNull(in, "No input byte array specified"); Assert.notNull(out, "No output File specified"); copy(new ByteArrayInputStream(in), Files.newOutputStream(out.toPath())); }
/** * Copy the contents of the given input File to the given output File. * @param in the file to copy from * @param out the file to copy to * @return the number of bytes copied * @throws IOException in case of I/O errors */ public static int copy(File in, File out) throws IOException { Assert.notNull(in, "No input File specified"); Assert.notNull(out, "No output File specified"); return copy(Files.newInputStream(in.toPath()), Files.newOutputStream(out.toPath())); }
/** * Copy the contents of the given InputStream into a new byte array. * Closes the stream when done. * @param in the stream to copy from (may be {@code null} or empty) * @return the new byte array that has been copied to (possibly empty) * @throws IOException in case of I/O errors */ public static byte[] copyToByteArray(@Nullable InputStream in) throws IOException { if (in == null) { return new byte[0]; } ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE); copy(in, out); return out.toByteArray(); }
/** * Copy the contents of the given Reader into a String. * Closes the reader when done. * @param in the reader to copy from (may be {@code null} or empty) * @return the String that has been copied to (possibly empty) * @throws IOException in case of I/O errors */ public static String copyToString(@Nullable Reader in) throws IOException { if (in == null) { return ""; } StringWriter out = new StringWriter(); copy(in, out); return out.toString(); }
@Override public void transferTo(Path dest) throws IOException, IllegalStateException { FileCopyUtils.copy(this.part.getInputStream(), Files.newOutputStream(dest)); } }
@Override public void transferTo(File dest) throws IOException, IllegalStateException { FileCopyUtils.copy(this.content, dest); }
@Override public void transferTo(File dest) throws IOException, IllegalStateException { this.part.write(dest.getPath()); if (dest.isAbsolute() && !dest.exists()) { // Servlet 3.0 Part.write is not guaranteed to support absolute file paths: // may translate the given path to a relative location within a temp dir // (e.g. on Jetty whereas Tomcat and Undertow detect absolute paths). // At least we offloaded the file from memory storage; it'll get deleted // from the temp dir eventually in any case. And for our user's purposes, // we can manually copy it to the requested location as a fallback. FileCopyUtils.copy(this.part.getInputStream(), Files.newOutputStream(dest.toPath())); } }
@Override public void transferTo(Path dest) throws IOException, IllegalStateException { if (!isAvailable()) { throw new IllegalStateException("File has already been moved - cannot be transferred again"); } FileCopyUtils.copy(this.fileItem.getInputStream(), Files.newOutputStream(dest)); }
static void createGzippedFile(String filePath) throws IOException { Resource location = new ClassPathResource("test/", EncodedResourceResolverTests.class); Resource resource = new FileSystemResource(location.createRelative(filePath).getFile()); Path gzFilePath = Paths.get(resource.getFile().getAbsolutePath() + ".gz"); Files.deleteIfExists(gzFilePath); File gzFile = Files.createFile(gzFilePath).toFile(); GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile)); FileCopyUtils.copy(resource.getInputStream(), out); gzFile.deleteOnExit(); }
static void createGzippedFile(String filePath) throws IOException { Resource location = new ClassPathResource("test/", EncodedResourceResolverTests.class); Resource resource = new FileSystemResource(location.createRelative(filePath).getFile()); Path gzFilePath = Paths.get(resource.getFile().getAbsolutePath() + ".gz"); Files.deleteIfExists(gzFilePath); File gzFile = Files.createFile(gzFilePath).toFile(); GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(gzFile)); FileCopyUtils.copy(resource.getInputStream(), out); gzFile.deleteOnExit(); }
@Test public void copyFromByteArray() throws IOException { byte[] content = "content".getBytes(); ByteArrayOutputStream out = new ByteArrayOutputStream(content.length); FileCopyUtils.copy(content, out); assertTrue(Arrays.equals(content, out.toByteArray())); }
@Test public void copyFromString() throws IOException { String content = "content"; StringWriter out = new StringWriter(); FileCopyUtils.copy(content, out); assertEquals(content, out.toString()); }
@Test public void copyFromInputStream() throws IOException { byte[] content = "content".getBytes(); ByteArrayInputStream in = new ByteArrayInputStream(content); ByteArrayOutputStream out = new ByteArrayOutputStream(content.length); int count = FileCopyUtils.copy(in, out); assertEquals(content.length, count); assertTrue(Arrays.equals(content, out.toByteArray())); }
@Test public void copyFromReader() throws IOException { String content = "content"; StringReader in = new StringReader(content); StringWriter out = new StringWriter(); int count = FileCopyUtils.copy(in, out); assertEquals(content.length(), count); assertEquals(content, out.toString()); }
@Test public void doesNotExistOutputStream() throws IOException { File file = temporaryFolder.newFile("test"); file.delete(); PathResource resource = new PathResource(file.toPath()); FileCopyUtils.copy("test".getBytes(), resource.getOutputStream()); assertThat(resource.contentLength(), equalTo(4L)); }
@Test public void getBody() throws Exception { byte[] content = "Hello World".getBytes("UTF-8"); FileCopyUtils.copy(content, response.getBody()); assertArrayEquals("Invalid content written", content, mockResponse.getContentAsByteArray()); }
@Test public void outputStream() throws IOException { PathResource resource = new PathResource(temporaryFolder.newFile("test").toPath()); FileCopyUtils.copy("test".getBytes(StandardCharsets.UTF_8), resource.getOutputStream()); assertThat(resource.contentLength(), equalTo(4L)); }
@Test(expected = UnsupportedOperationException.class) public void headersAfterExecute() throws Exception { ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/status/ok"), HttpMethod.POST); request.getHeaders().add("MyHeader", "value"); byte[] body = "Hello World".getBytes("UTF-8"); FileCopyUtils.copy(body, request.getBody()); ClientHttpResponse response = request.execute(); try { request.getHeaders().add("MyHeader", "value"); } finally { response.close(); } }