Search in sources :

Example 11 with CountingOutputStream

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;
    });
}
Also used : lombok.val(lombok.val) Path(java.nio.file.Path) CountingOutputStream(com.google.common.io.CountingOutputStream) JsonMetadata(oap.storage.migration.JsonMetadata) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ToString(lombok.ToString) FileStorageMigrationException(oap.storage.migration.FileStorageMigrationException) SneakyThrows(lombok.SneakyThrows)

Example 12 with CountingOutputStream

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);
    }
}
Also used : CountingOutputStream(com.google.common.io.CountingOutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) StorageManagerException(org.bboxdb.storage.StorageManagerException) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 13 with CountingOutputStream

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();
        }
    }
}
Also used : ResponseProxy(org.n52.iceland.coding.encode.ResponseProxy) CountingOutputStream(com.google.common.io.CountingOutputStream) HTTPException(org.n52.iceland.exception.HTTPException) GZIPOutputStream(java.util.zip.GZIPOutputStream) EncodingException(org.n52.svalbard.encode.exception.EncodingException) CountingOutputStreamEvent(org.n52.iceland.event.events.CountingOutputStreamEvent) CountingOutputStream(com.google.common.io.CountingOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream)

Example 14 with CountingOutputStream

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);
}
Also used : CountingOutputStream(com.google.common.io.CountingOutputStream) Test(org.junit.Test)

Example 15 with CountingOutputStream

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());
}
Also used : CountingOutputStream(com.google.common.io.CountingOutputStream)

Aggregations

CountingOutputStream (com.google.common.io.CountingOutputStream)35 IOException (java.io.IOException)10 OutputStream (java.io.OutputStream)5 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FileInputStream (java.io.FileInputStream)3 Path (java.nio.file.Path)3 Test (org.junit.Test)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)2 FileBackedOutputStream (com.google.common.io.FileBackedOutputStream)2 ResourceResponse (ddf.catalog.operation.ResourceResponse)2 BufferedOutputStream (java.io.BufferedOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 InputStream (java.io.InputStream)2 GZIPOutputStream (java.util.zip.GZIPOutputStream)2 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 Produces (javax.ws.rs.Produces)2