forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInefficientOutputStream.qhelp
More file actions
64 lines (53 loc) · 2.12 KB
/
InefficientOutputStream.qhelp
File metadata and controls
64 lines (53 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
The classes <code>java.io.OutputStream</code>
and <code>java.io.FilterOutputStream</code> only require
subclasses to implement the method <code>write(byte b)</code>.
Typically, uses of <code>OutputStream</code>s will not write
single bytes, but an array via the <code>write(byte[] b, int off,
int len)</code> method. The default implementation of this method,
which you are not required to override,
calls <code>write(byte b)</code> for each byte in the array. If
this method involves I/O, such as accessing the network or disk,
this is likely to incur significant overhead.
</p>
</overview>
<recommendation>
<p>
Always provide an implementation of the <code>write(byte[] b, int
off, int len)</code> method.
</p>
</recommendation>
<example>
<p>
The following example shows a subclass
of <code>OutputStream</code> that simply wraps
a <code>DigestOutputStream</code> to confirm that the data it
writes to a file has the expected MD5 hash. Without an
implementation of <code>write(byte[] b, int off, int len)</code>
this will be very slow, because it makes a call
to <code>DigestOutputStream.write(byte b)</code>
and <code>FileOutputStream.write(byte b)</code> for each byte
written.
</p>
<sample src="InefficientOutputStreamBad.java" />
<p>
The example can be updated to use a more efficient method. In this
case, calls to <code>write(byte[] b, int off, int len)</code> are
simply forwarded to <code>DigestOutputStream.write(byte[] b, int
off, int len)</code>.
</p>
<sample src="InefficientOutputStreamGood.java" />
</example>
<references>
<li>
Java API Specification:
<a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/OutputStream.html#write(byte[],int,int)">OutputStream.write(byte[] b, int off, int len)</a>,
<a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/FilterOutputStream.html#write(byte[],int,int)">FilterOutputStream.write(byte[] b, int off, int len)</a>.
</li>
</references>
</qhelp>