CountingOutputStream os = new CountingOutputStream(new WriterOutputStream(w)); int sz; while ((sz = f.read(buf)) >= 0) os.write(buf, 0, sz); } else { ByteBuf buf = new ByteBuf(null, f); os.flush(); return os.getCount()+start;
final CountingOutputStream mergedFileOutputStream = new CountingOutputStream( new FileOutputStream(outputFile)); final long initialFileLength = mergedFileOutputStream.getByteCount(); partitionLengths[partition] = (mergedFileOutputStream.getByteCount() - initialFileLength);
@Test public void testCounting() throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final CountingOutputStream cos = new CountingOutputStream(baos); cos.write(i); assertEquals("CountingOutputStream.getCount()", cos.getCount(), 20); array[i-20] = (byte)i; cos.write(array); assertByteArrayEquals("CountingOutputStream.write(byte[])", baos.toByteArray(), 0, 30); assertEquals("CountingOutputStream.getCount()", cos.getCount(), 30); cos.write(array, 5, 5); assertByteArrayEquals("CountingOutputStream.write(byte[], int, int)", baos.toByteArray(), 0, 35); assertEquals("CountingOutputStream.getCount()", cos.getCount(), 35); final int count = cos.resetCount(); assertEquals("CountingOutputStream.resetCount()", count, 35); cos.write(i); assertEquals("CountingOutputStream.getCount()", cos.getCount(), 10); cos.close();
@Test public void testLargeFiles_IO84() throws Exception { final long size = (long)Integer.MAX_VALUE + (long)1; final NullInputStream mock = new NullInputStream(size); final OutputStream nos = new NullOutputStream(); final CountingOutputStream cos = new CountingOutputStream(nos); // Test integer methods IOUtils.copyLarge(mock, cos); try { cos.getCount(); fail("Expected getCount() to throw an ArithmeticException"); } catch (final ArithmeticException ae) { // expected result } try { cos.resetCount(); fail("Expected resetCount() to throw an ArithmeticException"); } catch (final ArithmeticException ae) { // expected result } mock.close(); // Test long methods IOUtils.copyLarge(mock, cos); assertEquals("getByteCount()", size, cos.getByteCount()); assertEquals("resetByteCount()", size, cos.resetByteCount()); }
CountingOutputStream os = new CountingOutputStream(out); int sz; while((sz=f.read(buf))>=0) os.write(buf,0,sz); } else { ByteBuf buf = new ByteBuf(null,f); os.flush(); return os.getByteCount()+start;
OutputStream os = ...; CountingOutputStream cos = new CountingOutputStream(os); PrintStream ps = new PrintStream(cos); // write to ps. ps.close(); long count = cos.getCount();
try CountingOutputStream output = new CountingOutputStream( new BufferedOutputStream(new FileOutputStream(outputFile))); output.flush(); output.close(); writer.close(); output.getByteCount(), (System.nanoTime() - startTime) / 1e9 );
@Override public int writeTo(File outputFile) throws FileNotFoundException, IOException { // Ensure that the directory for the SWF exists. final File outputDirectory = new File(outputFile.getAbsoluteFile().getParent()); outputDirectory.mkdirs(); // Write out the SWF, counting how many bytes were written. final CountingOutputStream output = new CountingOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile))); writeTo(output); output.flush(); output.close(); close(); final int swfSize = output.getCount(); return swfSize; }
/** * The number of bytes that have passed through this stream. * <p> * NOTE: From v1.3 this method throws an ArithmeticException if the * count is greater than can be expressed by an <code>int</code>. * See {@link #getByteCount()} for a method using a <code>long</code>. * * @return the number of bytes accumulated * @throws ArithmeticException if the byte count is too large */ public int getCount() { final long result = getByteCount(); if (result > Integer.MAX_VALUE) { throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int"); } return (int) result; }
public void add( MutableString word, MutableString nonWord ) throws IOException { int t = terms.getInt( word ); if ( t == -1 ) { terms.put( word.copy(), t = terms.size() ); termsOutputStream.resetByteCount(); word.writeSelfDelimUTF8( termsOutputStream ); termOffsetsObs.writeLongDelta( termsOutputStream.getByteCount() ); } fieldContent.add( t ); if ( exact ) { t = nonTerms.getInt( nonWord ); if ( t == -1 ) { nonTerms.put( nonWord.copy(), t = nonTerms.size() ); nonTermsOutputStream.resetByteCount(); nonWord.writeSelfDelimUTF8( nonTermsOutputStream ); nonTermOffsetsObs.writeLongDelta( nonTermsOutputStream.getByteCount() ); } fieldContent.add( t ); } }
/** * Performs actual gzip of the filtered content. */ private void doGzipResponse(final HttpServletRequest req, final HttpServletResponse response, final FilterChain chain) throws IOException, ServletException { LOG.debug("Applying gzip on resource: " + req.getRequestURI()); response.setHeader(HttpHeader.CONTENT_ENCODING.toString(), "gzip"); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final CountingOutputStream countingStream = new CountingOutputStream(new GZIPOutputStream(new BufferedOutputStream( baos))); // final GZIPOutputStream gzout = new GZIPOutputStream(new BufferedOutputStream(baos)); // Perform gzip operation in-memory before sending response final HttpServletResponseWrapper wrappedResponse = new RedirectedStreamServletResponseWrapper(countingStream, response); chain.doFilter(req, wrappedResponse); // close underlying stream countingStream.close(); response.setContentLength(countingStream.getCount()); // avoid NO CONTENT error thrown by jetty when gzipping empty response if (countingStream.getCount() > 0) { IOUtils.write(baos.toByteArray(), response.getOutputStream()); } }
throw new IllegalStateException("reached max payload size"); } else { if (cos.getByteCount() + record.length < MAX_RECORD_SIZE) { cos.write(record); return true; this.dataRecords.add(new Record().withData(data)); baos.reset(); cos.resetByteCount(); cos.resetCount(); cos.write(record);
CountingOutputStream os = new CountingOutputStream( responseFile.getContent().getOutputStream(append)); try { messageFormatter.writeTo(msgContext, format, os, false); } finally { os.close(); metrics.incrementBytesSent(msgContext, os.getByteCount());
int size; String remote, local; // do some work to initialize size, remote and local file path // before saving remoteSource to local OutputStream output = new FileOutputStream(local); CountingOutputStream cos = new CountingOutputStream(output){ protected void beforeWrite(int n){ super.beforeWrite(n); System.err.println("Downloaded "+getCount() + "/" + size); } }; ftp.retrieveFile(remote, cos); output.close();
/** * Writes raw bytes to the output stream * @param bytes The bytes * @throws IOException In case of an I/O error */ public void writeBytes(byte[] bytes) throws IOException { out.write(bytes); }
CountingOutputStream cos = new CountingOutputStream(socket.getOutputStream()); cos.write(...); System.out.println("wrote " + cos.getByteCount() + " bytes");
public void close() throws IOException { os.close(); }
/** * Set the byte count back to 0. * <p> * NOTE: From v1.3 this method throws an ArithmeticException if the * count is greater than can be expressed by an <code>int</code>. * See {@link #resetByteCount()} for a method using a <code>long</code>. * * @return the count previous to resetting * @throws ArithmeticException if the byte count is too large */ public int resetCount() { final long result = resetByteCount(); if (result > Integer.MAX_VALUE) { throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int"); } return (int) result; }
@Override public void close() { if (this.os != this.cos) { try { this.os.flush(); } catch (IOException e) { logger.warn("unable to flush os"); } try { this.os.close(); } catch (IOException e) { logger.warn("unable to close os"); } } try { this.cos.flush(); } catch (IOException e) { logger.warn("unable to flush cos"); } try { this.cos.close(); } catch (IOException e) { logger.warn("unable to close cos"); } }
@Override public void run() { progressListener.updateProgress(countingOutputStream.getCount()); } }, 0, 50);