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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.apache.commons.httpclient.methods.multipart;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.appwork.utils.net.meteredconnection.MeteredOutputStream;
/**
*
* @author firone
*/
public class MeteredFilePart extends FilePart {
private MeteredOutputStream meteredOutputStream;
public MeteredFilePart(String name, File file) throws FileNotFoundException {
super(name, file);
}
public MeteredFilePart(String name, PartSource partSource) {
super(name, partSource);
}
public MeteredFilePart(String name, String fileName, File file) throws FileNotFoundException {
super(name, fileName, file);
}
public MeteredFilePart(String name, File file, String contentType, String charset) throws FileNotFoundException {
super(name, file, contentType, charset);
}
public MeteredFilePart(String name, PartSource partSource, String contentType, String charset) {
super(name, partSource, contentType, charset);
}
public MeteredFilePart(String name, String fileName, File file, String contentType, String charset) throws FileNotFoundException {
super(name, fileName, file, contentType, charset);
}
/**
* Write the data in "source" to the specified stream.
* @param out The output stream.
* @throws IOException if an IO problem occurs.
* @see org.apache.commons.httpclient.methods.multipart.Part#sendData(OutputStream)
*/
@Override
protected void sendData(OutputStream out) throws IOException {
if (lengthOfData() == 0) {
// this file contains no data, so there is nothing to send.
// we don't want to create a zero length buffer as this will
// cause an infinite loop when reading.
return;
}
if (meteredOutputStream == null) {
out = meteredOutputStream = new MeteredOutputStream(out);
}
byte[] tmp = new byte[4096];
InputStream instream = getSource().createInputStream();
try {
int len;
while ((len = instream.read(tmp)) >= 0) {
out.write(tmp, 0, len);
}
} finally {
// we're done with the stream, close it
instream.close();
}
}
public MeteredOutputStream getMeteredOutputStream() {
return meteredOutputStream;
}
} |
Partager