List of usage examples for java.io BufferedInputStream read
public synchronized int read(byte b[], int off, int len) throws IOException
From source file:msec.org.TarUtil.java
private static void archiveFile(File file, TarArchiveOutputStream taos, String dir) throws Exception { TarArchiveEntry entry = new TarArchiveEntry(dir + file.getName()); entry.setSize(file.length());/*from ww w .jav a 2 s . co m*/ taos.putArchiveEntry(entry); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); int count; byte data[] = new byte[BUFFERSZ]; while ((count = bis.read(data, 0, BUFFERSZ)) != -1) { taos.write(data, 0, count); } bis.close(); taos.closeArchiveEntry(); }
From source file:es.deustotech.piramide.utils.tts.TextToSpeechWeb.java
private static void downloadFile(HttpResponse response, OutputStream os) throws IOException { final InputStream is = response.getEntity().getContent(); long size = response.getEntity().getContentLength(); final BufferedInputStream bis = new BufferedInputStream(is); final byte[] buffer = new byte[1024 * 1024]; // 1 MB long position = 0; while (position < size) { final int read = bis.read(buffer, 0, buffer.length); if (read <= 0) { break; }/*from ww w . j a v a 2 s . c o m*/ os.write(buffer, 0, read); os.flush(); position += read; } is.close(); }
From source file:com.amazonaws.util.Md5Utils.java
/** * Computes the MD5 hash of the data in the given input stream and returns * it as an array of bytes./*w ww .j a v a2 s. c o m*/ * Note this method closes the given input stream upon completion. */ public static byte[] computeMD5Hash(InputStream is) throws IOException { BufferedInputStream bis = new BufferedInputStream(is); try { MessageDigest messageDigest = MessageDigest.getInstance("MD5"); byte[] buffer = new byte[SIXTEEN_K]; int bytesRead; while ((bytesRead = bis.read(buffer, 0, buffer.length)) != -1) { messageDigest.update(buffer, 0, bytesRead); } return messageDigest.digest(); } catch (NoSuchAlgorithmException e) { // should never get here throw new IllegalStateException(e); } finally { try { bis.close(); } catch (Exception e) { LogFactory.getLog(Md5Utils.class).debug("Unable to close input stream of hash candidate: " + e); } } }
From source file:cn.guoyukun.spring.utils.FileCharset.java
public static String getCharset(final BufferedInputStream is) { String charset = DEFAULT_CHARSET; byte[] first3Bytes = new byte[3]; try {/* w w w.ja v a 2 s . c om*/ boolean checked = false; is.mark(0); int read = is.read(first3Bytes, 0, 3); if (read == -1) return charset; if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) { charset = "UTF-16LE"; checked = true; } else if (first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF) { charset = "UTF-16BE"; checked = true; } else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB && first3Bytes[2] == (byte) 0xBF) { charset = "UTF-8"; checked = true; } is.reset(); if (!checked) { int loc = 0; while ((read = is.read()) != -1 && loc < 100) { loc++; if (read >= 0xF0) break; if (0x80 <= read && read <= 0xBF) // ?BFGBK break; if (0xC0 <= read && read <= 0xDF) { read = is.read(); if (0x80 <= read && read <= 0xBF) // ? (0xC0 - 0xDF) // (0x80 // - 0xBF),?GB? continue; else break; } else if (0xE0 <= read && read <= 0xEF) {// ?? read = is.read(); if (0x80 <= read && read <= 0xBF) { read = is.read(); if (0x80 <= read && read <= 0xBF) { charset = "UTF-8"; break; } else break; } else break; } } } is.reset(); } catch (Exception e) { e.printStackTrace(); } return charset; }
From source file:ImageUtils.java
final public static ImageProperties getGifProperties(File file) throws FileNotFoundException, IOException { BufferedInputStream in = null; try {//from w ww. j a v a 2 s . c om in = new BufferedInputStream(new FileInputStream(file)); byte[] buf = new byte[10]; int count = in.read(buf, 0, 10); if (count < 10) { throw new RuntimeException("Not a valid Gif file!"); } if ((buf[0]) != (byte) 'G' || (buf[1]) != (byte) 'I' || (buf[2]) != (byte) 'F') { throw new RuntimeException("Not a valid Gif file!"); } int w1 = (buf[6] & 0xff) | (buf[6] & 0x80); int w2 = (buf[7] & 0xff) | (buf[7] & 0x80); int h1 = (buf[8] & 0xff) | (buf[8] & 0x80); int h2 = (buf[9] & 0xff) | (buf[9] & 0x80); int width = w1 + (w2 << 8); int height = h1 + (h2 << 8); return (new ImageProperties(width, height, null, "gif")); } finally { if (in != null) { try { in.close(); } catch (IOException e) { } } } }
From source file:Main.java
/** * Move the file in oldLocation to newLocation. */// w ww . j a va 2s .c om public static void moveFile(String tempLocation, String newLocation) throws IOException { File oldLocation = new File(tempLocation); if (oldLocation != null && oldLocation.exists()) { BufferedInputStream reader = new BufferedInputStream(new FileInputStream(oldLocation)); BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(newLocation, false)); try { byte[] buff = new byte[8192]; int numChars; while ((numChars = reader.read(buff, 0, buff.length)) != -1) { writer.write(buff, 0, numChars); } } catch (IOException ex) { } finally { try { if (reader != null) { writer.close(); reader.close(); } } catch (IOException ex) { } } } else { } }
From source file:Main.java
public static void copyFile(File oldLocation, File newLocation) throws IOException { if (oldLocation.exists()) { BufferedInputStream reader = new BufferedInputStream(new FileInputStream(oldLocation)); BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(newLocation, false)); try {// w w w. ja v a 2 s . c om byte[] buff = new byte[8192]; int numChars; while ((numChars = reader.read(buff, 0, buff.length)) != -1) { writer.write(buff, 0, numChars); } } catch (IOException ex) { throw new IOException( "IOException when transferring " + oldLocation.getPath() + " to " + newLocation.getPath()); } finally { try { if (reader != null) { writer.close(); reader.close(); } } catch (IOException ex) { Log.e(TAG, "Error closing files when transferring " + oldLocation.getPath() + " to " + newLocation.getPath()); } } } else { throw new IOException("Old location does not exist when transferring " + oldLocation.getPath() + " to " + newLocation.getPath()); } }
From source file:fr.pasteque.client.utils.URLTextGetter.java
public static void getBinary(final String url, final Map<String, String> getParams, final Handler h) { new Thread() { @Override/*from w w w . ja v a2 s . c o m*/ public void run() { try { String fullUrl = url; if (getParams != null && getParams.size() > 0) { fullUrl += "?"; for (String param : getParams.keySet()) { fullUrl += URLEncoder.encode(param, "utf-8") + "=" + URLEncoder.encode(getParams.get(param), "utf-8") + "&"; } } if (fullUrl.endsWith("&")) { fullUrl = fullUrl.substring(0, fullUrl.length() - 1); } HttpClient client = new DefaultHttpClient(); HttpResponse response = null; HttpGet req = new HttpGet(fullUrl); response = client.execute(req); int status = response.getStatusLine().getStatusCode(); if (status == HttpStatus.SC_OK) { // Get http response byte[] content = null; try { final int size = 10240; ByteArrayOutputStream bos = new ByteArrayOutputStream(size); byte[] buffer = new byte[size]; BufferedInputStream bis = new BufferedInputStream(response.getEntity().getContent(), size); int read = bis.read(buffer, 0, size); while (read != -1) { bos.write(buffer, 0, read); read = bis.read(buffer, 0, size); } content = bos.toByteArray(); } catch (IOException ioe) { ioe.printStackTrace(); } if (h != null) { Message m = h.obtainMessage(); m.what = SUCCESS; m.obj = content; m.sendToTarget(); } } else { if (h != null) { Message m = h.obtainMessage(); m.what = STATUS_NOK; m.obj = new Integer(status); m.sendToTarget(); } } } catch (IOException e) { if (h != null) { Message m = h.obtainMessage(); m.what = ERROR; m.obj = e; m.sendToTarget(); } } } }.start(); }
From source file:Main.java
public static boolean prepareDex(Context context, File dexInternalStoragePath, String dexFile) { BufferedInputStream bis = null; OutputStream dexWriter = null; try {/*from w w w . j av a2s .co m*/ bis = new BufferedInputStream(context.getAssets().open(dexFile)); dexWriter = new BufferedOutputStream(new FileOutputStream(dexInternalStoragePath)); byte[] buf = new byte[BUF_SIZE]; int len; while ((len = bis.read(buf, 0, BUF_SIZE)) > 0) { dexWriter.write(buf, 0, len); } dexWriter.close(); bis.close(); return true; } catch (IOException e) { if (dexWriter != null) { try { dexWriter.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } if (bis != null) { try { bis.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } return false; } }
From source file:fr.pasteque.client.utils.URLTextGetter.java
public static void getText(final String url, final Map<String, String> getParams, final Map<String, String> postParams, final Handler h) { new Thread() { @Override//from w w w.j a v a2 s . c om public void run() { try { String fullUrl = url; if (getParams != null && getParams.size() > 0) { fullUrl += "?"; for (String param : getParams.keySet()) { fullUrl += URLEncoder.encode(param, "utf-8") + "=" + URLEncoder.encode(getParams.get(param), "utf-8") + "&"; } } if (fullUrl.endsWith("&")) { fullUrl = fullUrl.substring(0, fullUrl.length() - 1); } HttpClient client = new DefaultHttpClient(); HttpResponse response = null; if (postParams == null) { HttpGet req = new HttpGet(fullUrl); response = client.execute(req); } else { HttpPost req = new HttpPost(fullUrl); List<NameValuePair> args = new ArrayList<NameValuePair>(); for (String key : postParams.keySet()) { String value = postParams.get(key); args.add(new BasicNameValuePair(key, value)); } UrlEncodedFormEntity entity = new UrlEncodedFormEntity(args, HTTP.UTF_8); req.setEntity(entity); response = client.execute(req); } int status = response.getStatusLine().getStatusCode(); if (status == HttpStatus.SC_OK) { // Get http response String content = ""; try { final int size = 10240; ByteArrayOutputStream bos = new ByteArrayOutputStream(size); byte[] buffer = new byte[size]; BufferedInputStream bis = new BufferedInputStream(response.getEntity().getContent(), size); int read = bis.read(buffer, 0, size); while (read != -1) { bos.write(buffer, 0, read); read = bis.read(buffer, 0, size); } content = new String(bos.toByteArray()); } catch (IOException ioe) { ioe.printStackTrace(); } if (h != null) { Message m = h.obtainMessage(); m.what = SUCCESS; m.obj = content; m.sendToTarget(); } } else { if (h != null) { Message m = h.obtainMessage(); m.what = STATUS_NOK; m.obj = new Integer(status); m.sendToTarget(); } } } catch (IOException e) { if (h != null) { Message m = h.obtainMessage(); m.what = ERROR; m.obj = e; m.sendToTarget(); } } } }.start(); }