| 1 |
/* |
| 2 |
* Copyright (C) 2007 The Guava Authors |
| 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 |
package io.airlift.slice; |
| 17 |
|
| 18 |
import javax.annotation.Nullable; |
| 19 |
|
| 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 |
*/ |
| 29 |
final class CountingOutputStream |
| 30 |
extends FilterOutputStream |
| 31 |
{ |
| 32 |
private long count; |
| 33 |
|
| 34 |
/** |
| 35 |
* Wraps another output stream, counting the number of bytes written. |
| 36 |
* |
| 37 |
* @param out the output stream to be wrapped |
| 38 |
*/ |
| 39 |
public CountingOutputStream(@Nullable OutputStream out) |
| 40 |
{ |
| 41 |
super(out); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Returns the number of bytes written. |
| 46 |
*/ |
| 47 |
public long getCount() |
| 48 |
{ |
| 49 |
return count; |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public void write(byte[] b, int off, int len) |
| 54 |
throws IOException |
| 55 |
{ |
| 56 |
out.write(b, off, len); |
| 57 |
count += len; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public void write(int b) |
| 62 |
throws IOException |
| 63 |
{ |
| 64 |
out.write(b); |
| 65 |
count++; |
| 66 |
} |
| 67 |
} |