Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 package org.adblockplus; | 18 package org.adblockplus; |
19 | 19 |
20 import java.io.FilterOutputStream; | 20 import java.io.FilterOutputStream; |
21 import java.io.IOException; | 21 import java.io.IOException; |
22 import java.io.OutputStream; | 22 import java.io.OutputStream; |
23 | 23 |
24 /** | 24 /** |
25 * ChunkedOutputStream implements chunked HTTP transfer encoding wrapper for Out putStream. | 25 * ChunkedOutputStream implements chunked HTTP transfer encoding wrapper for |
26 * OutputStream. | |
26 */ | 27 */ |
27 public class ChunkedOutputStream extends FilterOutputStream | 28 public class ChunkedOutputStream extends FilterOutputStream |
28 { | 29 { |
29 private final static int MAX_CHUNK_SIZE = 2048; | 30 private static final int MAX_CHUNK_SIZE = 2048; |
30 private final static byte[] CRLF = { '\r', '\n' }; | |
31 private final static byte[] FINAL_CHUNK = new byte[] { '0', '\r', '\n', '\r', '\n' }; | |
32 | 31 |
32 private static final byte[] CRLF = {'\r', '\n'}; | |
33 private static final byte[] FINAL_CHUNK = new byte[] {'0', '\r', '\n', '\r', ' \n'}; | |
33 private boolean wroteFinalChunk = false; | 34 private boolean wroteFinalChunk = false; |
34 | 35 |
35 public ChunkedOutputStream(final OutputStream out) | 36 public ChunkedOutputStream(final OutputStream out) |
36 { | 37 { |
37 super(out); | 38 super(out); |
38 } | 39 } |
39 | 40 |
40 @Override | 41 @Override |
41 public void close() throws IOException | 42 public void close() throws IOException |
42 { | 43 { |
43 // TODO: Huh? No sync, no 'this.wroteFinalChunk = false'? | 44 if (!wroteFinalChunk) |
Felix Dahlke
2014/04/16 15:24:25
Can you make this TODO more clear? If you think th
René Jeschke
2014/04/16 17:51:47
Removed. It will throw an exception anyway if it i
| |
44 if (!this.wroteFinalChunk) | 45 writeFinalChunk(); |
Felix Dahlke
2014/04/16 15:24:25
I agree to adding final's everywhere, but let's pl
René Jeschke
2014/04/16 17:51:47
Done.
| |
45 { | |
Felix Dahlke
2014/04/16 15:24:25
This is also an unrelated change.
René Jeschke
2014/04/16 17:51:47
Done.
| |
46 this.writeFinalChunk(); | |
47 } | |
48 super.close(); | 46 super.close(); |
49 } | 47 } |
50 | 48 |
51 @Override | 49 @Override |
52 public void write(final byte[] buffer, final int offset, final int length) thr ows IOException | 50 public void write(final byte[] buffer, final int offset, final int length) thr ows IOException |
53 { | 51 { |
54 this.writeChunk(buffer, offset, length); | 52 writeChunk(buffer, offset, length); |
55 } | 53 } |
56 | 54 |
57 @Override | 55 @Override |
58 public void write(final byte[] buffer) throws IOException | 56 public void write(final byte[] buffer) throws IOException |
59 { | 57 { |
60 int offset = 0; | 58 int offset = 0; |
61 int remain = buffer.length; | 59 int remain = buffer.length; |
62 while (remain > 0) | 60 while (remain > 0) |
63 { | 61 { |
64 final int size = Math.min(remain, MAX_CHUNK_SIZE); | 62 int size = MAX_CHUNK_SIZE; |
65 this.writeChunk(buffer, offset, size); | 63 if (size > remain) |
64 size = remain; | |
65 writeChunk(buffer, offset, size); | |
66 offset += size; | 66 offset += size; |
67 remain -= size; | 67 remain -= size; |
68 } | 68 } |
69 } | 69 } |
70 | 70 |
71 @Override | 71 @Override |
72 public void write(final int oneByte) throws IOException | 72 public void write(final int oneByte) throws IOException |
73 { | 73 { |
74 throw new UnsupportedOperationException("Not implemented"); | 74 throw new UnsupportedOperationException("Not implemented"); |
75 } | 75 } |
76 | 76 |
77 public void writeFinalChunk() throws IOException | 77 public void writeFinalChunk() throws IOException |
78 { | 78 { |
79 this.out.write(FINAL_CHUNK); | 79 out.write(FINAL_CHUNK); |
80 this.out.flush(); | 80 out.flush(); |
81 this.wroteFinalChunk = true; | 81 wroteFinalChunk = true; |
82 } | 82 } |
83 | 83 |
84 private void writeChunk(final byte buffer[], final int offset, final int lengt h) throws IOException | 84 private void writeChunk(final byte buffer[], final int offset, final int lengt h) throws IOException |
85 { | 85 { |
86 // Zero sized buffers are ok on slow connections but not in our case - zero chunk is used to indicate the end of transfer. | 86 // Zero sized buffers are ok on slow connections but not in our case - zero |
Felix Dahlke
2014/04/16 15:24:25
Our rule is to wrap at 100 columns, which is what
René Jeschke
2014/04/16 17:51:47
Done.
| |
87 // chunk is used to indicate the end of transfer. | |
87 if (length > 0) | 88 if (length > 0) |
88 { | 89 { |
89 // Write the chunk length as a hex number | 90 // Write the chunk length as a hex number |
90 this.writeHex(length); | 91 writeHex(length); |
91 // Write the data | 92 // Write the data |
92 this.out.write(buffer, offset, length); | 93 out.write(buffer, offset, length); |
93 // Write a CRLF | 94 // Write a CRLF |
94 this.out.write(CRLF); | 95 out.write(CRLF); |
95 // Flush the underlying stream | 96 // Flush the underlying stream |
96 this.out.flush(); | 97 out.flush(); |
97 } | 98 } |
98 } | 99 } |
99 | 100 |
100 private void writeHex(final int i) throws IOException | 101 private void writeHex(final int i) throws IOException |
101 { | 102 { |
102 this.out.write(Integer.toHexString(i).getBytes()); | 103 out.write(Integer.toHexString(i).getBytes()); |
103 this.out.write(CRLF); | 104 out.write(CRLF); |
104 } | 105 } |
105 } | 106 } |
LEFT | RIGHT |