/** * Copies the fromFile to the toFile location. If toFile is a directory, a * new file is created in that directory, with the name of the fromFile. * If the toFile exists, the file will not be copied, unless owerWrite is * true. * * @param pFromFile The file to copy from * @param pToFile The file to copy to * @return true if the file was copied successfully, * false if the output file exists. In all other cases, an * IOException is thrown, and the method does not return a value. * @throws IOException if an i/o error occurs during copy */ public static boolean copy(File pFromFile, File pToFile) throws IOException { return copy(pFromFile, pToFile, false); }
copy(in, out);
/** * Copies the fromFile to the toFile location. If toFile is a directory, a * new file is created in that directory, with the name of the fromFile. * If the toFile exists, the file will not be copied, unless owerWrite is * true. * * @param pFromFileName The name of the file to copy from * @param pToFileName The name of the file to copy to * @return true if the file was copied successfully, * false if the output file exists. In all other cases, an * IOException is thrown, and the method does not return a value. * @throws IOException if an i/o error occurs during copy */ public static boolean copy(String pFromFileName, String pToFileName) throws IOException { return copy(new File(pFromFileName), new File(pToFileName), false); }
/** * Copies the fromFile to the toFile location. If toFile is a directory, a * new file is created in that directory, with the name of the fromFile. * If the toFile exists, the file will not be copied, unless owerWrite is * true. * * @param pFromFileName The name of the file to copy from * @param pToFileName The name of the file to copy to * @param pOverWrite Specifies if the toFile should be overwritten, if it * exists. * @return true if the file was copied successfully, * false if the output file exists, and the owerWrite parameter is * false. In all other cases, an * IOException is thrown, and the method does not return a value. * @throws IOException if an i/o error occurs during copy */ public static boolean copy(String pFromFileName, String pToFileName, boolean pOverWrite) throws IOException { return copy(new File(pFromFileName), new File(pToFileName), pOverWrite); }
private static File dumpToFile(final ImageInputStream stream) throws IOException { File tempFile = File.createTempFile("imageio-icns-", ".png"); tempFile.deleteOnExit(); FileOutputStream out = new FileOutputStream(tempFile); try { FileUtil.copy(IIOUtil.createStreamAdapter(stream), out); } finally { out.close(); } return tempFile; }
/** * Reads all data from the input stream to a byte array. * * @param pInput The input stream to read from * @return The content of the stream as a byte array. * @throws IOException if an i/o error occurs during read. */ public static byte[] read(InputStream pInput) throws IOException { // Create byte array ByteArrayOutputStream bytes = new FastByteArrayOutputStream(BUF_SIZE); // Copy from stream to byte array copy(pInput, bytes); return bytes.toByteArray(); }
/** * Copies a directory recursively. If the destination folder does not exist, * it is created * * @param pFrom the source directory * @param pTo the destination directory * @param pOverWrite {@code true} if we should allow overwrting existing files * @return {@code true} if all files were copied sucessfully * @throws IOException if {@code pTo} exists, and it not a directory, * or if copying of any of the files in the folder fails */ private static boolean copyDir(File pFrom, File pTo, boolean pOverWrite) throws IOException { if (pTo.exists() && !pTo.isDirectory()) { throw new IOException("A directory may only be copied to another directory, not to a file"); } pTo.mkdirs(); // mkdir? boolean allOkay = true; File[] files = pFrom.listFiles(); for (File file : files) { if (!copy(file, new File(pTo, file.getName()), pOverWrite)) { allOkay = false; } } return allOkay; }
FileUtil.copy(in, response.getOutputStream());
private File createFileWithContent(final InputStream pStream) throws IOException { File temp = File.createTempFile("tm-io-junit", null); temp.deleteOnExit(); OutputStream os = new FileOutputStream(temp); try { FileUtil.copy(pStream, os); } finally { os.close(); pStream.close(); } return temp; }
fromClient = pRequest.getInputStream(); toRemote = pRemoteConnection.getOutputStream(); FileUtil.copy(fromClient, toRemote);
FileUtil.copy(fromRemote, toClient);
public Void answer(InvocationOnMock invocation) throws Throwable { HttpServletResponse response = (HttpServletResponse) invocation.getArguments()[1]; response.setContentType("image/png"); response.setContentLength(104417); InputStream stream = getClass().getResourceAsStream("/com/twelvemonkeys/servlet/image/12monkeys-splash.png"); assertNotNull("Missing test resource", stream); FileUtil.copy(stream, response.getOutputStream()); return null; } }).when(chain).doFilter(any(ServletRequest.class), any(ServletResponse.class));
@Test public void testEmptyDecode2() throws IOException { String data = ""; InputStream in = new DecoderStream(new ByteArrayInputStream(data.getBytes()), createDecoder()); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); FileUtil.copy(in, bytes); assertEquals("Strings does not match", "", new String(bytes.toByteArray())); }
@Test public void testShortDecode() throws IOException { String data = "dGVzdA=="; InputStream in = new DecoderStream(new ByteArrayInputStream(data.getBytes()), createDecoder()); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); FileUtil.copy(in, bytes); assertEquals("Strings does not match", "test", new String(bytes.toByteArray())); }
public Void answer(InvocationOnMock invocation) throws Throwable { HttpServletRequest request = (HttpServletRequest) invocation.getArguments()[0]; HttpServletResponse response = (HttpServletResponse) invocation.getArguments()[1]; // Fake chaining here.. if (first.compareAndSet(false, true)) { barFilter.doFilter(request, response, (FilterChain) invocation.getMock()); return null; } // Otherwise, fake servlet/file response response.setContentType("image/gif"); InputStream stream = getClass().getResourceAsStream("/com/twelvemonkeys/servlet/image/tux.gif"); assertNotNull("Missing test resource", stream); FileUtil.copy(stream, response.getOutputStream()); return null; } }).when(chain).doFilter(any(ServletRequest.class), any(ServletResponse.class));
@Test public void testLongDecode() throws IOException { String data = "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVlciBhZGlwaXNjaW5nIGVsaXQuIEZ1" + "c2NlIGVzdC4gTW9yYmkgbHVjdHVzIGNvbnNlY3RldHVlciBqdXN0by4gVml2YW11cyBkYXBpYnVzIGxh" + "b3JlZXQgcHVydXMuIE51bmMgdml2ZXJyYSBkaWN0dW0gbmlzbC4gSW50ZWdlciB1bGxhbWNvcnBlciwg" + "bmlzaSBpbiBkaWN0dW0gYW1ldC4="; InputStream in = new DecoderStream(new ByteArrayInputStream(data.getBytes()), createDecoder()); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); FileUtil.copy(in, bytes); assertEquals("Strings does not match", "Lorem ipsum dolor sit amet, consectetuer adipiscing " + "elit. Fusce est. Morbi luctus consectetuer justo. Vivamus " + "dapibus laoreet purus. Nunc viverra dictum nisl. Integer " + "ullamcorper, nisi in dictum amet.", new String(bytes.toByteArray())); } }
private void runStreamTest(int pLength) throws Exception { byte[] data = createData(pLength); ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); OutputStream out = new EncoderStream(outBytes, createCompatibleEncoder(), true); out.write(data); out.close(); byte[] encoded = outBytes.toByteArray(); byte[] decoded = FileUtil.read(new DecoderStream(new ByteArrayInputStream(encoded), createDecoder())); assertArrayEquals(String.format("Data %d", pLength), data, decoded); InputStream in = new DecoderStream(new ByteArrayInputStream(encoded), createDecoder()); outBytes = new ByteArrayOutputStream(); FileUtil.copy(in, outBytes); outBytes.close(); in.close(); decoded = outBytes.toByteArray(); assertArrayEquals(String.format("Data %d", pLength), data, decoded); }
private void fakeResponse(HttpServletRequest pRequest, ImageServletResponseImpl pImageResponse) throws IOException { String uri = pRequest.getRequestURI(); int index = uri.lastIndexOf('/'); assertTrue(uri, index >= 0); String name = uri.substring(index + 1); InputStream in = getClass().getResourceAsStream(name); if (in == null) { pImageResponse.sendError(HttpServletResponse.SC_NOT_FOUND, uri + " not found"); } else { String ext = name.substring(name.lastIndexOf(".")); pImageResponse.setContentType(context.getMimeType("file" + ext)); pImageResponse.setContentLength(234); try { ServletOutputStream out = pImageResponse.getOutputStream(); try { FileUtil.copy(in, out); } finally { out.close(); } } finally { in.close(); } } }
FileUtil.copy(in, outBytes);
public static byte[] decode(String pData) throws IOException { InputStream in = new DecoderStream(new ByteArrayInputStream(pData.getBytes()), new Base64Decoder()); ByteArrayOutputStream bytes = new FastByteArrayOutputStream(pData.length() * 3); FileUtil.copy(in, bytes); return bytes.toByteArray(); }