List of usage examples for java.io BufferedOutputStream write
@Override public synchronized void write(int b) throws IOException
From source file:com.uberspot.storageutils.StorageUtils.java
/** Save the given string to a file in external storage * @param obj the object to save//from w ww . j a va 2s . com * @param directory the directory in the SD card to save it into * @param fileName the name of the file * @param overwrite if set to true the file will be overwritten if it already exists * @return true if the file was written successfully, false otherwise */ public static boolean saveStringToExternalStorage(String obj, String directory, String fileName, boolean overwrite) { if (!directory.startsWith(File.separator)) directory = File.separator + directory; File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + directory); if (!dir.exists()) dir.mkdirs(); File file = new File(dir, fileName); if (file.exists() && !overwrite) return false; BufferedOutputStream output = null; try { output = new BufferedOutputStream(new FileOutputStream(file)); output.write(obj.getBytes()); output.flush(); return true; } catch (Exception e) { e.printStackTrace(System.out); } finally { if (output != null) try { output.close(); } catch (IOException e) { } } return false; }
From source file:de.nrw.hbz.deepzoomer.fileUtil.FileUtil.java
/** * Method saves a String to File// w w w .ja va 2s . co m * @param fileName * @param contentString * @return */ public static String saveStringToResultFile(String fileName, String contentString) { FileOutputStream fos = null; BufferedOutputStream bos = null; try { inputFile = new File(Configuration.getResultDirPath() + fileName); fos = new FileOutputStream(inputFile); bos = new BufferedOutputStream(fos); bos.write(contentString.getBytes("UTF-8")); } catch (IOException ioExc) { log.error(ioExc); } finally { if (bos != null) { try { bos.close(); } catch (IOException ioExc) { log.error(ioExc); } } if (fos != null) { try { fos.close(); } catch (IOException ioExc) { log.error(ioExc); } } } log.debug("File-Size: " + inputFile.length()); return inputFile.getName(); }
From source file:fr.paris.lutece.plugins.updater.service.UpdateService.java
/** * Copies data from an input stream to an output stream. * @param inStream The input stream/* ww w . j a v a 2 s . c o m*/ * @param outStream The output stream * @throws IOException If an I/O error occurs */ private static void copyStream(InputStream inStream, OutputStream outStream) throws IOException { BufferedInputStream inBufferedStream = new BufferedInputStream(inStream); BufferedOutputStream outBufferedStream = new BufferedOutputStream(outStream); int nByte; while ((nByte = inBufferedStream.read()) > -1) { outBufferedStream.write(nByte); } outBufferedStream.close(); inBufferedStream.close(); }
From source file:de.nrw.hbz.deepzoomer.fileUtil.FileUtil.java
/** * <p><em>Title: Save InputSream to an temporary File</em></p> * <p>Description: </p>/* www.jav a 2s . c om*/ * * @return */ public static void saveInputStreamToTempFile(InputStream is, String fileName) { File outputFile = new File(Configuration.getTempDirPath() + fileName); BufferedInputStream bis = new BufferedInputStream(is); BufferedOutputStream bos = null; FileOutputStream fos = null; try { fos = new FileOutputStream(outputFile); bos = new BufferedOutputStream(fos); int i = -1; while ((i = bis.read()) != -1) { bos.write(i); } bos.flush(); } catch (Exception e) { log.error(e); } finally { if (bos != null) { try { bos.close(); } catch (IOException ioExc) { log.error(ioExc); } } if (fos != null) { try { fos.close(); } catch (IOException ioExc) { log.error(ioExc); } } if (bis != null) { try { bis.close(); } catch (IOException ioExc) { log.error(ioExc); } } if (is != null) { try { is.close(); } catch (IOException ioExc) { log.error(ioExc); } } } }
From source file:com.manning.androidhacks.hack040.util.ImageFetcher.java
/** * Download a bitmap from a URL, write it to a disk and return the File * pointer. This implementation uses a simple disk cache. * /*www .ja v a2s . c om*/ * @param context * The context to use * @param urlString * The URL to fetch * @param cache * The disk cache instance to get the download directory from. * @return A File pointing to the fetched bitmap */ public static File downloadBitmap(Context context, String urlString, DiskLruCache cache) { final File cacheFile = new File(cache.createFilePath(urlString)); if (cache.containsKey(urlString)) { if (BuildConfig.DEBUG) { Log.d(TAG, "downloadBitmap - found in http cache - " + urlString); } return cacheFile; } if (BuildConfig.DEBUG) { Log.d(TAG, "downloadBitmap - downloading - " + urlString); } Utils.disableConnectionReuseIfNecessary(); HttpURLConnection urlConnection = null; BufferedOutputStream out = null; try { final URL url = new URL(urlString); urlConnection = (HttpURLConnection) url.openConnection(); final InputStream in = new BufferedInputStream(urlConnection.getInputStream(), Utils.IO_BUFFER_SIZE); out = new BufferedOutputStream(new FileOutputStream(cacheFile), Utils.IO_BUFFER_SIZE); int b; while ((b = in.read()) != -1) { out.write(b); } cache.putFromFetcher(urlString); return cacheFile; } catch (final IOException e) { Log.e(TAG, "Error in downloadBitmap - " + e); } finally { if (urlConnection != null) { urlConnection.disconnect(); } if (out != null) { try { out.close(); } catch (final IOException e) { Log.e(TAG, "Error in downloadBitmap - " + e); } } } return null; }
From source file:fr.landel.utils.io.FileUtils.java
/** * Write the content of the buffer into a file and create intermediate * directories if necessary./*from w w w .ja va 2 s . c o m*/ * * @param buffer * the buffer * @param file * The file * @param charset * The charset * @throws IOException * Exception thrown if problems occurs during writing */ public static void writeFileContent(final StringBuilder buffer, final File file, final Charset charset) throws IOException { if (buffer != null && file != null && FileSystemUtils.createDirectory(file.getParentFile())) { final BufferedOutputStream bos = IOStreamUtils.createBufferedOutputStream(file); bos.write(buffer.toString().getBytes(ObjectUtils.defaultIfNull(charset, StandardCharsets.UTF_8))); CloseableManager.close(file); } }
From source file:fr.paris.lutece.plugins.upload.web.UploadJspBean.java
/** * Copies data from an input stream to an output stream. * @param inStream The input stream/* w ww. j a v a 2 s . com*/ * @param outStream The output stream * @throws IOException If an I/O error occurs */ private static void copyStream(InputStream inStream, OutputStream outStream) throws IOException { BufferedInputStream inBufferedStream = new BufferedInputStream(inStream); BufferedOutputStream outBufferedStream = new BufferedOutputStream(outStream); int nByte = 0; while ((nByte = inBufferedStream.read()) > -1) { outBufferedStream.write(nByte); } outBufferedStream.close(); inBufferedStream.close(); }
From source file:de.nrw.hbz.deepzoomer.fileUtil.FileUtil.java
/** * <p><em>Title: Create a temporary File from a Base64 encoded byteStream</em></p> * <p>Description: Method creates a temporary file from the bytestream * representing the orginal PDF, that should be converted</p> * //from w ww. j a v a 2 s . co m * @param stream <code>String</code> * @return <code>String</code> Filename of newly created temporary File */ public static String saveStreamToTempFile(String fileName, String stream) { FileOutputStream fos = null; BufferedOutputStream bos = null; try { //System.out.println("Base64 kodierter Stream: " + stream.length()); inputFile = new File(Configuration.getTempDirPath() + fileName); log.debug(Configuration.getTempDirPath()); fos = new FileOutputStream(inputFile); bos = new BufferedOutputStream(fos); bos.write(Base64.decodeBase64(stream.getBytes("UTF-8"))); } catch (IOException ioExc) { log.error(ioExc); } finally { if (bos != null) { try { bos.close(); } catch (IOException ioExc) { log.error(ioExc); } } if (fos != null) { try { fos.close(); } catch (IOException ioExc) { log.error(ioExc); } } } log.debug("File-Size: " + inputFile.length()); return inputFile.getName(); }
From source file:cn.kk.exia.MangaDownloader.java
public final static HttpURLConnection getUrlConnection(final String url, final boolean post, final String output) throws IOException { int retries = 0; HttpURLConnection conn;//from w w w.j a v a2s . com while (true) { try { final URL urlObj = new URL(url); conn = (HttpURLConnection) urlObj.openConnection(); conn.setConnectTimeout(15000); conn.setReadTimeout(30000); if (post) { conn.setRequestMethod("POST"); } final String referer; final int pathIdx; if ((pathIdx = url.lastIndexOf('/')) > "https://".length()) { referer = url.substring(0, pathIdx); } else { referer = url; } conn.setRequestProperty("Referer", referer); final Set<String> keys = MangaDownloader.DEFAULT_CONN_HEADERS.keySet(); for (final String k : keys) { final String value = MangaDownloader.DEFAULT_CONN_HEADERS.get(k); if (value != null) { conn.setRequestProperty(k, value); } } // conn.setUseCaches(false); if (output != null) { conn.setDoOutput(true); final BufferedOutputStream out = new BufferedOutputStream(conn.getOutputStream()); out.write(output.getBytes(MangaDownloader.CHARSET_UTF8)); out.close(); } if (MangaDownloader.appendCookies(MangaDownloader.cookie, conn)) { MangaDownloader.putConnectionHeader("Cookie", MangaDownloader.cookie.toString()); } break; } catch (final Throwable e) { // System.err.println(e.toString()); if (retries++ > 10) { throw new IOException(e); } else { try { Thread.sleep((60 * retries * MangaDownloader.sleepBase) + ((int) Math.random() * MangaDownloader.sleepBase * 60 * retries)); } catch (final InterruptedException e1) { e1.printStackTrace(); } } } } return conn; }
From source file:org.alfresco.selenium.FetchUtil.java
/** * Retrieve file with authentication, using {@link WebDriver} cookie we * keep the session and use HttpClient to download files that requires * user to be authenticated. /*w w w.j a v a 2 s .c o m*/ * @param resourceUrl path to file to download * @param driver {@link WebDriver} * @param output path to output the file * @throws IOException if error */ protected static File retrieveFile(final String resourceUrl, final CloseableHttpClient client, final File output) throws IOException { HttpGet httpGet = new HttpGet(resourceUrl); BufferedOutputStream bos = null; BufferedInputStream bis = null; HttpResponse response = null; try { response = client.execute(httpGet); HttpEntity entity = response.getEntity(); bos = new BufferedOutputStream(new FileOutputStream(output)); bis = new BufferedInputStream(entity.getContent()); int inByte; while ((inByte = bis.read()) != -1) bos.write(inByte); HttpClientUtils.closeQuietly(response); } catch (Exception e) { logger.error("Unable to fetch file " + resourceUrl, e); } finally { if (response != null) { HttpClientUtils.closeQuietly(response); } if (bis != null) { bis.close(); } if (bos != null) { bos.close(); } } return output; }