Coverage Summary for Class: CountingOutputStream (com.google.common.io)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| CountingOutputStream | 0% (0/1) | 0% (0/5) | 0% (0/7) |
1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.io; 16 17 import static com.google.common.base.Preconditions.checkNotNull; 18 19 import com.google.common.annotations.GwtIncompatible; 20 import java.io.FilterOutputStream; 21 import java.io.IOException; 22 import java.io.OutputStream; 23 24 /** 25 * An OutputStream that counts the number of bytes written. 26 * 27 * @author Chris Nokleberg 28 * @since 1.0 29 */ 30 @GwtIncompatible 31 @ElementTypesAreNonnullByDefault 32 public final class CountingOutputStream extends FilterOutputStream { 33 34 private long count; 35 36 /** 37 * Wraps another output stream, counting the number of bytes written. 38 * 39 * @param out the output stream to be wrapped 40 */ 41 public CountingOutputStream(OutputStream out) { 42 super(checkNotNull(out)); 43 } 44 45 /** Returns the number of bytes written. */ 46 public long getCount() { 47 return count; 48 } 49 50 @Override 51 public void write(byte[] b, int off, int len) throws IOException { 52 out.write(b, off, len); 53 count += len; 54 } 55 56 @Override 57 public void write(int b) throws IOException { 58 out.write(b); 59 count++; 60 } 61 62 // Overriding close() because FilterOutputStream's close() method pre-JDK8 has bad behavior: 63 // it silently ignores any exception thrown by flush(). Instead, just close the delegate stream. 64 // It should flush itself if necessary. 65 @Override 66 public void close() throws IOException { 67 out.close(); 68 } 69 }