BufferedInputStream bis = new BufferedInputStream(inputStream); ByteArrayOutputStream buf = new ByteArrayOutputStream(); int result = bis.read(); while(result != -1) { buf.write((byte) result); result = bis.read(); } return buf.toString();
/** * Loads a log file from a web server into the LogFactor5 GUI. */ private String loadLogFile(InputStream stream) throws IOException { BufferedInputStream br = new BufferedInputStream(stream); int count = 0; int size = br.available(); StringBuffer sb = null; if (size > 0) { sb = new StringBuffer(size); } else { sb = new StringBuffer(1024); } while ((count = br.read()) != -1) { sb.append((char) count); } br.close(); br = null; return sb.toString(); }
BufferedInputStream bis = new BufferedInputStream(entity.getContent()); String filePath = "sample.txt"; BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath))); int inByte; while((inByte = bis.read()) != -1) bos.write(inByte); bis.close(); bos.close();
public static String getCharset1(String fileName) throws IOException { BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fileName)); int p = (bin.read() << 8) + bin.read(); String code; switch (p) { case 0xefbb: code = "UTF-8"; break; case 0xfffe: code = "Unicode"; break; case 0xfeff: code = "UTF-16BE"; break; default: code = "GBK"; } return code; }
BufferedInputStream bis = new BufferedInputStream(is); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); //We create an array of bytes byte[] data = new byte[50]; int current = 0; while((current = bis.read(data,0,data.length)) != -1){ buffer.write(data,0,current); } FileOutputStream fos = new FileOutputStream(file); fos.write(buffer.toByteArray()); fos.close();
public static void download(String source_url, File target_file) { try { ClientConnection connection = new ClientConnection(source_url); try { OutputStream os = new BufferedOutputStream(new FileOutputStream(target_file)); int count; byte[] buffer = new byte[2048]; try { while ((count = connection.inputStream.read(buffer)) > 0) os.write(buffer, 0, count); } catch (IOException e) { DAO.severe(e); } finally { os.close(); } } catch (IOException e) { DAO.severe(e); } finally { connection.close(); } } catch (IOException e) { DAO.severe(e); } }
File file = new File(path); int size = (int) file.length(); byte[] bytes = new byte[size]; try { BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file)); buf.read(bytes, 0, bytes.length); buf.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
boolean running = true; BufferedInputStream reader = new BufferedInputStream(new FileInputStream( "out.txt" ) ); public void run() { while( running ) { if( reader.available() > 0 ) { System.out.print( (char)reader.read() ); } else { try { sleep( 500 ); } catch( InterruptedException ex ) { running = false; } } } }
BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(50); while ((current = bis.read()) != -1) { baf.append((byte) current); } FileOutputStream fos = new FileOutputStream(file); fos.write(buffer.toByteArray());
BufferedInputStream bis = new BufferedInputStream(inputStream); bis.mark(2); int byte1 = bis.read(); int byte2 = bis.read(); bis.reset(); // note: you must continue using the BufferedInputStream instead of the inputStream
private static byte[] computeSHA256Hash(byte[] data) throws NoSuchAlgorithmException, IOException { BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(data)); try { MessageDigest messageDigest = MessageDigest.getInstance("SHA-256"); byte[] buffer = new byte[16384]; int bytesRead = -1; while ( (bytesRead = bis.read(buffer, 0, buffer.length)) != -1 ) { messageDigest.update(buffer, 0, bytesRead); } return messageDigest.digest(); } finally { try { bis.close(); } catch ( Exception e ) {} } } }
private byte[] fileDigest(File file) throws IOException { BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file)); try { byte[] data = new byte[8192]; int read; while ((read = fis.read(data)) > 0) { messageDigest.update(data, 0, read); } } finally { fis.close(); } return messageDigest.digest(); }
@Override byte[] readFile(File file) throws IOException { InputStream is = new FileInputStream(file); BufferedInputStream buff = new BufferedInputStream(is); ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] buffer = new byte[0xFFFF]; int len; while ((len = buff.read(buffer)) > 0) { os.write(buffer, 0, len); } os.flush(); close(buff); return os.toByteArray(); }
@Override public void processStream(InputStream input) throws IOException { BufferedInputStream in = new BufferedInputStream(input); byte[] buffer = new byte[8092]; int len; while ((len = in.read(buffer)) != -1) { if (len > 0) { bytes.write(buffer, 0, len); } } } }
public static int copyAndCloseInputStream(InputStream inputStream, OutputStream outputStream) throws IOException { BufferedInputStream bufferedInputStream = null; int totalBytes = 0; try { bufferedInputStream = new BufferedInputStream(inputStream); byte[] buffer = new byte[8192]; int bytesRead; while ((bytesRead = bufferedInputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); totalBytes += bytesRead; } } finally { if (bufferedInputStream != null) { bufferedInputStream.close(); } if (inputStream != null) { inputStream.close(); } } return totalBytes; }
@Override protected byte[] readValueFromDisk(File file) throws IOException { BufferedInputStream istream = new BufferedInputStream(new FileInputStream(file)); long fileSize = file.length(); if (fileSize > Integer.MAX_VALUE) { throw new IOException("Cannot read files larger than " + Integer.MAX_VALUE + " bytes"); } int imageDataLength = (int) fileSize; byte[] imageData = new byte[imageDataLength]; istream.read(imageData, 0, imageDataLength); istream.close(); return imageData; }
@Override byte[] readFile(File file) throws IOException { InputStream is = new FileInputStream(file); ZipInputStream zis = new ZipInputStream(is); zis.getNextEntry(); BufferedInputStream buff = new BufferedInputStream(zis); ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] buffer = new byte[0xFFFF]; int len; while ((len = buff.read(buffer)) > 0) { os.write(buffer, 0, len); } os.flush(); Helper.close(buff); return os.toByteArray(); }
BufferedInputStream in = new BufferedInputStream(new URL("http://www.website.com/information.asp").openStream()) byte data[] = new byte[1024]; int count; while((count = in.read(data,0,1024)) != -1) { out.write(data, 0, count); }