ResultReceiver receiver = (ResultReceiver) intent.getParcelableExtra("receiver"); try { URL url = new URL(urlToDownload); URLConnection connection = url.openConnection(); connection.connect(); int fileLength = connection.getContentLength(); InputStream input = new BufferedInputStream(connection.getInputStream()); OutputStream output = new FileOutputStream("/sdcard/BarcodeScanner-debug.apk"); while ((count = input.read(data)) != -1) { total += count; resultData.putInt("progress" ,(int) (total * 100 / fileLength)); receiver.send(UPDATE_PROGRESS, resultData); output.write(data, 0, count); output.flush(); output.close(); input.close(); } catch (IOException e) { e.printStackTrace();
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; }
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
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();
/** * Tries once to read ahead in the stream to fill the context and * resets the stream to its position before the call. */ private byte[] tryReadAhead(BufferedInputStream stream, ByteBuffer haystack, int offset, int fileLength, int bytesRead) throws IOException { int numExpected = Math.min(fileLength - bytesRead, GREP_CONTEXT_SIZE); byte[] afterBytes = new byte[numExpected]; stream.mark(numExpected); // Only try reading once. stream.read(afterBytes, 0, numExpected); stream.reset(); return afterBytes; }
public void chop() { File targetFile = new File(txnLogFile.getParentFile(), txnLogFile.getName() + ".chopped" + zxid); try ( InputStream is = new BufferedInputStream(new FileInputStream(txnLogFile)); OutputStream os = new BufferedOutputStream(new FileOutputStream(targetFile)) ) { if (!LogChopper.chop(is, os, zxid)) { throw new TxnLogToolkitException(ExitCode.INVALID_INVOCATION.getValue(), "Failed to chop %s", txnLogFile.getName()); } } catch (Exception e) { System.out.println("Got exception: " + e.getMessage()); } }
InputStream reader = new BufferedInputStream( object.getObjectContent()); File file = new File("localFilename"); OutputStream writer = new BufferedOutputStream(new FileOutputStream(file)); int read = -1; while ( ( read = reader.read() ) != -1 ) { writer.write(read); } writer.flush(); writer.close(); reader.close();
public static void bsdiff(File oldFile, File newFile, File diffFile) throws IOException { InputStream oldInputStream = new BufferedInputStream(new FileInputStream(oldFile)); InputStream newInputStream = new BufferedInputStream(new FileInputStream(newFile)); OutputStream diffOutputStream = new FileOutputStream(diffFile); try { byte[] diffBytes = bsdiff(oldInputStream, (int) oldFile.length(), newInputStream, (int) newFile.length()); diffOutputStream.write(diffBytes); } finally { diffOutputStream.close(); } }
public SetupPreferences () { if (!file.exists()) return; InputStream in = null; try { in = new BufferedInputStream(new FileInputStream(file)); properties.loadFromXML(in); } catch (Throwable t) { t.printStackTrace(); } finally { if (in != null) try { in.close(); } catch (IOException e) { } } }
jar = new JarFile(new File(jarFile.toURI())); final List<JarEntry> containedJarFileEntries = new ArrayList<JarEntry>(); try { out = new FileOutputStream(tempFile); in = new BufferedInputStream(jar.getInputStream(entry)); while ((numRead = in.read(buffer)) != -1) { out.write(buffer, 0, numRead); out.close(); in.close();
byte[] imageRaw = null; try { URL url = new URL("http://some.domain.tld/somePicture.jpg"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); InputStream in = new BufferedInputStream(urlConnection.getInputStream()); ByteArrayOutputStream out = new ByteArrayOutputStream(); int c; while ((c = in.read()) != -1) { out.write(c); } out.flush(); imageRaw = out.toByteArray(); urlConnection.disconnect(); in.close(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String image64 = Base64.encodeToString(imageRaw, Base64.DEFAULT); String urlStr = "http://example.com/my.jpg"; String mimeType = "text/html"; String encoding = null; String pageData = "<img src=\"data:image/jpeg;base64," + image64 + "\" />"; WebView wv; wv = (WebView) findViewById(R.id.webview); wv.loadDataWithBaseURL(urlStr, pageData, mimeType, encoding, urlStr);
private String postQuery(String endPoint, String json) throws IOException { URL url = new URL(endPoint); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); OutputStream os = conn.getOutputStream(); os.write(json.getBytes("UTF-8")); os.close(); // read the response InputStream in = new BufferedInputStream(conn.getInputStream()); String result = null; try (Scanner scanner = new Scanner(in, "UTF-8")) { result = scanner.useDelimiter("\\A").next(); } in.close(); conn.disconnect(); return result; }
URL url = new URL("http://www.yahoo.com/image_to_read.jpg"); InputStream in = new BufferedInputStream(url.openStream()); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int n = 0; while (-1!=(n=in.read(buf))) { out.write(buf, 0, n); } out.close(); in.close(); byte[] response = out.toByteArray();
public static int countLines(String filename) throws IOException { InputStream is = new BufferedInputStream(new FileInputStream(filename)); try { byte[] c = new byte[1024]; int count = 0; int readChars = 0; boolean empty = true; while ((readChars = is.read(c)) != -1) { empty = false; for (int i = 0; i < readChars; ++i) { if (c[i] == '\n') { ++count; } } } return (count == 0 && !empty) ? 1 : count; } finally { is.close(); } }
private Bitmap getUserImage(String uri) { Bitmap image = null; try { URL url = new URL(uri); URLConnection conn = url.openConnection(); conn.connect(); InputStream input = conn.getInputStream(); BufferedInputStream reader = new BufferedInputStream(input); image = Bitmap.createScaledBitmap( BitmapFactory.decodeStream(reader), 48, 48, false); reader.close(); input.close(); } catch (IOException e) { System.errorLogging(e); } return image; }
URL url = new URL("http://www.android.com/"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); try { InputStream in = new BufferedInputStream(urlConnection.getInputStream()); readStream(in); finally { urlConnection.disconnect(); } }
public static void gunzip(File source, File dest, boolean deleteSource) throws IOException { byte[] buffer = new byte[2^20]; FileOutputStream out = new FileOutputStream(dest); GZIPInputStream in = new GZIPInputStream(new BufferedInputStream(new FileInputStream(source))); int l; while ((l = in.read(buffer)) > 0) out.write(buffer, 0, l); in.close(); out.close(); if (deleteSource && dest.exists()) source.delete(); }
private void copyAttachmentFromFile(String resourceName, int attachmentId, int expectedFilesize) throws IOException { File resourceFile = new File(getClass().getResource("/attach/" + resourceName).getFile()); File attachmentFile = new File(attachmentDir, Integer.toString(attachmentId)); BufferedInputStream input = new BufferedInputStream(new FileInputStream(resourceFile)); BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(attachmentFile)); int copied = IOUtils.copy(input, output); input.close(); output.close(); Assert.assertEquals(expectedFilesize, copied); }
protected void copyFile(File from, File to) { try { FileInputStream fis = new FileInputStream(from); FileOutputStream fos = new FileOutputStream(to); BufferedInputStream bis = new BufferedInputStream(fis); BufferedOutputStream bos = new BufferedOutputStream(fos); byte[] buffer = new byte[4096]; int len; while ((len = bis.read(buffer, 0, 4096)) != -1) { bos.write(buffer, 0, len); } bis.close(); bos.close(); } catch (IOException ioe) { throw new RuntimeException("Copy file failed", ioe); } }
final void process(Socket clnt) throws IOException { InputStream in = new BufferedInputStream(clnt.getInputStream()); String cmd = readLine(in); logging(clnt.getInetAddress().getHostName(), new Date().toString(), cmd); while (skipLine(in) > 0){ } OutputStream out = new BufferedOutputStream(clnt.getOutputStream()); try { doReply(in, out, cmd); } catch (BadHttpRequest e) { replyError(out, e); } out.flush(); in.close(); out.close(); clnt.close(); }