private static MappedByteBuffer mapInternal(File file, MapMode mode, long size) throws IOException { checkNotNull(file); checkNotNull(mode); Closer closer = Closer.create(); try { RandomAccessFile raf = closer.register(new RandomAccessFile(file, mode == MapMode.READ_ONLY ? "r" : "rw")); FileChannel channel = closer.register(raf.getChannel()); return channel.map(mode, 0, size == -1 ? channel.size() : size); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
"Failure Allowance must be a ratio between 0 and 1"); Preconditions.checkArgument(maxOutstandingWrites > 0, "Max outstanding writes must be greater than 0"); Preconditions.checkNotNull(asyncDataWriter, "Async Data Writer cannot be null"); this.closer = Closer.create(); State state = ConfigUtils.configToState(config); this.instrumentationEnabled = GobblinMetrics.isEnabled(state); this.metricContext = this.closer.register(Instrumented.getMetricContext(state, asyncDataWriter.getClass())); this.writePermits = new Semaphore(maxOutstandingWrites); this.asyncDataWriter = asyncDataWriter; this.closer.register(asyncDataWriter);
/** * Constructs a Block writer given the file path of the block. * * @param path file path of the block */ public LocalFileBlockWriter(String path) throws IOException { mFilePath = Preconditions.checkNotNull(path, "path"); mLocalFile = mCloser.register(new RandomAccessFile(mFilePath, "rw")); mLocalFileChannel = mCloser.register(mLocalFile.getChannel()); }
public static void testLoggingSuppressor() throws IOException { TestLogHandler logHandler = new TestLogHandler(); Closeables.logger.addHandler(logHandler); try { Closer closer = new Closer(new Closer.LoggingSuppressor()); TestCloseable c1 = closer.register(TestCloseable.throwsOnClose(new IOException())); TestCloseable c2 = closer.register(TestCloseable.throwsOnClose(new RuntimeException())); try { throw closer.rethrow(new IOException("thrown"), IOException.class); } catch (IOException expected) { } assertTrue(logHandler.getStoredLogRecords().isEmpty()); closer.close(); assertEquals(2, logHandler.getStoredLogRecords().size()); LogRecord record = logHandler.getStoredLogRecords().get(0); assertEquals("Suppressing exception thrown when closing " + c2, record.getMessage()); record = logHandler.getStoredLogRecords().get(1); assertEquals("Suppressing exception thrown when closing " + c1, record.getMessage()); } finally { Closeables.logger.removeHandler(logHandler); } }
private MongoContext(final MongoClient client) { Preconditions.checkNotNull(client, "client"); final Closer closer = Closer.create(); closer.register(new Closeable() { @Override public void close() throws IOException { closer.register(new Closeable() { @Override public void close() throws IOException { closer.register(new Closeable() { @Override public void close() throws IOException {
/** * Constructs a Block reader given the file path of the block. * * @param path file path of the block */ public LocalFileBlockReader(String path) throws IOException { mFilePath = Preconditions.checkNotNull(path, "path"); mLocalFile = mCloser.register(new RandomAccessFile(mFilePath, "r")); mFileSize = mLocalFile.length(); mLocalFileChannel = mCloser.register(mLocalFile.getChannel()); }
public void close() { try (Closer closer = Closer.create()) { closer.register(userAggregateMemoryContext::close); closer.register(revocableAggregateMemoryContext::close); closer.register(systemAggregateMemoryContext::close); closer.register(userLocalMemoryContext::close); closer.register(revocableLocalMemoryContext::close); closer.register(systemLocalMemoryContext::close); } catch (IOException e) { throw new RuntimeException("Exception closing memory tracking context", e); } }
public void testExceptionThrown_fromTryBlock() throws IOException { Closer closer = new Closer(suppressor); TestCloseable c1 = closer.register(TestCloseable.normal()); TestCloseable c2 = closer.register(TestCloseable.normal()); IOException exception = new IOException(); try { try { throw exception; } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } } catch (Throwable expected) { assertSame(exception, expected); } assertTrue(c1.isClosed()); assertTrue(c2.isClosed()); assertTrue(suppressor.suppressions.isEmpty()); }
public void testExceptionThrown_whileClosingLastCloseable() throws IOException { Closer closer = new Closer(suppressor); IOException exception = new IOException(); // c1 is added first, closed last TestCloseable c1 = closer.register(TestCloseable.throwsOnClose(exception)); TestCloseable c2 = closer.register(TestCloseable.normal()); try { closer.close(); } catch (Throwable expected) { assertSame(exception, expected); } assertTrue(c1.isClosed()); assertTrue(c2.isClosed()); assertTrue(suppressor.suppressions.isEmpty()); }
@Override public long copyTo(CharSink sink) throws IOException { checkNotNull(sink); Closer closer = Closer.create(); try { Writer writer = closer.register(sink.openStream()); writer.write((String) seq); return seq.length(); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } } }
mCloser = Closer.create(); mUri = Preconditions.checkNotNull(path, "path"); mBlockSize = options.getBlockSizeBytes(); mAlluxioStorageType = options.getAlluxioStorageType(); .register(UnderFileSystemFileOutStream.create(mContext, workerNetAddress, mOptions)); } catch (Throwable t) { throw CommonUtils.closeAndRethrow(mCloser, t);
GoogleCommon.getJsonFactory(), credential) .setApplicationName(Preconditions.checkNotNull(wuState.getProp(APPLICATION_NAME))) .build(); Preconditions.checkNotNull(credential, "Credential is required")) .setApplicationName(Preconditions.checkNotNull(wuState.getProp(APPLICATION_NAME), "ApplicationName is required")) .build(); GoogleDriveFsHelper fsHelper = closer.register(new GoogleDriveFsHelper(wuState, driveClient)); nextWatermark = Long.parseLong(watermarkFormatter.print(nextWatermarkDateTime)); this.actualExtractor = closer.register(new GoogleDriveExtractor<S, D>(copyOf(wuState), fsHelper));
public void testExceptionThrown_whenCreatingCloseables() throws IOException { Closer closer = new Closer(suppressor); TestCloseable c1 = null; TestCloseable c2 = null; TestCloseable c3 = null; try { try { c1 = closer.register(TestCloseable.normal()); c2 = closer.register(TestCloseable.normal()); c3 = closer.register(TestCloseable.throwsOnCreate()); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } } catch (Throwable expected) { assertThat(expected).isInstanceOf(IOException.class); } assertTrue(c1.isClosed()); assertTrue(c2.isClosed()); assertNull(c3); assertTrue(suppressor.suppressions.isEmpty()); }