java.io.BufferedInputStream类的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(10.1k)|赞(0)|评价(0)|浏览(197)

本文整理了Java中java.io.BufferedInputStream类的一些代码示例,展示了BufferedInputStream类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BufferedInputStream类的具体详情如下:
包路径:java.io.BufferedInputStream
类名称:BufferedInputStream

BufferedInputStream介绍

[英]A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time. The mark operation remembers a point in the input stream and the reset operation causes all the bytes read since the most recent mark operation to be reread before new bytes are taken from the contained input stream.
[中]BufferedInputStream为另一个输入流添加了功能,即缓冲输入并支持markreset方法的能力。创建BufferedInputStream时,将创建一个内部缓冲区数组。当读取或跳过流中的字节时,将根据需要从包含的输入流中重新填充内部缓冲区,每次填充多个字节。mark操作会记住输入流中的一个点,reset操作会在从包含的输入流中获取新字节之前,重新读取自最近的mark操作以来读取的所有字节。

代码示例

代码示例来源:origin: stackoverflow.com

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();

代码示例来源:origin: facebook/facebook-android-sdk

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;
}

代码示例来源:origin: stackoverflow.com

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

代码示例来源:origin: stackoverflow.com

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();

代码示例来源:origin: apache/storm

/**
 * 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;
}

代码示例来源:origin: apache/zookeeper

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());
  }
}

代码示例来源:origin: stackoverflow.com

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();

代码示例来源:origin: Tencent/tinker

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();
  }
}

代码示例来源:origin: libgdx/libgdx

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) {
      }
  }
}

代码示例来源:origin: apache/flink

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();

代码示例来源:origin: stackoverflow.com

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);

代码示例来源:origin: ethereum/ethereumj

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;
}

代码示例来源:origin: stackoverflow.com

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();

代码示例来源:origin: stackoverflow.com

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();
  }
}

代码示例来源:origin: cSploit/android

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;
}

代码示例来源:origin: stackoverflow.com

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();
 }
}

代码示例来源:origin: loklak/loklak_server

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();
}

代码示例来源:origin: k9mail/k-9

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);
}

代码示例来源:origin: spring-projects/spring-loaded

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);
  }
}

代码示例来源:origin: redisson/redisson

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();
}

相关文章