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

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

本文整理了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

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

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

private void closeDecoder() {
    closeQuietly(decoder);
    decoder = null;
  }
}

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

void shutdown() {
 if (logWriter != null) {
  logWriter.shutdown();
  try {
   logWriter.awaitTermination(WAIT_TIME, TimeUnit.SECONDS);
  } catch (InterruptedException e) {
   LOG.warn("Got interrupted exception while waiting for events to be flushed", e);
  }
 }
 IOUtils.closeQuietly(writer);
}

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

@Override
public void close() {
 if (closed) {
  return;
 }
 synchronized (this) {
  closed = true;
 }
 responseScanner.close();
 response.close();
 IOUtils.closeQuietly(isr);
}

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

private void movePcapFileFromCacheToStorage() {
 File inputFile = new File(mPcapFileName);
 InputStream in = null;
 OutputStream out = null;
 try {
  in = new FileInputStream(inputFile);
  out = new FileOutputStream(new File(System.getStoragePath(),new File(mPcapFileName).getName()));
  IOUtils.copy(in, out);
 } catch (IOException e) {
  System.errorLogging(e);
 } finally {
  IOUtils.closeQuietly(in);
  IOUtils.closeQuietly(out);
  inputFile.delete();
 }
}

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

/**
 * Gets the public key from file.
 *
 * @param file the file
 * @return the public
 * @throws IOException         the i/o exception
 * @throws InvalidKeyException invalid key exception
 */
public static PublicKey getPublic(File file) throws IOException, InvalidKeyException {
 DataInputStream dis = null;
 try {
  FileInputStream fis = new FileInputStream(file);
  dis = new DataInputStream(fis);
  byte[] keyBytes = new byte[(int) file.length()];
  dis.readFully(keyBytes);
  return getPublic(keyBytes);
 } finally {
  IOUtils.closeQuietly(dis);
 }
}

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

/**
 * Gets the private key from file.
 *
 * @param file the file
 * @return the private
 * @throws IOException         the i/o exception
 * @throws InvalidKeyException invalid key exception
 */
public static PrivateKey getPrivate(File file) throws IOException, InvalidKeyException {
 DataInputStream dis = null;
 try {
  FileInputStream fis = new FileInputStream(file);
  dis = new DataInputStream(fis);
  byte[] keyBytes = new byte[(int) file.length()];
  dis.readFully(keyBytes);
  return getPrivate(keyBytes);
 } finally {
  IOUtils.closeQuietly(dis);
 }
}

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

/**
   * Terminate running all child processes for a given pid.
   *
   * @param pid - process id
   */
  void killChildProcesses(int pid) {
    BufferedReader reader = null;
    try {
      Process findChildProcess = Runtime.getRuntime().exec(String.format("pgrep -P %d", pid));
      findChildProcess.waitFor();
      reader = new BufferedReader(
          new InputStreamReader(findChildProcess.getInputStream(), Charset.defaultCharset()));
      String line;
      int childProcessID;
      while ((line = reader.readLine()) != null) {
        childProcessID = Integer.parseInt(line);
        kill(childProcessID);
      }
    } catch (Throwable e) {
      LOGGER.error("Launcher was unable to find parent for process:" + pid + ".");
    } finally {
      if (reader != null) {
        IOUtils.closeQuietly(reader);
      }
    }
  }
}

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

/** Open a reader for a file. */
public static <D> FileReader<D> openReader(File file, DatumReader<D> reader)
 throws IOException {
 SeekableFileInput input = new SeekableFileInput( file );
 try {
  return openReader( input, reader );
 } catch ( final Throwable e ) {
  IOUtils.closeQuietly( input );
  throw e;
 }
}

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

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

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

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

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

/**
 * Saves public and private keys to specified files.
 *
 * @param keyPair        the key pair
 * @param privateKeyFile the private key file
 * @param publicKeyFile  the public key file
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void saveKeyPair(KeyPair keyPair, String privateKeyFile, String publicKeyFile)
    throws IOException {
 File privateFile = makeDirs(privateKeyFile);
 File publicFile = makeDirs(publicKeyFile);
 OutputStream privateKeyOutput = null;
 OutputStream publicKeyOutput = null;
 try {
  privateKeyOutput = new FileOutputStream(privateFile);
  publicKeyOutput = new FileOutputStream(publicFile);
  saveKeyPair(keyPair, privateKeyOutput, publicKeyOutput);
 } finally {
  IOUtils.closeQuietly(privateKeyOutput);
  IOUtils.closeQuietly(publicKeyOutput);
 }
}

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

/**
 * Terminate running ballerina program.
 */
public void terminate() {
  int processID;
  String[] findProcessCommand = getFindProcessCommand(processIdentifier);
  BufferedReader reader = null;
  try {
    Process findProcess = Runtime.getRuntime().exec(findProcessCommand);
    findProcess.waitFor();
    reader = new BufferedReader(new InputStreamReader(findProcess.getInputStream(), Charset.defaultCharset()));
    String line;
    while ((line = reader.readLine()) != null) {
      try {
        processID = Integer.parseInt(line);
        killChildProcesses(processID);
        kill(processID);
      } catch (Throwable e) {
        LOGGER.error("Launcher was unable to kill process " + line + ".");
      }
    }
  } catch (Throwable e) {
    LOGGER.error("Launcher was unable to find the process ID for " + processIdentifier + ".");
  } finally {
    if (reader != null) {
      IOUtils.closeQuietly(reader);
    }
  }
}

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

@Override
public void create(final String image, final InputStream imagePayload,
          final ProgressHandler handler)
  throws DockerException, InterruptedException {
 WebTarget resource = resource().path("images").path("create");
 resource = resource
   .queryParam("fromSrc", "-")
   .queryParam("tag", image);
 final CreateProgressHandler createProgressHandler = new CreateProgressHandler(handler);
 final Entity<InputStream> entity = Entity.entity(imagePayload,
                          APPLICATION_OCTET_STREAM);
 try {
  requestAndTail(POST, createProgressHandler, resource,
      resource.request(APPLICATION_JSON_TYPE), entity);
  tag(createProgressHandler.getImageId(), image, true);
 } finally {
  IOUtils.closeQuietly(imagePayload);
 }
}

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

private boolean maybeRolloverWriterForDay() throws IOException {
 if (writer == null || !logger.getNow().toLocalDate().equals(writerDate)) {
  if (writer != null) {
   // Day change over case, reset the logFileCount.
   logFileCount = 0;
   IOUtils.closeQuietly(writer);
   writer = null;
  }
  // increment log file count, if creating a new writer.
  writer = logger.getWriter(logFileName + "_" + ++logFileCount);
  writerDate = logger.getDateFromDir(writer.getPath().getParent().getName());
  return true;
 }
 return false;
}

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

/**
 * Creates a new ZIP OutputStream writing to a File.  Will use
 * random access if possible.
 * @param file the file to zip to
 * @throws IOException on error
 */
public ZipArchiveOutputStream(final File file) throws IOException {
  def = new Deflater(level, true);
  OutputStream o = null;
  SeekableByteChannel _channel = null;
  StreamCompressor _streamCompressor = null;
  try {
    _channel = Files.newByteChannel(file.toPath(),
      EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.WRITE,
            StandardOpenOption.READ,
            StandardOpenOption.TRUNCATE_EXISTING));
    // will never get opened properly when an exception is thrown so doesn't need to get closed
    _streamCompressor = StreamCompressor.create(_channel, def); //NOSONAR
  } catch (final IOException e) {
    IOUtils.closeQuietly(_channel);
    _channel = null;
    o = new FileOutputStream(file);
    _streamCompressor = StreamCompressor.create(o, def);
  }
  out = o;
  channel = _channel;
  streamCompressor = _streamCompressor;
}

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

@Override
public Set<String> load(final InputStream imagePayload, final ProgressHandler handler)
  throws DockerException, InterruptedException {
 final WebTarget resource = resource()
     .path("images")
     .path("load")
     .queryParam("quiet", "false");
 final LoadProgressHandler loadProgressHandler = new LoadProgressHandler(handler);
 final Entity<InputStream> entity = Entity.entity(imagePayload, APPLICATION_OCTET_STREAM);
 try (final ProgressStream load =
     request(POST, ProgressStream.class, resource,
         resource.request(APPLICATION_JSON_TYPE), entity)) {
  load.tail(loadProgressHandler, POST, resource.getUri());
  return loadProgressHandler.getImageNames();
 } catch (IOException e) {
  throw new DockerException(e);
 } finally {
  IOUtils.closeQuietly(imagePayload);
 }
}

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

/** Construct a reader for a file. */
protected DataFileReader(SeekableInput sin, DatumReader<D> reader, boolean closeOnError)
 throws IOException {
 super(reader);
 try {
  this.sin = new SeekableInputStream(sin);
  initialize(this.sin);
  blockFinished();
 } catch(final Throwable e) {
  if (closeOnError) {
   IOUtils.closeQuietly( sin );
  }
  throw e;
 }
}

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

private ZipFile(final SeekableByteChannel channel, final String archiveName,
        final String encoding, final boolean useUnicodeExtraFields,
        final boolean closeOnError)
  throws IOException {
  this.archiveName = archiveName;
  this.encoding = encoding;
  this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding);
  this.useUnicodeExtraFields = useUnicodeExtraFields;
  archive = channel;
  boolean success = false;
  try {
    final Map<ZipArchiveEntry, NameAndComment> entriesWithoutUTF8Flag =
      populateFromCentralDirectory();
    resolveLocalFileHeaderData(entriesWithoutUTF8Flag);
    success = true;
  } finally {
    closed = !success;
    if (!success && closeOnError) {
      IOUtils.closeQuietly(archive);
    }
  }
}

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

IOUtils.closeQuietly(writer);
  writer = null;
 } else {
} catch (IOException e) {
 IOUtils.closeQuietly(writer);
 writer = null;
 if (retryCount < MAX_RETRIES) {

相关文章