public static byte[] isToBytes(InputStream is) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int read; try { while ((read = is.read(buffer)) != -1) { os.write(buffer, 0, read); } } finally { is.close(); } return os.toByteArray(); }
public void copy(File src, File dst) throws IOException { InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dst); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); }
File f = new File(getCacheDir()+"/m1.map"); if (!f.exists()) try { InputStream is = getAssets().open("m1.map"); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); FileOutputStream fos = new FileOutputStream(f); fos.write(buffer); fos.close(); } catch (Exception e) { throw new RuntimeException(e); } mapView.setMapFile(f.getPath());
@Test public void nonClosingInputStream() throws Exception { InputStream source = mock(InputStream.class); InputStream nonClosing = StreamUtils.nonClosing(source); nonClosing.read(); nonClosing.read(bytes); nonClosing.read(bytes, 1, 2); nonClosing.close(); InOrder ordered = inOrder(source); ordered.verify(source).read(); ordered.verify(source).read(bytes, 0, bytes.length); ordered.verify(source).read(bytes, 1, 2); ordered.verify(source, never()).close(); }
static void copy(InputStream in, File dst) throws IOException { FileOutputStream out=new FileOutputStream(dst); byte[] buf=new byte[1024]; int len; while ((len=in.read(buf)) >= 0) { out.write(buf, 0, len); } in.close(); out.close(); } }
private static File copyResourceToFile(String resourcePath) throws IOException { if (tempDir == null){ File tempFile = File.createTempFile("prefix", "suffix"); tempFile.deleteOnExit(); tempDir = tempFile.getParentFile(); } InputStream jarIn = SdkStore.class.getClassLoader().getResourceAsStream(resourcePath); File outFile = new File(tempDir, new File(resourcePath).getName()); outFile.deleteOnExit(); try (FileOutputStream jarOut = new FileOutputStream(outFile)) { byte[] buffer = new byte[4096]; int len; while ((len = jarIn.read(buffer)) != -1) { jarOut.write(buffer, 0, len); } } return outFile; }
public static void copyFile(InputStream from, File to) throws IOException { if (! to.getParentFile().exists()) { to.getParentFile().mkdirs(); } try (OutputStream os = new FileOutputStream(to)) { byte[] buffer = new byte[65536]; int count = from.read(buffer); while (count > 0) { os.write(buffer, 0, count); count = from.read(buffer); } } }
public static String toString(InputStream inputStream) throws IOException { ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { result.write(buffer, 0, length); } return result.toString("UTF-8"); }
public static void copyResourceUsingStream(String name, File dest) throws IOException { FileOutputStream os = null; File parent = dest.getParentFile(); if (parent != null && (!parent.exists())) { parent.mkdirs(); } InputStream is = null; try { is = FileOperation.class.getResourceAsStream("/" + name); os = new FileOutputStream(dest, false); byte[] buffer = new byte[TypedValue.BUFFER_SIZE]; int length; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } } finally { StreamUtil.closeQuietly(os); StreamUtil.closeQuietly(is); } }
@Override public void run() { try { try { byte[] buf = new byte[8192]; int len; while ((len = in.read(buf)) >= 0) out.write(buf, 0, len); } finally { // it doesn't make sense not to close InputStream that's already EOF-ed, // so there's no 'closeIn' flag. in.close(); if(closeOut) out.close(); } } catch (IOException e) { // TODO: what to do? } } }
private static byte[] readAllBytes(InputStream input) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); int numRead; byte[] buffer = new byte[16384]; while ((numRead = input.read(buffer, 0, buffer.length)) != -1) { out.write(buffer, 0, numRead); } return out.toByteArray(); }
InputStream is = entity.getContent(); String filePath = "sample.txt"; FileOutputStream fos = new FileOutputStream(new File(filePath)); int inByte; while((inByte = is.read()) != -1) fos.write(inByte); is.close(); fos.close();
@GwtIncompatible // Reader private static void testStreamingDecodes(BaseEncoding encoding, String encoded, String decoded) throws IOException { byte[] bytes = decoded.getBytes(UTF_8); InputStream decodingStream = encoding.decodingStream(new StringReader(encoded)); for (int i = 0; i < bytes.length; i++) { assertThat(decodingStream.read()).isEqualTo(bytes[i] & 0xFF); } assertThat(decodingStream.read()).isEqualTo(-1); decodingStream.close(); }
static void copy(InputStream in, File dst) throws IOException { FileOutputStream out=new FileOutputStream(dst); byte[] buf=new byte[1024]; int len; while ((len=in.read(buf)) >= 0) { out.write(buf, 0, len); } in.close(); out.close(); } }
public void publicizeResources(File arscFile) throws AndrolibException { byte[] data = new byte[(int) arscFile.length()]; try(InputStream in = new FileInputStream(arscFile); OutputStream out = new FileOutputStream(arscFile)) { in.read(data); publicizeResources(data); out.write(data); } catch (IOException ex){ throw new AndrolibException(ex); } }
public void run() { try { byte[] b = new byte[bufferSize]; int bytesRead; while ((bytesRead = inStream.read(b)) >= 0) { if (bytesRead > 0) { outStream.write(b, 0, bytesRead); } } inStream.close(); } catch (Exception ex) { System.out.println("Problem reading stream :"+inStream.getClass().getCanonicalName()+ " "+ ex); ex.printStackTrace(); } } }