/** * 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); }
/** * Gets the contents of the given file, as a byte array. * * @param pFilename the name of the file to get content from * @return the content of the file as a byte array. * @throws IOException if the read operation fails */ public static byte[] read(String pFilename) throws IOException { return read(new File(pFilename)); }
public boolean accept(File pFile) { // Directories are always supported if (pFile.isDirectory()) { return true; } // Test if we have an ImageWriter for this suffix String suffix = FileUtil.getExtension(pFile); return !StringUtil.isEmpty(suffix) && hasWriterForSuffix(suffix); }
public static void main(String[] pArgs) throws IOException { File file; if (pArgs[0].startsWith("file:")) { file = toFile(new URL(pArgs[0])); System.out.println(file); } else { file = new File(pArgs[0]); System.out.println(file.toURL()); } System.out.println("Free space: " + getFreeSpace(file) + "/" + getTotalSpace(file) + " bytes"); }
/** * Writes the contents from a byte array to a file. * * @param pFile The file to write to * @param pData The byte array to write * @return {@code true}, otherwise an IOException is thrown. * @throws IOException if an i/o error occurs during write. */ public static boolean write(File pFile, byte[] pData) throws IOException { boolean success = false; OutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(pFile)); success = write(out, pData); } finally { close(out); } return success; }
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); }
if (content != null && content.exists()) { byte[] contents = FileUtil.read(content); contentCache.put(pCacheURI + '.' + FileUtil.getExtension(content), response);
format = FileUtil.getExtension(out); String baseName = FileUtil.getBasename(in);
@SuppressWarnings("SameParameterValue") private static ICC_Profile readProfileFromClasspathResource(final String profilePath) { InputStream stream = ColorSpaces.class.getResourceAsStream(profilePath); if (stream != null) { if (DEBUG) { System.out.println("Loading profile from classpath resource: " + profilePath); } try { return ICC_Profile.getInstance(stream); } catch (IOException ignore) { if (DEBUG) { ignore.printStackTrace(); } } finally { FileUtil.close(stream); } } return null; }
/** * Deletes the specified file. * * @param pFile The file to delete * @return {@code true}, if the file existed and was deleted. * @throws java.io.IOException if deletion fails */ public static boolean delete(File pFile) throws IOException { return delete(pFile, false); }
/** * Writes the contents from a byte array to a file. * * @param pFilename The name of the file to write to * @param pData The byte array to write * @return {@code true}, otherwise an IOException is thrown. * @throws IOException if an i/o error occurs during write. */ public static boolean write(String pFilename, byte[] pData) throws IOException { return write(new File(pFilename), pData); }
/** * This method is called by the server before the filter goes into service, * and here it determines the file upload directory. * * @throws ServletException */ public void init() throws ServletException { // Get the name of the upload directory. String uploadDirParam = getInitParameter("uploadDir"); if (!StringUtil.isEmpty(uploadDirParam)) { try { URL uploadDirURL = getServletContext().getResource(uploadDirParam); uploadDir = FileUtil.toFile(uploadDirURL); } catch (MalformedURLException e) { throw new ServletException(e.getMessage(), e); } } if (uploadDir == null) { uploadDir = ServletUtil.getTempDir(getServletContext()); } }
/** * Deletes the specified file. * * @param pFile The file to delete * @param pForce Forces delete, even if the parameter is a directory, and * is not empty. Be careful! * @return {@code true}, if the file existed and was deleted. * @throws IOException if an i/o error occurs during delete. */ public static boolean delete(final File pFile, final boolean pForce) throws IOException { if (pForce && pFile.isDirectory()) { return deleteDir(pFile); } return pFile.exists() && pFile.delete(); }
@Test public void testSplit() throws IOException { InputStream inputStream = getClassLoaderResource("/contrib/tiff/multipage.tif").openStream(); File inputFile = File.createTempFile("imageiotest", "tif"); byte[] data = FileUtil.read(inputStream); FileUtil.write(inputFile, data); inputStream.close(); File outputDirectory = Files.createTempDirectory("imageio").toFile(); TIFFUtilities.split(inputFile, outputDirectory); ImageReader reader = ImageIO.getImageReadersByFormatName("TIF").next(); File[] outputFiles = outputDirectory.listFiles(); Assert.assertEquals(3, outputFiles.length); for (File outputFile : outputFiles) { ImageInputStream iis = ImageIO.createImageInputStream(outputFile); reader.setInput(iis); Assert.assertEquals(1, reader.getNumImages(true)); iis.close(); outputFile.delete(); } outputDirectory.delete(); inputFile.delete(); }
format = FileUtil.getExtension(out); String baseName = FileUtil.getBasename(in);
public static void main(String[] pArgs) throws IOException { File file; if (pArgs[0].startsWith("file:")) { file = toFile(new URL(pArgs[0])); System.out.println(file); } else { file = new File(pArgs[0]); System.out.println(file.toURL()); } System.out.println("Free space: " + getFreeSpace(file) + "/" + getTotalSpace(file) + " bytes"); }