java.io.InputStream.read()方法的使用及代码示例

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

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

InputStream.read介绍

[英]Reads a single byte from this stream and returns it as an integer in the range from 0 to 255. Returns -1 if the end of the stream has been reached. Blocks until one byte has been read, the end of the source stream is detected or an exception is thrown.
[中]从该流中读取单个字节,并将其作为0到255范围内的整数返回。如果已到达流的结尾,则返回-1。块,直到读取了一个字节,检测到源流的结尾或引发异常。

代码示例

代码示例来源:origin: skylot/jadx

public static void copyStream(InputStream input, OutputStream output) throws IOException {
  byte[] buffer = new byte[READ_BUFFER_SIZE];
  while (true) {
    int count = input.read(buffer);
    if (count == -1) {
      break;
    }
    output.write(buffer, 0, count);
  }
}

代码示例来源:origin: bumptech/glide

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

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

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

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

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

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

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

代码示例来源:origin: commonsguy/cw-omnibus

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

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

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

代码示例来源:origin: org.testng/testng

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

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

public static void copy(InputStream in, OutputStream out) throws IOException {
 byte[] buffer = new byte[8196];
 int len;
 try {
  while ((len = in.read(buffer)) != -1) {
   out.write(buffer, 0, len);
  }
 } finally {
  in.close();
 }
}

代码示例来源:origin: alibaba/arthas

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

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

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

代码示例来源:origin: jenkinsci/jenkins

@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?
    }
  }
}

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

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

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

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

代码示例来源:origin: google/guava

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

代码示例来源:origin: commonsguy/cw-omnibus

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

代码示例来源:origin: alibaba/druid

public static long copy(InputStream input, OutputStream output) throws IOException {
  final int EOF = -1;
  byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
  long count = 0;
  int n = 0;
  while (EOF != (n = input.read(buffer))) {
    output.write(buffer, 0, n);
    count += n;
  }
  return count;
}

代码示例来源:origin: iBotPeaches/Apktool

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

代码示例来源:origin: stanfordnlp/CoreNLP

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

代码示例来源:origin: jenkinsci/jenkins

@Override
  public void run() {
    try {
      try {
        byte[] buf = new byte[8192];
        int len;
        while ((len = in.read(buf)) >= 0) {
          out.write(buf, 0, len);
          out.flush();
        }
      } finally {
        in.close();
        out.close();
      }
    } catch (IOException e) {
      // TODO: what to do?
    }
  }
}

相关文章