| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.util.http; |
| 16 |
|
| 17 |
import java.io.OutputStream; |
| 18 |
import java.io.IOException; |
| 19 |
|
| 20 |
public class HTTPChunkedOutputStream extends OutputStream { |
| 21 |
|
| 22 |
protected boolean closed = false; |
| 23 |
protected int count; |
| 24 |
protected byte[] buf; |
| 25 |
protected OutputStream out; |
| 26 |
|
| 27 |
public HTTPChunkedOutputStream(OutputStream out) { |
| 28 |
this(out, 4096); |
| 29 |
} |
| 30 |
|
| 31 |
public HTTPChunkedOutputStream(OutputStream out, int size) { |
| 32 |
this.out = out; |
| 33 |
this.buf = new byte[size]; |
| 34 |
this.count = 0; |
| 35 |
} |
| 36 |
|
| 37 |
private void flushBuffer(boolean flush) throws IOException { |
| 38 |
String chunkLength = Integer.toString(this.count, 16); |
| 39 |
this.out.write(chunkLength.getBytes()); |
| 40 |
this.out.write(HTTPProtocol.CRLF.getBytes()); |
| 41 |
this.out.write(this.buf, 0, this.count); |
| 42 |
this.out.write(HTTPProtocol.CRLF.getBytes()); |
| 43 |
this.count = 0; |
| 44 |
if (flush) { |
| 45 |
this.out.flush(); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
private void checkNotClosed() throws IOException { |
| 50 |
if (this.closed) { |
| 51 |
throw new IOException("Already closed"); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
public synchronized void write(int b) |
| 56 |
throws IOException { |
| 57 |
checkNotClosed(); |
| 58 |
if (this.count >= this.buf.length) { |
| 59 |
flushBuffer(false); |
| 60 |
} |
| 61 |
this.buf[this.count++] = (byte)b; |
| 62 |
} |
| 63 |
|
| 64 |
public void write(byte b[]) |
| 65 |
throws IOException { |
| 66 |
write(b, 0, b.length); |
| 67 |
} |
| 68 |
|
| 69 |
public synchronized void flush() |
| 70 |
throws IOException { |
| 71 |
if (this.closed) { |
| 72 |
return; |
| 73 |
} |
| 74 |
if (this.count > 0) { |
| 75 |
flushBuffer(true); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
public synchronized void close() |
| 80 |
throws IOException { |
| 81 |
if (this.closed) { |
| 82 |
return; |
| 83 |
} |
| 84 |
flush(); |
| 85 |
// signal end of content with a zero-length chunk |
| 86 |
flushBuffer(true); |
| 87 |
this.closed = true; |
| 88 |
} |
| 89 |
|
| 90 |
public synchronized void write(byte b[], int off, int len) |
| 91 |
throws IOException { |
| 92 |
checkNotClosed(); |
| 93 |
int remaining = len; |
| 94 |
int sofar = 0; |
| 95 |
while (remaining > 0) { |
| 96 |
int roomInBuf = this.buf.length - this.count; |
| 97 |
int lenToWrite = Math.min(roomInBuf, len); |
| 98 |
System.arraycopy(b, sofar + off, buf, this.count, lenToWrite); |
| 99 |
sofar += lenToWrite; |
| 100 |
this.count += lenToWrite; |
| 101 |
remaining -= lenToWrite; |
| 102 |
if (this.count >= this.buf.length) { |
| 103 |
flushBuffer(false); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
} |
| 109 |
|