private static int writeProtobufObject(OutputStream output, MessageLite object) throws IOException { CountingOutputStream countingOutput = new CountingOutputStream(output); object.writeTo(countingOutput); return toIntExact(countingOutput.getCount()); } }
@Override public void write(OutputStream outputStream) throws WebApplicationException { Exception e = null; CountingOutputStream os = new CountingOutputStream(outputStream); try { // json serializer will always close the yielder jsonWriter.writeValue(os, yielder); os.flush(); // Some types of OutputStream suppress flush errors in the .close() method. os.close(); } catch (Exception ex) { e = ex; log.error(ex, "Unable to send query response."); throw Throwables.propagate(ex); } finally { Thread.currentThread().setName(currThreadName); queryLifecycle.emitLogsAndMetrics(e, req.getRemoteAddr(), os.getCount()); if (e == null) { successfulQueryCount.incrementAndGet(); } else { failedQueryCount.incrementAndGet(); } } } },
public void testCount() throws Exception { int written = 0; ByteArrayOutputStream out = new ByteArrayOutputStream(); CountingOutputStream counter = new CountingOutputStream(out); assertEquals(written, out.size()); assertEquals(written, counter.getCount()); counter.write(0); written += 1; assertEquals(written, out.size()); assertEquals(written, counter.getCount()); counter.write(data); written += 10; assertEquals(written, out.size()); assertEquals(written, counter.getCount()); counter.write(data, 0, 5); written += 5; assertEquals(written, out.size()); assertEquals(written, counter.getCount()); counter.write(data, 2, 5); written += 5; assertEquals(written, out.size()); assertEquals(written, counter.getCount()); counter.write(data, 0, data.length + 1); fail("expected exception"); } catch (IndexOutOfBoundsException expected) {
public FlexibleDelimitedFileWriter(LogFilePath path, CompressionCodec codec) throws IOException { Path fsPath = new Path(path.getLogFilePath()); FileSystem fs = FileUtil.getFileSystem(path.getLogFilePath()); this.mCountingStream = new CountingOutputStream(fs.create(fsPath)); this.mWriter = (codec == null) ? new BufferedOutputStream( this.mCountingStream) : new BufferedOutputStream( codec.createOutputStream(this.mCountingStream, mCompressor = CodecPool.getCompressor(codec))); }
@Override public long getWrittenBytes() { return outputStream.getCount(); }
private OutputStream getCurrentTarget() throws IOException { if (currentTarget == null) { currentTarget = new CountingOutputStream(outputFactory.get()); } else if (currentTarget.getCount() >= byteSoftLimit) { LOGGER.info( String.format("Closing stream and opening a new one, reached %,d bytes.\n", currentTarget.getCount())); currentTarget.close(); currentTarget = new CountingOutputStream(outputFactory.get()); } return currentTarget; }
CountingOutputStream countingStream = new CountingOutputStream(out); } else { countingStream.write(','); break; countingStream.flush(); long serializationTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - serializationStart); pm.setSerializationTime(serializationTime); long pageCallTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - callStart); pm.setCallTime(pageCallTime); pm.setBytesWritten(countingStream.getCount()); sentResults = true; } catch (Exception e) {
out.write(FSImageUtil.MAGIC_HEADER); long prevOffset = out.getCount(); FileSummary fileSummary = fileSummaryBld.build(); if (LOG.isDebugEnabled()) { int summaryLen = Ints.checkedCast(out.getCount() - prevOffset); byte[] summaryLenBytes = new byte[4]; ByteBuffer.wrap(summaryLenBytes).asIntBuffer().put(summaryLen); out.write(summaryLenBytes);
@Override protected void encode(OutputStream out) { if (packer != null && deduplicator != null) { CountingOutputStream counting = new CountingOutputStream(out); OutputStream output = counting; try { ObjectFunnel funnel; funnel = ObjectFunnels.newFunnel(output, DataStreamSerializationFactoryV1.INSTANCE); packer.write(funnel, want, have, false, deduplicator); counting.flush(); funnel.close(); } catch (Exception e) { throw new RuntimeException(e); } finally { deduplicator.release(); } } }
@Override public void write(final byte[] b, final int off, final int len) throws IOException { outputStream.write(b, off, len); }
@Override public void close() throws IOException { countingOutputStream.close(); }
@Override public void flush() throws IOException { countingOutputStream.flush(); }
public DelimitedTextFileWriter(LogFilePath path, CompressionCodec codec) throws IOException { Path fsPath = new Path(path.getLogFilePath()); FileSystem fs = FileUtil.getFileSystem(path.getLogFilePath()); this.mCountingStream = new CountingOutputStream(fs.create(fsPath)); this.mWriter = (codec == null) ? new BufferedOutputStream( this.mCountingStream) : new BufferedOutputStream( codec.createOutputStream(this.mCountingStream, mCompressor = CodecPool.getCompressor(codec))); }
@Override public long getLength() throws IOException { assert this.mCountingStream != null; return this.mCountingStream.getCount(); }
private OutputStream getCurrentTarget() throws IOException { if (currentTarget == null) { currentTarget = new CountingOutputStream(outputFactory.get()); } else if (currentTarget.getCount() >= byteSoftLimit) { LOGGER.info( String.format("Closing stream and opening a new one, reached %,d bytes.\n", currentTarget.getCount())); currentTarget.close(); currentTarget = new CountingOutputStream(outputFactory.get()); } return currentTarget; }
public void write(T objectToWrite) throws IOException { if (prevObject != null && strategy.compare(prevObject, objectToWrite) >= 0) { objectsSorted = false; } byte[] bytesToWrite = strategy.toBytes(objectToWrite); ++numWritten; valuesOut.write(Ints.toByteArray(bytesToWrite.length)); valuesOut.write(bytesToWrite); headerOut.write(Ints.toByteArray((int) valuesOut.getCount())); prevObject = objectToWrite; }
@Override public void close() throws IOException { headerOut.close(); valuesOut.close(); final long numBytesWritten = headerOut.getCount() + valuesOut.getCount(); Preconditions.checkState( headerOut.getCount() == (numWritten * 4), "numWritten[%s] number of rows should have [%s] bytes written to headerOut, had[%s]", numWritten, numWritten * 4, headerOut.getCount() ); Preconditions.checkState( numBytesWritten < Integer.MAX_VALUE, "Wrote[%s] bytes, which is too many.", numBytesWritten ); OutputStream metaOut = ioPeon.makeOutputStream(makeFilename("meta")); try { metaOut.write(0x1); metaOut.write(objectsSorted ? 0x1 : 0x0); metaOut.write(Ints.toByteArray((int) numBytesWritten + 4)); metaOut.write(Ints.toByteArray(numWritten)); } finally { metaOut.close(); } }
/** * Writes the specified byte to this output stream. The general * contract for <code>write</code> is that one byte is written * to the output stream. The byte to be written is the eight * low-order bits of the argument <code>b</code>. The 24 * high-order bits of <code>b</code> are ignored. * <p> * Subclasses of <code>OutputStream</code> must provide an * implementation for this method. * </p> * * @param b the <code>byte</code>. * @throws java.io.IOException if an I/O error occurs. In particular, * an <code>IOException</code> may be thrown if the * output stream has been closed. */ @Override public void write(int b) throws IOException { outputStream.write(b); }
@Override public void close() throws IOException { countingOutputStream.close(); }