/** * 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();
/** * Returns the digest for the file. * * @param valueToDigest the file to use * @return the digest * @throws IOException * If an I/O error occurs. * @since 1.11 */ public byte[] hmac(final File valueToDigest) throws IOException { final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(valueToDigest)); try { return hmac(stream); } finally { stream.close(); } }
/** * Determine the content generator (i.e. Slic3r, Skeinforge) for the given resource. * * @param res * @param resId * @return * @throws IOException */ public static final GCodeFlavor tasteFlavor(Resources res, int resId) throws IOException, NotFoundException { BufferedInputStream buffer = new BufferedInputStream(res.openRawResource(resId)); GCodeFlavor ret = tasteFlavor(buffer); buffer.close(); return ret; }
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(); }
/** * Reads through a File and updates the digest for the data * * @param digest * The MessageDigest to use (e.g. MD5) * @param data * Data to digest * @return the digest * @throws IOException * On error reading from the stream * @since 1.11 */ public static MessageDigest updateDigest(final MessageDigest digest, final File data) throws IOException { final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(data)); try { return updateDigest(digest, stream); } finally { stream.close(); } }
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 ResPackage[] getResPackagesFromApk(ExtFile apkFile,ResTable resTable, boolean keepBroken) throws AndrolibException { try { Directory dir = apkFile.getDirectory(); BufferedInputStream bfi = new BufferedInputStream(dir.getFileInput("resources.arsc")); try { return ARSCDecoder.decode(bfi, false, keepBroken, resTable).getPackages(); } finally { try { bfi.close(); } catch (IOException ignored) {} } } catch (DirectoryException ex) { throw new AndrolibException("Could not load resources.arsc from file: " + apkFile, ex); } }
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(); }
public static byte[] md5Hash(final File file) throws IOException { final MessageDigest digest = getMd5Digest(); final FileInputStream fStream = new FileInputStream(file); final BufferedInputStream bStream = new BufferedInputStream(fStream); final DigestInputStream blobStream = new DigestInputStream(bStream, digest); final byte[] buffer = new byte[BYTE_BUFFER_SIZE]; int num = 0; do { num = blobStream.read(buffer); } while (num > 0); bStream.close(); return digest.digest(); }
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; }
/** * Loads a classifier from the file specified. If the file's name ends in .gz, * uses a GZIPInputStream, else uses a regular FileInputStream. This method * closes the File when done. * * @param file Loads a classifier from this file. * @param props Properties in this object will be used to overwrite those * specified in the serialized classifier * * @throws IOException If there are problems accessing the input stream * @throws ClassCastException If there are problems interpreting the serialized data * @throws ClassNotFoundException If there are problems interpreting the serialized data */ public void loadClassifier(File file, Properties props) throws ClassCastException, IOException, ClassNotFoundException { Timing t = new Timing(); BufferedInputStream bis; if (file.getName().endsWith(".gz")) { bis = new BufferedInputStream(new GZIPInputStream(new FileInputStream(file))); } else { bis = new BufferedInputStream(new FileInputStream(file)); } try { loadClassifier(bis, props); t.done(log, "Loading classifier from " + file.getAbsolutePath()); } finally { bis.close(); } }
public static byte[] loadBytesFromStream(InputStream stream) { try { BufferedInputStream bis = new BufferedInputStream(stream); byte[] theData = new byte[10000000]; int dataReadSoFar = 0; byte[] buffer = new byte[1024]; int read = 0; while ((read = bis.read(buffer)) != -1) { System.arraycopy(buffer, 0, theData, dataReadSoFar, read); dataReadSoFar += read; } bis.close(); // Resize to actual data read byte[] returnData = new byte[dataReadSoFar]; System.arraycopy(theData, 0, returnData, 0, dataReadSoFar); return returnData; } catch (IOException e) { throw new RuntimeException(e); } }
public void saveUrl(final String filename, final String urlString) throws MalformedURLException, IOException { BufferedInputStream in = null; FileOutputStream fout = null; try { in = new BufferedInputStream(new URL(urlString).openStream()); fout = new FileOutputStream(filename); final byte data[] = new byte[1024]; int count; while ((count = in.read(data, 0, 1024)) != -1) { fout.write(data, 0, count); } } finally { if (in != null) { in.close(); } if (fout != null) { fout.close(); } } }
zipEntry.setMethod(ZipEntry.STORED); zipEntry.setSize(file.length()); BufferedInputStream unknownFile = new BufferedInputStream(new FileInputStream(file)); CRC32 crc = BrutIO.calculateCrc(unknownFile); zipEntry.setCrc(crc.getValue()); unknownFile.close(); } else { zipEntry.setMethod(ZipEntry.DEFLATED); try (FileInputStream inputStream = new FileInputStream(file)) { IOUtils.copy(inputStream, zipOutputStream);
public static byte[] loadBytesFromStream(InputStream stream) { try { BufferedInputStream bis = new BufferedInputStream(stream); byte[] theData = new byte[10000000]; int dataReadSoFar = 0; byte[] buffer = new byte[1024]; int read = 0; while ((read = bis.read(buffer)) != -1) { System.arraycopy(buffer, 0, theData, dataReadSoFar, read); dataReadSoFar += read; } bis.close(); // Resize to actual data read byte[] returnData = new byte[dataReadSoFar]; System.arraycopy(theData, 0, returnData, 0, dataReadSoFar); return returnData; } catch (IOException e) { throw new RuntimeException(e); } }
private static byte[] getFileBytes(File file) throws IOException { BufferedInputStream bis = null; try { bis = new BufferedInputStream(new FileInputStream(file)); int bytes = (int) file.length(); byte[] buffer = new byte[bytes]; int readBytes = bis.read(buffer); if (readBytes != buffer.length) { throw new IOException("Entire file not read"); } return buffer; } finally { if (bis != null) { bis.close(); } } }