Skip to content

Commit a5e135a

Browse files
hramosfacebook-github-bot
authored andcommitted
Add CountingOutputStream
Reviewed By: mdvacca Differential Revision: D6932872 fbshipit-source-id: 226f30833a786d0c03564f25ec8c4f43d94c48f4
1 parent 2f02dd4 commit a5e135a

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
17+
package com.facebook.react.modules.network;
18+
19+
import java.io.FilterOutputStream;
20+
import java.io.IOException;
21+
import java.io.OutputStream;
22+
23+
/**
24+
* An OutputStream that counts the number of bytes written.
25+
*
26+
* @author Chris Nokleberg
27+
* @since 1.0
28+
*/
29+
public class CountingOutputStream extends FilterOutputStream {
30+
31+
private long mCount;
32+
33+
/**
34+
* Constructs a new {@code FilterOutputStream} with {@code out} as its
35+
* target stream.
36+
*
37+
* @param out the target stream that this stream writes to.
38+
*/
39+
public CountingOutputStream(OutputStream out) {
40+
super(out);
41+
mCount = 0;
42+
}
43+
44+
/**
45+
* Returns the number of bytes written.
46+
*/
47+
public long getCount() {
48+
return mCount;
49+
}
50+
51+
@Override
52+
public void write(byte[] b, int off, int len) throws IOException {
53+
out.write(b, off, len);
54+
mCount += len;
55+
}
56+
57+
@Override
58+
public void write(int b) throws IOException {
59+
out.write(b);
60+
mCount++;
61+
}
62+
63+
// Overriding close() because FilterOutputStream's close() method pre-JDK8 has bad behavior:
64+
// it silently ignores any exception thrown by flush(). Instead, just close the delegate stream.
65+
// It should flush itself if necessary.
66+
@Override
67+
public void close() throws IOException {
68+
out.close();
69+
}
70+
}

0 commit comments

Comments
 (0)