/** * Get an output <code>FileChannel</code> for the provided * <code>File</code> * * @param file * <code>File</code> for which we need to get an output * <code>FileChannel</code> * @return a <code>FileChannel</code> * @throws IOException in case something bad happens. */ public static FileChannel getOuputChannel(File file) throws IOException { inputNotNull(file); return new RandomAccessFile(file, "rw").getChannel(); }
/** * Delete the specified File. * * @param file the file to delete */ public static void deleteFile(File file) { inputNotNull(file); if (!file.exists() || !file.canRead() || !file.isFile()) throw new IllegalStateException("Source is not in a legal state."); if (file.delete()) return; IOUtils.FILE_CLEANER.addFile(file); }
/** * Convert the input from the provided {@link Reader} into a {@link String}. * * @param inputReader the {@link Reader} to copy from. * @return a {@link String} that contains the content of the provided {@link Reader}. * @throws IOException in case something bad happens. */ public static String getStringFromReader(final Reader inputReader) throws IOException { inputNotNull(inputReader); final StringBuilder sb = new StringBuilder(); final char[] buffer = new char[1024]; int len; while ((len = inputReader.read(buffer)) >= 0) { char[] read = new char[len]; System.arraycopy(buffer, 0, read, 0, len); sb.append(read); } return sb.toString(); }
/** * Convert the input from the provided {@link InputStream} into a {@link String}. * * @param inputStream the {@link InputStream} to copy from. * @return a {@link String} that contains the content of the provided {@link InputStream}. * @throws IOException in case something bad happens. */ public static String getStringFromStream(InputStream inputStream) throws IOException { inputNotNull(inputStream); final Reader inReq = new InputStreamReader(inputStream); return getStringFromReader(inReq); }
/** * Convert the input from the provided {@link InputStream} into a {@link String}. * * @param inputStream the {@link InputStream} to copy from. * @return a {@link String} that contains the content of the provided {@link InputStream}. * @throws IOException in case something bad happens. */ public static String getStringFromStream(InputStream inputStream) throws IOException { inputNotNull(inputStream); final Reader inReq = new InputStreamReader(inputStream); return getStringFromReader(inReq); }
/** * Tries to convert a {@link URL} into a {@link File}. Return null if something bad happens * * @param fileURL {@link URL} to be converted into a {@link File}. * @return {@link File} for this {@link URL} or null. */ public static File URLToFile(URL fileURL) { inputNotNull(fileURL); try { return URLs.urlToFile(fileURL); } catch (Throwable t) { if (LOGGER.isLoggable(Level.FINE)) LOGGER.log(Level.FINE, t.getLocalizedMessage(), t); } return null; }
/** * Convert the input from the provided {@link Reader} into a {@link String}. * * @param inputStream the {@link Reader} to copy from. * @return a {@link String} that contains the content of the provided {@link Reader}. * @throws IOException in case something bad happens. */ public static String getStringFromReader(final Reader inputReader) throws IOException { inputNotNull(inputReader); final StringBuilder sb = new StringBuilder(); final char[] buffer = new char[1024]; int len; while ((len = inputReader.read(buffer)) >= 0) { char[] read = new char[len]; System.arraycopy(buffer, 0, read, 0, len); sb.append(read); } return sb.toString(); }
/** * Tries to convert a {@link URL} into a {@link File}. Return null if something bad happens * @param fileURL {@link URL} to be converted into a {@link File}. * @return {@link File} for this {@link URL} or null. */ public static File URLToFile(URL fileURL) { inputNotNull(fileURL); try { final File retFile= DataUtilities.urlToFile(fileURL); return retFile; }catch (Throwable t) { if(LOGGER.isLoggable(Level.FINE)) LOGGER.log(Level.FINE,t.getLocalizedMessage(),t); } return null; }
/** * Get an input <code>FileChannel</code> for the provided <code>File</code> * * @param source <code>File</code> for which we need to get an input <code>FileChannel</code> * @return a <code>FileChannel</code> * @throws IOException in case something bad happens. */ public static FileChannel getInputChannel(File source) { inputNotNull(source); if (!source.exists() || !source.canRead() || !source.isDirectory()) throw new IllegalStateException("Source is not in a legal state."); FileChannel channel = null; while (channel == null) { try { channel = new FileInputStream(source).getChannel(); } catch (Exception e) { channel = null; } } return channel; }
/** * Get an input <code>FileChannel</code> for the provided * <code>File</code> * * @param file * <code>File</code> for which we need to get an input * <code>FileChannel</code> * @return a <code>FileChannel</code> * @throws IOException in case something bad happens. */ public static FileChannel getInputChannel(File source) throws IOException { inputNotNull(source); if(!source.exists()||!source.canRead()||!source.isDirectory()) throw new IllegalStateException("Source is not in a legal state."); FileChannel channel = null; while (channel == null) { try { channel = new FileInputStream(source).getChannel(); } catch (Exception e) { channel = null; } } return channel; }
/** * Delete the specified File. * * @param sourceDirectory the directory to delete files from. * @param filter the {@link FilenameFilter} to use for selecting files to delete. * @param recursive boolean that specifies if we want to delete files recursively or not. * @return */ public static void deleteFile(File file) { inputNotNull(file); if(!file.exists()||!file.canRead()||!file.isFile()) throw new IllegalStateException("Source is not in a legal state."); if(file.delete()) return; IOUtils.FILE_CLEANER.addFile(file); }
/** * Delete all the files with matching the specified {@link FilenameFilter} in the specified * directory. The method can work recursively. * * @param sourceDirectory the directory to delete files from. * @param filter the {@link FilenameFilter} to use for selecting files to delete. * @param recursive boolean that specifies if we want to delete files recursively or not. */ public static boolean deleteDirectory( File sourceDirectory, FilenameFilter filter, boolean recursive, boolean deleteItself) { inputNotNull(sourceDirectory, filter); if (!sourceDirectory.exists() || !sourceDirectory.canRead() || !sourceDirectory.isDirectory()) throw new IllegalStateException("Source is not in a legal state."); final File[] files = (filter != null ? sourceDirectory.listFiles(filter) : sourceDirectory.listFiles()); for (File file : files) { if (file.isDirectory()) { if (recursive) { deleteDirectory(file, filter, recursive, deleteItself); } } else { if (!file.delete()) return false; } } return !deleteItself || sourceDirectory.delete(); }
/** * Delete all the files with matching the specified {@link FilenameFilter} in the specified directory. * The method can work recursively. * * @param sourceDirectory the directory to delete files from. * @param filter the {@link FilenameFilter} to use for selecting files to delete. * @param recursive boolean that specifies if we want to delete files recursively or not. * @return */ public static boolean deleteDirectory(File sourceDirectory, FilenameFilter filter, boolean recursive, boolean deleteItself) { inputNotNull(sourceDirectory,filter); if(!sourceDirectory.exists()||!sourceDirectory.canRead()||!sourceDirectory.isDirectory()) throw new IllegalStateException("Source is not in a legal state."); final File[] files = (filter != null ? sourceDirectory.listFiles(filter) : sourceDirectory.listFiles()); for (File file:files) { if (file.isDirectory()) { if(recursive) deleteDirectory(file, filter,recursive,deleteItself); } else { if(!file.delete()) return false; } } return deleteItself?sourceDirectory.delete():true; }
/** * Copies the content of the source channel onto the destination channel. * * @param bufferSize size of the temp buffer to use for this copy. * @param source the source {@link ReadableByteChannel}. * @param destination the destination {@link WritableByteChannel};. * @throws IOException in case something bad happens. */ public static void copyChannel( int bufferSize, ReadableByteChannel source, WritableByteChannel destination) throws IOException { inputNotNull(source, destination); if (!source.isOpen() || !destination.isOpen()) throw new IllegalStateException("Source and destination channels must be open."); final java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocateDirect(bufferSize); while (source.read(buffer) != -1) { // prepare the buffer for draining buffer.flip(); // write to destination while (buffer.hasRemaining()) destination.write(buffer); // clear buffer.clear(); } }
/** * Close the specified input <code>FileChannel</code> * * @throws IOException in case something bad happens. */ public static void closeQuietly(Channel channel) throws IOException { inputNotNull(channel); if (channel.isOpen()) channel.close(); }
inputNotNull(source, destDir); if (!source.exists() || !source.canRead() || source.isDirectory()) throw new IllegalStateException("Source is not in a legal state.");
/** * Close the specified input <code>FileChannel</code> * * @throws IOException in case something bad happens. */ public static void closeQuietly(Channel channel) throws IOException { inputNotNull(channel); if (channel.isOpen()) channel.close(); }
/** * Copies the content of the source channel onto the destination channel. * * @param bufferSize size of the temp buffer to use for this copy. * @param source the source {@link ReadableByteChannel}. * @param destination the destination {@link WritableByteChannel};. * @throws IOException in case something bad happens. */ public static void copyChannel(int bufferSize, ReadableByteChannel source,WritableByteChannel destination) throws IOException { inputNotNull(source,destination); if(!source.isOpen()||!destination.isOpen()) throw new IllegalStateException("Source and destination channels must be open."); final java.nio.ByteBuffer buffer= java.nio.ByteBuffer.allocateDirect(bufferSize); while(source.read(buffer)!=-1) { //prepare the buffer for draining buffer.flip(); //write to destination while(buffer.hasRemaining()) destination.write(buffer); //clear buffer.clear(); } }
/** * Convert the input from the provided {@link Reader} into a {@link String}. * * @param src the {@link StreamSource} to copy from. * @return a {@link String} that contains the content of the provided {@link Reader}. * @throws IOException in case something bad happens. */ public static String getStringFromStreamSource(StreamSource src) throws IOException { inputNotNull(src); InputStream inputStream = src.getInputStream(); if (inputStream != null) { return getStringFromStream(inputStream); } else { final Reader r = src.getReader(); return getStringFromReader(r); } }
/** * Convert the input from the provided {@link Reader} into a {@link String}. * * @param inputStream the {@link Reader} to copy from. * @return a {@link String} that contains the content of the provided {@link Reader}. * @throws IOException in case something bad happens. */ public static String getStringFromStreamSource(StreamSource src) throws IOException { inputNotNull(src); InputStream inputStream = src.getInputStream(); if(inputStream != null) { return getStringFromStream(inputStream); }else { final Reader r = src.getReader(); return getStringFromReader(r); } }