org.apache.commons.compress.utils.IOUtils.closeQuietly()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(202)

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

IOUtils.closeQuietly介绍

[英]Closes the given Closeable and swallows any IOException that may occur.
[中]关闭给定的可关闭项并接受任何可能发生的IOException。

代码示例

代码示例来源:origin: org.apache.commons/commons-compress

  1. /**
  2. * close a zipfile quietly; throw no io fault, do nothing
  3. * on a null parameter
  4. * @param zipfile file to close, can be null
  5. */
  6. public static void closeQuietly(final ZipFile zipfile) {
  7. IOUtils.closeQuietly(zipfile);
  8. }

代码示例来源:origin: org.apache.commons/commons-compress

  1. private void closeDecoder() {
  2. closeQuietly(decoder);
  3. decoder = null;
  4. }
  5. }

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

  1. void shutdown() {
  2. if (logWriter != null) {
  3. logWriter.shutdown();
  4. try {
  5. logWriter.awaitTermination(WAIT_TIME, TimeUnit.SECONDS);
  6. } catch (InterruptedException e) {
  7. LOG.warn("Got interrupted exception while waiting for events to be flushed", e);
  8. }
  9. }
  10. IOUtils.closeQuietly(writer);
  11. }

代码示例来源:origin: confluentinc/ksql

  1. @Override
  2. public void close() {
  3. if (closed) {
  4. return;
  5. }
  6. synchronized (this) {
  7. closed = true;
  8. }
  9. responseScanner.close();
  10. response.close();
  11. IOUtils.closeQuietly(isr);
  12. }

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

  1. private void movePcapFileFromCacheToStorage() {
  2. File inputFile = new File(mPcapFileName);
  3. InputStream in = null;
  4. OutputStream out = null;
  5. try {
  6. in = new FileInputStream(inputFile);
  7. out = new FileOutputStream(new File(System.getStoragePath(),new File(mPcapFileName).getName()));
  8. IOUtils.copy(in, out);
  9. } catch (IOException e) {
  10. System.errorLogging(e);
  11. } finally {
  12. IOUtils.closeQuietly(in);
  13. IOUtils.closeQuietly(out);
  14. inputFile.delete();
  15. }
  16. }

代码示例来源:origin: kaaproject/kaa

  1. /**
  2. * Gets the public key from file.
  3. *
  4. * @param file the file
  5. * @return the public
  6. * @throws IOException the i/o exception
  7. * @throws InvalidKeyException invalid key exception
  8. */
  9. public static PublicKey getPublic(File file) throws IOException, InvalidKeyException {
  10. DataInputStream dis = null;
  11. try {
  12. FileInputStream fis = new FileInputStream(file);
  13. dis = new DataInputStream(fis);
  14. byte[] keyBytes = new byte[(int) file.length()];
  15. dis.readFully(keyBytes);
  16. return getPublic(keyBytes);
  17. } finally {
  18. IOUtils.closeQuietly(dis);
  19. }
  20. }

代码示例来源:origin: kaaproject/kaa

  1. /**
  2. * Gets the private key from file.
  3. *
  4. * @param file the file
  5. * @return the private
  6. * @throws IOException the i/o exception
  7. * @throws InvalidKeyException invalid key exception
  8. */
  9. public static PrivateKey getPrivate(File file) throws IOException, InvalidKeyException {
  10. DataInputStream dis = null;
  11. try {
  12. FileInputStream fis = new FileInputStream(file);
  13. dis = new DataInputStream(fis);
  14. byte[] keyBytes = new byte[(int) file.length()];
  15. dis.readFully(keyBytes);
  16. return getPrivate(keyBytes);
  17. } finally {
  18. IOUtils.closeQuietly(dis);
  19. }
  20. }

代码示例来源:origin: ballerina-platform/ballerina-lang

  1. /**
  2. * Terminate running all child processes for a given pid.
  3. *
  4. * @param pid - process id
  5. */
  6. void killChildProcesses(int pid) {
  7. BufferedReader reader = null;
  8. try {
  9. Process findChildProcess = Runtime.getRuntime().exec(String.format("pgrep -P %d", pid));
  10. findChildProcess.waitFor();
  11. reader = new BufferedReader(
  12. new InputStreamReader(findChildProcess.getInputStream(), Charset.defaultCharset()));
  13. String line;
  14. int childProcessID;
  15. while ((line = reader.readLine()) != null) {
  16. childProcessID = Integer.parseInt(line);
  17. kill(childProcessID);
  18. }
  19. } catch (Throwable e) {
  20. LOGGER.error("Launcher was unable to find parent for process:" + pid + ".");
  21. } finally {
  22. if (reader != null) {
  23. IOUtils.closeQuietly(reader);
  24. }
  25. }
  26. }
  27. }

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

  1. /** Open a reader for a file. */
  2. public static <D> FileReader<D> openReader(File file, DatumReader<D> reader)
  3. throws IOException {
  4. SeekableFileInput input = new SeekableFileInput( file );
  5. try {
  6. return openReader( input, reader );
  7. } catch ( final Throwable e ) {
  8. IOUtils.closeQuietly( input );
  9. throw e;
  10. }
  11. }

代码示例来源:origin: ballerina-platform/ballerina-lang

  1. } finally {
  2. if (reader != null) {
  3. IOUtils.closeQuietly(reader);

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

  1. /** Open a new file for data matching a schema with a random sync. */
  2. public DataFileWriter<D> create(Schema schema, File file) throws IOException {
  3. SyncableFileOutputStream sfos = new SyncableFileOutputStream(file);
  4. try {
  5. return create(schema, sfos, null);
  6. } catch (final Throwable e) {
  7. IOUtils.closeQuietly(sfos);
  8. throw e;
  9. }
  10. }

代码示例来源:origin: kaaproject/kaa

  1. /**
  2. * Saves public and private keys to specified files.
  3. *
  4. * @param keyPair the key pair
  5. * @param privateKeyFile the private key file
  6. * @param publicKeyFile the public key file
  7. * @throws IOException Signals that an I/O exception has occurred.
  8. */
  9. public static void saveKeyPair(KeyPair keyPair, String privateKeyFile, String publicKeyFile)
  10. throws IOException {
  11. File privateFile = makeDirs(privateKeyFile);
  12. File publicFile = makeDirs(publicKeyFile);
  13. OutputStream privateKeyOutput = null;
  14. OutputStream publicKeyOutput = null;
  15. try {
  16. privateKeyOutput = new FileOutputStream(privateFile);
  17. publicKeyOutput = new FileOutputStream(publicFile);
  18. saveKeyPair(keyPair, privateKeyOutput, publicKeyOutput);
  19. } finally {
  20. IOUtils.closeQuietly(privateKeyOutput);
  21. IOUtils.closeQuietly(publicKeyOutput);
  22. }
  23. }

代码示例来源:origin: ballerina-platform/ballerina-lang

  1. /**
  2. * Terminate running ballerina program.
  3. */
  4. public void terminate() {
  5. int processID;
  6. String[] findProcessCommand = getFindProcessCommand(processIdentifier);
  7. BufferedReader reader = null;
  8. try {
  9. Process findProcess = Runtime.getRuntime().exec(findProcessCommand);
  10. findProcess.waitFor();
  11. reader = new BufferedReader(new InputStreamReader(findProcess.getInputStream(), Charset.defaultCharset()));
  12. String line;
  13. while ((line = reader.readLine()) != null) {
  14. try {
  15. processID = Integer.parseInt(line);
  16. killChildProcesses(processID);
  17. kill(processID);
  18. } catch (Throwable e) {
  19. LOGGER.error("Launcher was unable to kill process " + line + ".");
  20. }
  21. }
  22. } catch (Throwable e) {
  23. LOGGER.error("Launcher was unable to find the process ID for " + processIdentifier + ".");
  24. } finally {
  25. if (reader != null) {
  26. IOUtils.closeQuietly(reader);
  27. }
  28. }
  29. }

代码示例来源:origin: spotify/docker-client

  1. @Override
  2. public void create(final String image, final InputStream imagePayload,
  3. final ProgressHandler handler)
  4. throws DockerException, InterruptedException {
  5. WebTarget resource = resource().path("images").path("create");
  6. resource = resource
  7. .queryParam("fromSrc", "-")
  8. .queryParam("tag", image);
  9. final CreateProgressHandler createProgressHandler = new CreateProgressHandler(handler);
  10. final Entity<InputStream> entity = Entity.entity(imagePayload,
  11. APPLICATION_OCTET_STREAM);
  12. try {
  13. requestAndTail(POST, createProgressHandler, resource,
  14. resource.request(APPLICATION_JSON_TYPE), entity);
  15. tag(createProgressHandler.getImageId(), image, true);
  16. } finally {
  17. IOUtils.closeQuietly(imagePayload);
  18. }
  19. }

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

  1. private boolean maybeRolloverWriterForDay() throws IOException {
  2. if (writer == null || !logger.getNow().toLocalDate().equals(writerDate)) {
  3. if (writer != null) {
  4. // Day change over case, reset the logFileCount.
  5. logFileCount = 0;
  6. IOUtils.closeQuietly(writer);
  7. writer = null;
  8. }
  9. // increment log file count, if creating a new writer.
  10. writer = logger.getWriter(logFileName + "_" + ++logFileCount);
  11. writerDate = logger.getDateFromDir(writer.getPath().getParent().getName());
  12. return true;
  13. }
  14. return false;
  15. }

代码示例来源:origin: org.apache.commons/commons-compress

  1. /**
  2. * Creates a new ZIP OutputStream writing to a File. Will use
  3. * random access if possible.
  4. * @param file the file to zip to
  5. * @throws IOException on error
  6. */
  7. public ZipArchiveOutputStream(final File file) throws IOException {
  8. def = new Deflater(level, true);
  9. OutputStream o = null;
  10. SeekableByteChannel _channel = null;
  11. StreamCompressor _streamCompressor = null;
  12. try {
  13. _channel = Files.newByteChannel(file.toPath(),
  14. EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.WRITE,
  15. StandardOpenOption.READ,
  16. StandardOpenOption.TRUNCATE_EXISTING));
  17. // will never get opened properly when an exception is thrown so doesn't need to get closed
  18. _streamCompressor = StreamCompressor.create(_channel, def); //NOSONAR
  19. } catch (final IOException e) {
  20. IOUtils.closeQuietly(_channel);
  21. _channel = null;
  22. o = new FileOutputStream(file);
  23. _streamCompressor = StreamCompressor.create(o, def);
  24. }
  25. out = o;
  26. channel = _channel;
  27. streamCompressor = _streamCompressor;
  28. }

代码示例来源:origin: spotify/docker-client

  1. @Override
  2. public Set<String> load(final InputStream imagePayload, final ProgressHandler handler)
  3. throws DockerException, InterruptedException {
  4. final WebTarget resource = resource()
  5. .path("images")
  6. .path("load")
  7. .queryParam("quiet", "false");
  8. final LoadProgressHandler loadProgressHandler = new LoadProgressHandler(handler);
  9. final Entity<InputStream> entity = Entity.entity(imagePayload, APPLICATION_OCTET_STREAM);
  10. try (final ProgressStream load =
  11. request(POST, ProgressStream.class, resource,
  12. resource.request(APPLICATION_JSON_TYPE), entity)) {
  13. load.tail(loadProgressHandler, POST, resource.getUri());
  14. return loadProgressHandler.getImageNames();
  15. } catch (IOException e) {
  16. throw new DockerException(e);
  17. } finally {
  18. IOUtils.closeQuietly(imagePayload);
  19. }
  20. }

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

  1. /** Construct a reader for a file. */
  2. protected DataFileReader(SeekableInput sin, DatumReader<D> reader, boolean closeOnError)
  3. throws IOException {
  4. super(reader);
  5. try {
  6. this.sin = new SeekableInputStream(sin);
  7. initialize(this.sin);
  8. blockFinished();
  9. } catch(final Throwable e) {
  10. if (closeOnError) {
  11. IOUtils.closeQuietly( sin );
  12. }
  13. throw e;
  14. }
  15. }

代码示例来源:origin: org.apache.commons/commons-compress

  1. private ZipFile(final SeekableByteChannel channel, final String archiveName,
  2. final String encoding, final boolean useUnicodeExtraFields,
  3. final boolean closeOnError)
  4. throws IOException {
  5. this.archiveName = archiveName;
  6. this.encoding = encoding;
  7. this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding);
  8. this.useUnicodeExtraFields = useUnicodeExtraFields;
  9. archive = channel;
  10. boolean success = false;
  11. try {
  12. final Map<ZipArchiveEntry, NameAndComment> entriesWithoutUTF8Flag =
  13. populateFromCentralDirectory();
  14. resolveLocalFileHeaderData(entriesWithoutUTF8Flag);
  15. success = true;
  16. } finally {
  17. closed = !success;
  18. if (!success && closeOnError) {
  19. IOUtils.closeQuietly(archive);
  20. }
  21. }
  22. }

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

  1. IOUtils.closeQuietly(writer);
  2. writer = null;
  3. } else {
  4. } catch (IOException e) {
  5. IOUtils.closeQuietly(writer);
  6. writer = null;
  7. if (retryCount < MAX_RETRIES) {

相关文章