Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.google.caliper; |
| 18 | |
| 19 | import java.io.PrintStream; |
| 20 | |
| 21 | /** |
| 22 | * Counts how many characters were written. |
| 23 | */ |
| 24 | final class CountingPrintStream extends PrintStream { |
| 25 | |
| 26 | private final PrintStream delegate; |
| 27 | private int count; |
| 28 | |
| 29 | CountingPrintStream(PrintStream delegate) { |
| 30 | super(delegate); |
| 31 | this.delegate = delegate; |
| 32 | } |
| 33 | |
| 34 | public int getCount() { |
| 35 | return count; |
| 36 | } |
| 37 | |
| 38 | @Override public void flush() { |
| 39 | delegate.flush(); |
| 40 | } |
| 41 | |
| 42 | @Override public void close() { |
| 43 | delegate.close(); |
| 44 | } |
| 45 | |
| 46 | @Override public boolean checkError() { |
| 47 | return delegate.checkError(); |
| 48 | } |
| 49 | |
| 50 | @Override protected void setError() { |
| 51 | throw new UnsupportedOperationException(); |
| 52 | } |
| 53 | |
| 54 | @Override protected void clearError() { |
| 55 | throw new UnsupportedOperationException(); |
| 56 | } |
| 57 | |
| 58 | @Override public void write(int b) { |
| 59 | count++; |
| 60 | delegate.write(b); |
| 61 | } |
| 62 | |
| 63 | @Override public void write(byte[] buffer, int offset, int length) { |
| 64 | count += length; |
| 65 | delegate.write(buffer, offset, length); |
| 66 | } |
| 67 | |
| 68 | @Override public void print(char[] chars) { |
| 69 | count += chars.length; |
| 70 | delegate.print(chars); |
| 71 | } |
| 72 | |
| 73 | @Override public void print(String s) { |
| 74 | count += s.length(); |
| 75 | delegate.print(s); |
| 76 | } |
| 77 | } |