use of com.google.common.io.CountingOutputStream in project gradle by gradle.
the class ByteOutput method start.
/**
* Starts writing to the given offset. Can be beyond the current length of the file.
*/
public DataOutputStream start(long offset) throws IOException {
file.seek(offset);
bufferedOutputStream.clear();
countingOutputStream = new CountingOutputStream(bufferedOutputStream);
return new DataOutputStream(countingOutputStream);
}
use of com.google.common.io.CountingOutputStream in project gerrit by GerritCodeReview.
the class RestApiServlet method replyBinaryResult.
@SuppressWarnings("resource")
static long replyBinaryResult(@Nullable HttpServletRequest req, HttpServletResponse res, BinaryResult bin) throws IOException {
final BinaryResult appResult = bin;
try {
if (bin.getAttachmentName() != null) {
res.setHeader("Content-Disposition", "attachment; filename=\"" + bin.getAttachmentName() + "\"");
}
if (bin.isBase64()) {
if (req != null && JSON_TYPE.equals(req.getHeader(HttpHeaders.ACCEPT))) {
bin = stackJsonString(res, bin);
} else {
bin = stackBase64(res, bin);
}
}
if (bin.canGzip() && acceptsGzip(req)) {
bin = stackGzip(res, bin);
}
res.setContentType(bin.getContentType());
long len = bin.getContentLength();
if (0 <= len && len < Integer.MAX_VALUE) {
res.setContentLength((int) len);
} else if (0 <= len) {
res.setHeader("Content-Length", Long.toString(len));
}
if (req == null || !"HEAD".equals(req.getMethod())) {
try (CountingOutputStream dst = new CountingOutputStream(res.getOutputStream())) {
bin.writeTo(dst);
return dst.getCount();
}
}
return 0;
} finally {
appResult.close();
}
}
use of com.google.common.io.CountingOutputStream in project tutorials by eugenp.
the class GuavaCountingOutputStreamTest method givenData_whenCountReachesLimit_thenThrowException.
@Test(expected = RuntimeException.class)
public void givenData_whenCountReachesLimit_thenThrowException() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
CountingOutputStream cos = new CountingOutputStream(out);
byte[] data = new byte[1024];
ByteArrayInputStream in = new ByteArrayInputStream(data);
int b;
while ((b = in.read()) != -1) {
cos.write(b);
if (cos.getCount() >= MAX) {
throw new RuntimeException("Write limit reached");
}
}
}
use of com.google.common.io.CountingOutputStream in project jib by google.
the class PullAndCacheBaseImageLayerStep method call.
/**
* Depends on {@code pullAuthorizationFuture}.
*/
@Override
public CachedLayer call() throws IOException, RegistryException, LayerPropertyNotFoundException, ExecutionException, InterruptedException {
try (Timer ignored = new Timer(buildConfiguration.getBuildLogger(), String.format(DESCRIPTION, layerDigest))) {
RegistryClient registryClient = new RegistryClient(pullAuthorizationFuture.get(), buildConfiguration.getBaseImageRegistry(), buildConfiguration.getBaseImageRepository());
// Checks if the layer already exists in the cache.
CachedLayer cachedLayer = new CacheReader(cache).getLayer(layerDigest);
if (cachedLayer != null) {
return cachedLayer;
}
CacheWriter cacheWriter = new CacheWriter(cache);
CountingOutputStream layerOutputStream = cacheWriter.getLayerOutputStream(layerDigest);
registryClient.pullBlob(layerDigest, layerOutputStream);
return cacheWriter.getCachedLayer(layerDigest, layerOutputStream);
}
}
use of com.google.common.io.CountingOutputStream in project druid by druid-io.
the class PrefetchableTextFilesFirehoseFactoryTest method setup.
@BeforeClass
public static void setup() throws IOException {
NullHandling.initializeForTests();
TEST_DIR = tempDir.newFolder();
for (int i = 0; i < 100; i++) {
try (CountingOutputStream cos = new CountingOutputStream(Files.newOutputStream(new File(TEST_DIR, "test_" + i).toPath()));
Writer writer = new BufferedWriter(new OutputStreamWriter(cos, StandardCharsets.UTF_8))) {
for (int j = 0; j < 100; j++) {
final String a = StringUtils.format("%d,%03d,%03d\n", (20171220 + i), i, j);
writer.write(a);
}
writer.flush();
// Every file size must be same
if (FILE_SIZE == -1) {
FILE_SIZE = cos.getCount();
} else {
Assert.assertEquals(FILE_SIZE, cos.getCount());
}
}
}
}
Aggregations