/** * Copy bytes from remote file denoted by given URL to specified local file. This method creates local file if not * already exists. * * @param url source remote file URL, * @param file target local file. * @return the number of bytes transfered. * @throws FileNotFoundException if given <code>file</code> does not exist and cannot be created. * @throws IOException if bytes transfer fails. */ public static long copy(URL url, File file) throws FileNotFoundException, IOException { return copy(url, new FileOutputStream(file)); }
/** * Copy bytes from requested input stream to given target file. This method creates the target file if does not * already exist. * * @param inputStream source input stream, * @param file target file. * @return the number of bytes processed. * @throws FileNotFoundException if given <code>file</code> does not exist and cannot be created. * @throws IOException if bytes transfer fails. */ public static long copy(InputStream inputStream, File file) throws FileNotFoundException, IOException { return copy(inputStream, new FileOutputStream(file)); }
/** * Copy source file to target. Copy destination should be a file and this method throws access denied if attempt to * write to a directory. Source file should exist but target is created by this method, but if not already exist. * * @param source file to read from, should exist, * @param target file to write to. * @return the number of bytes transfered. * @throws FileNotFoundException if source file does not exist or target file does not exist and cannot be created. * @throws IOException if copy operation fails, including if <code>target</code> is a directory. */ public static long copy(File source, File target) throws FileNotFoundException, IOException { return copy(new FileInputStream(source), new FileOutputStream(target)); }
/** * Copy source characters to requested output characters stream. If given <code>chars</code> parameter is null or * empty this method does nothing. * * @param chars source characters stream, * @param writer target writer. * @throws IOException if copy operation fails. */ public static void save(CharSequence chars, Writer writer) throws IOException { if(chars != null) { StringReader reader = new StringReader(chars.toString()); Files.copy(reader, writer); } }
/** * Copy source characters to requested output bytes stream. If given <code>chars</code> parameter is null or empty * this method does nothing. * * @param chars source characters stream, * @param outputStream target bytes stream. * @throws IOException if copy operation fails. */ public static void save(CharSequence chars, OutputStream outputStream) throws IOException { if(chars != null) { ByteArrayInputStream inputStream = new ByteArrayInputStream(chars.toString().getBytes()); Files.copy(inputStream, outputStream); } }
/** * Copy bytes from given input stream to file denoted by requested base directory and file name. Destination file is * create by {@link File#File(File, String)}. This method tries to create destination file and throws exception if * fails. * * @param inputStream source bytes stream, * @param baseDir destination base directory, * @param fileName destination file name. * @return the number of bytes processed. * @throws FileNotFoundException if given <code>baseDir</code> and <code>fileName</code> does not denote and existing * file and cannot be created. * @throws IOException if bytes transfer fails. */ public static long copy(InputStream inputStream, File baseDir, String fileName) throws FileNotFoundException, IOException { return copy(inputStream, new FileOutputStream(new File(baseDir, fileName))); }
/** * Dump UTF-8 character stream content to standard out. * * @param reader source UTF-8 character stream. * @throws IOException if reading from character stream fails. */ public static void dump(Reader reader) throws IOException { copy(new ReaderInputStream(reader), System.out); }
@Override protected void stream(String name, String fileName, String contentType, InputStream inputStream) throws Throwable { setValue(name, new UploadedFileImpl(name, fileName, contentType, Files.copy(inputStream))); }
/** * Binary copy to temporary file. Be aware that temporary file is removed at JVM exit. * * @param stream source stream. * @return temporarily created file. * @throws IOException if copy operation fails. */ public static File copy(InputStream stream) throws IOException { File file = createTempFile(); copy(stream, new FileOutputStream(file)); return file; }
/** * Serialize this file resource to HTTP response. Disable cache and set content type and content length. * * @param httpResponse HTTP response. * @throws IOException if writing to underlying stream fails. */ @Override public void serialize(HttpServletResponse httpResponse) throws IOException { httpResponse.setHeader(HttpHeader.CACHE_CONTROL, HttpHeader.NO_CACHE); httpResponse.addHeader(HttpHeader.CACHE_CONTROL, HttpHeader.NO_STORE); httpResponse.setHeader(HttpHeader.PRAGMA, HttpHeader.NO_CACHE); httpResponse.setDateHeader(HttpHeader.EXPIRES, 0); httpResponse.setContentType(contentType); httpResponse.setHeader(HttpHeader.CONTENT_LENGTH, Long.toString(file.length())); Files.copy(file, httpResponse.getOutputStream()); } }
/** * Copy text from character stream to temporary file then close both the source reader and temporary file. Be aware * that temporary file is removed at JVM exit. * * @param reader source character reader. * @return the newly created temporarily file. * @throws IOException if copy operation fails. */ public static File copy(Reader reader) throws IOException { File file = createTempFile(); copy(reader, new FileWriter(file)); return file; }
/** * Copy text from byte stream using specified encoding to temporary file then close both input stream and temporary * file. Be aware that temporary file is removed at JVM exit. * * @param stream input stream to read from, * @param encoding used character encoding. * @return temporarily created file. * @throws IOException if copy operation fails. */ public static File copy(InputStream stream, String encoding) throws IOException { File file = createTempFile(); copy(new InputStreamReader(stream, encoding), new OutputStreamWriter(new FileOutputStream(file), encoding)); return file; }
/** * Copy source file bytes to requested output stream. Note that output stream is closed after transfer completes, * including on error. * * @param file source file, * @param outputStream destination output stream. * @return the number of bytes processed. * @throws FileNotFoundException if <code>file</code> does not exist. * @throws IOException bytes processing fails. * @throws IllegalArgumentException if input file or output stream is null. */ public static long copy(File file, OutputStream outputStream) throws IOException { Params.notNull(file, "Input file"); return copy(new FileInputStream(file), outputStream); }
/** * Retrieve text resource content as a string. Uses {@link #getResourceAsReader(String)} to reader resource content * and store it in a String. * * @param name resource name. * @return resource content as string. * @throws NoSuchBeingException if resource not found. * @throws IOException if resource reading fails. */ public static String getResourceAsString(String name) throws IOException { StringWriter writer = new StringWriter(); Files.copy(getResourceAsReader(name), writer); return writer.toString(); }
/** * Binary copy from URL to local temporary file. * * @param url source file URL. * @return temporarily created file. * @throws IOException if copy operation fails. * @throws IllegalArgumentException if <code>url</code> parameter is null. */ public static File copy(URL url) throws IOException, IllegalArgumentException { Params.notNull(url, "Source URL"); URLConnection connection = url.openConnection(); connection.setDoInput(true); // copy method takes care to close input stream so there is no need to handle URL connection close here return copy(connection.getInputStream()); }
/** * Copy text from URL using specified encoding to local temporary file then close both URL input stream and temporary * file. * * @param url source file URL, * @param encoding character encoding to use. * @return temporarily created file. * @throws IOException if copy operation fails. * @throws IllegalArgumentException if <code>url</code> parameter is null. */ public static File copy(URL url, String encoding) throws IOException, IllegalArgumentException { Params.notNull(url, "Source URL"); URLConnection connection = url.openConnection(); connection.setDoInput(true); return copy(connection.getInputStream(), encoding); }
/** * Copy bytes from remote file denoted by given URL to requested output stream. Destination stream is closed before * this method returning. * * @param url URL of the source remote file, * @param outputStream destination output stream. * @return the number of bytes transfered. * @throws IOException the number of bytes transfered. * @throws IllegalArgumentException if <code>url</code> is null. */ public static long copy(URL url, OutputStream outputStream) throws IOException, IllegalArgumentException { Params.notNull(url, "Source URL"); URLConnection connection = url.openConnection(); connection.setDoInput(true); // copy method takes care to close both streams so there is no need to handle here URL connection close return copy(connection.getInputStream(), outputStream); }
/** * Copy remote binary file denoted by requested URL to local file identified by given base directory and file name. * Local file is created by {@link File#File(File, String)}. This method creates local file if it does already exist. * * @param url remote file URL, * @param baseDir local file base directory, * @param fileName local file name. * @return the number of bytes transfered. * @throws FileNotFoundException if given local file does not exist and cannot be created. * @throws IOException if transfer fails. * @throws IllegalArgumentException if <code>url</code> is null. */ public static long copy(URL url, File baseDir, String fileName) throws FileNotFoundException, IOException, IllegalArgumentException { Params.notNull(url, "Source URL"); URLConnection connection = url.openConnection(); connection.setDoInput(true); // copy method takes care to close both streams so there is no need to handle here URL connection close return copy(connection.getInputStream(), new FileOutputStream(new File(baseDir, fileName))); }
/** * Inject variables into template and return resulting string. This method reflects current status of the variables map. If * variables map is not completely initialized resulting string will still have <code>${...}</code> variable marks. */ @Override public String toString() { VariablesWriter writer = new VariablesWriter(variables); try { Files.copy(new StringReader(template), writer); } catch (IOException e) { log.error(e); } return writer.toString(); } }
/** * Inject string values into variables from a given template string. Template uses de facto standard variable * notation, <code>${variable-name}</code>, where name is the key from given variables hash. It is legal for a * variable to be declared into template but be missing from variables hash, in which case variable from template is * left unchanged. * * @param template source string template, * @param variables string variables hash. * @return resulting string with variables replaced. */ public static String injectVariables(String template, Map<String, String> variables) { VariablesWriter writer = new VariablesWriter(variables); try { Files.copy(new StringReader(template), writer); } catch(IOException e) { log.error(e); } return writer.toString(); }