/** * 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); }
private void closeDecoder() { closeQuietly(decoder); decoder = null; } }
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); }
@Override public void close() { if (closed) { return; } synchronized (this) { closed = true; } responseScanner.close(); response.close(); IOUtils.closeQuietly(isr); }
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(); } }
/** * 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); } }
/** * 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); } }
/** * 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); } } } }
/** 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; } }
} finally { if (reader != null) { IOUtils.closeQuietly(reader);
/** 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; } }
/** * 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); } }
/** * 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); } } }
@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); } }
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; }
/** * 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; }
@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); } }
/** 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; } }
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); } } }
IOUtils.closeQuietly(writer); writer = null; } else { } catch (IOException e) { IOUtils.closeQuietly(writer); writer = null; if (retryCount < MAX_RETRIES) {