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

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

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

InputStream.close介绍

[英]Closes this stream. Concrete implementations of this class should free any resources during close. This implementation does nothing.
[中]关闭此流。此类的具体实现应该在关闭期间释放所有资源。这个实现什么都不做。

代码示例

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

InputStream in;
OutputStream out;
IOUtils.copy(in,out);
in.close();
out.close();

代码示例来源: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: 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

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: iBotPeaches/Apktool

public static void cpdir(File src, File dest) throws BrutException {
  dest.mkdirs();
  File[] files = src.listFiles();
  for (int i = 0; i < files.length; i++) {
    File file = files[i];
    File destFile = new File(dest.getPath() + File.separatorChar
      + file.getName());
    if (file.isDirectory()) {
      cpdir(file, destFile);
      continue;
    }
    try {
      InputStream in = new FileInputStream(file);
      OutputStream out = new FileOutputStream(destFile);
      IOUtils.copy(in, out);
      in.close();
      out.close();
    } catch (IOException ex) {
      throw new BrutException("Could not copy file: " + file, ex);
    }
  }
}

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

@Override
  public void write(OutputStream output) throws IOException {
    InputStream input = null;
    try {
      input = new FileInputStream(new File(configFileStr));
      byte[] buf = new byte[1024];
      int bytesRead;
      while ((bytesRead = input.read(buf)) > 0) {
        output.write(buf, 0, bytesRead);
      }
    } finally {
      if( input != null) {
        input.close();
      }
    }
  }
});

代码示例来源:origin: apache/incubator-dubbo

/**
 * get md5.
 *
 * @param file file source.
 * @return MD5 byte array.
 */
public static byte[] getMD5(File file) throws IOException {
  InputStream is = new FileInputStream(file);
  try {
    return getMD5(is);
  } finally {
    is.close();
  }
}

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

/**
 * {@inheritDoc}
 */
public Manifest getManifest() throws IOException {
  File file = new File(folder, JarFile.MANIFEST_NAME);
  if (file.exists()) {
    InputStream inputStream = new FileInputStream(file);
    try {
      return new Manifest(inputStream);
    } finally {
      inputStream.close();
    }
  } else {
    return NO_MANIFEST;
  }
}

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

static private 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: pxb1988/dex2jar

public static byte[] readFile(File in) throws IOException {
  InputStream is = new FileInputStream(in);
  byte[] xml = new byte[is.available()];
  is.read(xml);
  is.close();
  return xml;
}

代码示例来源:origin: hs-web/hsweb-framework

@Override
public String saveStaticFile(InputStream fileStream, String fileName) throws IOException {
  try {
    //文件后缀
    String suffix = fileName.contains(".") ?
        fileName.substring(fileName.lastIndexOf(".")) : "";
    //以日期划分目录
    String filePath = DateFormatter.toString(new Date(), "yyyyMMdd");
    //创建目录
    new File(getStaticFilePath() + "/" + filePath).mkdirs();
    // 存储的文件名
    String realFileName = System.nanoTime() + suffix;
    String fileAbsName = getStaticFilePath() + "/" + filePath + "/" + realFileName;
    try (FileOutputStream out = new FileOutputStream(fileAbsName)) {
      StreamUtils.copy(fileStream, out);
    }
    //响应上传成功的资源信息
    return getStaticLocation() + filePath + "/" + realFileName;
  } finally {
    fileStream.close();
  }
}

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

private void addTestDex() throws IOException {
  //write test dex
  String dexMode = "jar";
  if (config.mDexRaw) {
    dexMode = "raw";
  }
  final InputStream is = DexDiffDecoder.class.getResourceAsStream("/" + TEST_DEX_NAME);
  String md5 = MD5.getMD5(is, 1024);
  is.close();
  String meta = TEST_DEX_NAME + "," + "" + "," + md5 + "," + md5 + "," + 0 + "," + 0 + "," + 0 + "," + dexMode;
  File dest = new File(config.mTempResultDir + "/" + TEST_DEX_NAME);
  FileOperation.copyResourceUsingStream(TEST_DEX_NAME, dest);
  Logger.d("\nAdd test install result dex: %s, size:%d", dest.getAbsolutePath(), dest.length());
  Logger.d("DexDecoder:write test dex meta file data: %s", meta);
  metaWriter.writeLineToInfoFile(meta);
}

代码示例来源: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: iBotPeaches/Apktool

public static File extractToTmp(String resourcePath, String tmpPrefix, Class clazz) throws BrutException {
    try {
      InputStream in = clazz.getResourceAsStream(resourcePath);
      if (in == null) {
        throw new FileNotFoundException(resourcePath);
      }
      File fileOut = File.createTempFile(tmpPrefix, null);
      fileOut.deleteOnExit();
      OutputStream out = new FileOutputStream(fileOut);
      IOUtils.copy(in, out);
      in.close();
      out.close();
      return fileOut;
    } catch (IOException ex) {
      throw new BrutException("Could not extract resource: " + resourcePath, ex);
    }
  }
}

代码示例来源: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: 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: prestodb/presto

private void writeZone(File outputDir, DateTimeZoneBuilder builder, DateTimeZone tz) throws IOException {
  if (ZoneInfoLogger.verbose()) {
    System.out.println("Writing " + tz.getID());
  }
  File file = new File(outputDir, tz.getID());
  if (!file.getParentFile().exists()) {
    file.getParentFile().mkdirs();
  }
  OutputStream out = new FileOutputStream(file);
  try {
    builder.writeTo(tz.getID(), out);
  } finally {
    out.close();
  }
  // Test if it can be read back.
  InputStream in = new FileInputStream(file);
  DateTimeZone tz2 = DateTimeZoneBuilder.readFrom(in, tz.getID());
  in.close();
  if (!tz.equals(tz2)) {
    System.out.println("*e* Error in " + tz.getID() +
              ": Didn't read properly from file");
  }
}

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

private static void skipHelper(long n, int expect, InputStream in) throws IOException {
 ByteStreams.skipFully(in, n);
 assertEquals(expect, in.read());
 in.close();
}

代码示例来源:origin: apache/incubator-dubbo

/**
 * get md5.
 *
 * @param file file source.
 * @return MD5 byte array.
 */
public static byte[] getMD5(File file) throws IOException {
  InputStream is = new FileInputStream(file);
  try {
    return getMD5(is);
  } finally {
    is.close();
  }
}

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

/**
 * {@inheritDoc}
 */
public Resolution locate(String name) throws IOException {
  File file = new File(folder, name.replace('.', File.separatorChar) + CLASS_FILE_EXTENSION);
  if (file.exists()) {
    InputStream inputStream = new FileInputStream(file);
    try {
      return new Resolution.Explicit(StreamDrainer.DEFAULT.drain(inputStream));
    } finally {
      inputStream.close();
    }
  } else {
    return new Resolution.Illegal(name);
  }
}

相关文章