use of com.google.common.io.CountingOutputStream in project oap by oaplatform.
the class FsPersistenceBackend method migration.
@SneakyThrows
private Path migration(Path path) {
return Threads.synchronously(lock, () -> {
JsonMetadata oldV = new JsonMetadata(Binder.json.unmarshal(new TypeReference<Map<String, Object>>() {
}, path));
Persisted fn = Persisted.valueOf(path);
log.debug("migration {}", fn);
val migration = Lists.find2(migrations, m -> m.fromVersion() == fn.version);
if (migration == null)
throw new FileStorageMigrationException("migration from version " + fn + " not found");
Path name = fn.toVersion(migration.fromVersion() + 1);
JsonMetadata newV = migration.run(oldV);
long writeLen = -1;
while (name.toFile().length() != writeLen) {
try (val out = new CountingOutputStream(IoStreams.out(name, PLAIN, DEFAULT_BUFFER, false, true))) {
Binder.json.marshal(out, newV.underlying);
writeLen = out.getCount();
}
}
Files.delete(path);
return name;
});
}
use of com.google.common.io.CountingOutputStream in project bboxdb by jnidzwetzki.
the class SSTableWriter method open.
/**
* Open all required files
* @throws StorageManagerException
*/
public void open() throws StorageManagerException {
final String directoryName = SSTableHelper.getSSTableDir(directory, name);
final File directoryHandle = new File(directoryName);
if (!directoryHandle.isDirectory()) {
final String error = "Directory for SSTable " + name + " does not exist: " + directoryName;
logger.error(error);
throw new StorageManagerException(error);
}
final String sstableOutputFileName = SSTableHelper.getSSTableFilename(directory, name, tablenumber);
sstableFile = new File(sstableOutputFileName);
final String outputIndexFileName = SSTableHelper.getSSTableIndexFilename(directory, name, tablenumber);
sstableIndexFile = new File(outputIndexFileName);
// Don't overwrite old data
if (sstableFile.exists()) {
throw new StorageManagerException("Table file already exists: " + sstableOutputFileName);
}
if (sstableIndexFile.exists()) {
throw new StorageManagerException("Table file already exists: " + sstableIndexFile);
}
if (sstableBloomFilterFile.exists()) {
throw new StorageManagerException("Bloom filter file already exists: " + sstableBloomFilterFile);
}
try {
logger.info("Writing new SSTable for relation: {} file: {}", name.getFullname(), sstableOutputFileName);
final BufferedOutputStream sstableFileOutputStream = new BufferedOutputStream(new FileOutputStream(sstableFile));
sstableOutputStream = new CountingOutputStream(sstableFileOutputStream);
sstableOutputStream.write(SSTableConst.MAGIC_BYTES);
sstableIndexOutputStream = new BufferedOutputStream(new FileOutputStream(sstableIndexFile));
sstableIndexOutputStream.write(SSTableConst.MAGIC_BYTES_INDEX);
} catch (FileNotFoundException e) {
exceptionDuringWrite = true;
throw new StorageManagerException("Unable to open output file", e);
} catch (IOException e) {
exceptionDuringWrite = true;
throw new StorageManagerException("Unable to write into output file", e);
}
}
use of com.google.common.io.CountingOutputStream in project arctic-sea by 52North.
the class HttpUtils method writeObject.
private void writeObject(HttpServletRequest request, HttpServletResponse response, MediaType contentType, Writable writable, EncodingExceptionHandler owserHandler) throws IOException, HTTPException {
OutputStream out = null;
response.setContentType(writable.getEncodedContentType().toString());
try {
out = response.getOutputStream();
if (HTTPHeaders.supportsGzipEncoding(request) && writable.supportsGZip()) {
out = new GZIPOutputStream(out);
response.setHeader(HTTPHeaders.CONTENT_ENCODING, HTTPConstants.GZIP_ENCODING);
}
if (isCountingOutputStream) {
out = new CountingOutputStream(out);
}
if (writable.hasForcedHttpStatus()) {
response.setStatus(writable.getForcedHttpStatus().getCode());
}
writable.write(out, new ResponseProxy(response));
out.flush();
} catch (EncodingException e) {
Object writeOwsExceptionReport = owserHandler.handleEncodingException(request, response, e);
if (writeOwsExceptionReport != null) {
Writable owserWritable = getWritable(writeOwsExceptionReport, contentType);
try {
owserWritable.write(out, new ResponseProxy(response));
if (out != null) {
out.flush();
}
} catch (EncodingException ex) {
throw new HTTPException(HTTPStatus.INTERNAL_SERVER_ERROR, ex);
}
}
} finally {
if (out instanceof CountingOutputStream) {
Long bytesWritten = ((CountingOutputStream) out).getCount();
eventBus.submit(new CountingOutputStreamEvent(bytesWritten));
}
if (out != null) {
LOGGER.debug("Response status = " + response.getStatus());
out.close();
}
}
}
use of com.google.common.io.CountingOutputStream in project jib by google.
the class CacheWriterTest method testGetLayerOutputStream.
@Test
public void testGetLayerOutputStream() throws IOException, LayerPropertyNotFoundException {
ExpectedLayer expectedLayer = getExpectedLayer();
// Writes resourceBlob as a layer to the cache.
CacheWriter cacheWriter = new CacheWriter(testCache);
CountingOutputStream layerOutputStream = cacheWriter.getLayerOutputStream(expectedLayer.blobDescriptor.getDigest());
expectedLayer.blob.writeTo(layerOutputStream);
CachedLayer cachedLayer = cacheWriter.getCachedLayer(expectedLayer.blobDescriptor.getDigest(), layerOutputStream);
CachedLayerWithMetadata layerInMetadata = testCache.getMetadata().getLayers().get(0);
Assert.assertNull(layerInMetadata.getMetadata());
verifyCachedLayerIsExpected(expectedLayer, cachedLayer);
}
use of com.google.common.io.CountingOutputStream in project presto by prestodb.
the class OrcMetadataWriter method writeProtobufObject.
private static int writeProtobufObject(OutputStream output, MessageLite object) throws IOException {
CountingOutputStream countingOutput = new CountingOutputStream(output);
object.writeTo(countingOutput);
return toIntExact(countingOutput.getCount());
}
Aggregations