use of org.apache.http.util.ByteArrayBuffer in project xUtils by wyouflf.
the class HttpMultipart method doWriteTo.
private void doWriteTo(final HttpMultipartMode mode, final OutputStream out, MultipartEntity.CallBackInfo callBackInfo, boolean writeContent) throws IOException {
callBackInfo.pos = 0;
ByteArrayBuffer boundary = encode(this.charset, getBoundary());
for (FormBodyPart part : this.parts) {
if (!callBackInfo.doCallBack(true)) {
throw new InterruptedIOException("cancel");
}
writeBytes(TWO_DASHES, out);
callBackInfo.pos += TWO_DASHES.length();
writeBytes(boundary, out);
callBackInfo.pos += boundary.length();
writeBytes(CR_LF, out);
callBackInfo.pos += CR_LF.length();
MinimalFieldHeader header = part.getHeader();
switch(mode) {
case STRICT:
for (MinimalField field : header) {
writeField(field, out);
callBackInfo.pos += encode(MIME.DEFAULT_CHARSET, field.getName() + field.getBody()).length() + FIELD_SEP.length() + CR_LF.length();
}
break;
case BROWSER_COMPATIBLE:
// Only write Content-Disposition
// Use content charset
MinimalField cd = header.getField(MIME.CONTENT_DISPOSITION);
writeField(cd, this.charset, out);
callBackInfo.pos += encode(this.charset, cd.getName() + cd.getBody()).length() + FIELD_SEP.length() + CR_LF.length();
String filename = part.getBody().getFilename();
if (filename != null) {
MinimalField ct = header.getField(MIME.CONTENT_TYPE);
writeField(ct, this.charset, out);
callBackInfo.pos += encode(this.charset, ct.getName() + ct.getBody()).length() + FIELD_SEP.length() + CR_LF.length();
}
break;
default:
break;
}
writeBytes(CR_LF, out);
callBackInfo.pos += CR_LF.length();
if (writeContent) {
ContentBody body = part.getBody();
body.setCallBackInfo(callBackInfo);
body.writeTo(out);
}
writeBytes(CR_LF, out);
callBackInfo.pos += CR_LF.length();
}
writeBytes(TWO_DASHES, out);
callBackInfo.pos += TWO_DASHES.length();
writeBytes(boundary, out);
callBackInfo.pos += boundary.length();
writeBytes(TWO_DASHES, out);
callBackInfo.pos += TWO_DASHES.length();
writeBytes(CR_LF, out);
callBackInfo.pos += CR_LF.length();
callBackInfo.doCallBack(true);
}
use of org.apache.http.util.ByteArrayBuffer in project xUtils by wyouflf.
the class HttpMultipart method writeBytes.
private static void writeBytes(final String s, final Charset charset, final OutputStream out) throws IOException {
ByteArrayBuffer b = encode(charset, s);
writeBytes(b, out);
}
use of org.apache.http.util.ByteArrayBuffer in project Libraries-for-Android-Developers by eoecn.
the class AsyncHttpResponseHandler method getResponseData.
/**
* Returns byte array of response HttpEntity contents
*
* @param entity can be null
* @return response entity body or null
* @throws java.io.IOException if reading entity or creating byte array failed
*/
byte[] getResponseData(HttpEntity entity) throws IOException {
byte[] responseBody = null;
if (entity != null) {
InputStream instream = entity.getContent();
if (instream != null) {
long contentLength = entity.getContentLength();
if (contentLength > Integer.MAX_VALUE) {
throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
}
int buffersize = (contentLength < 0) ? BUFFER_SIZE : (int) contentLength;
try {
ByteArrayBuffer buffer = new ByteArrayBuffer(buffersize);
try {
byte[] tmp = new byte[BUFFER_SIZE];
int l, count = 0;
// do not send messages if request has been cancelled
while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) {
count += l;
buffer.append(tmp, 0, l);
sendProgressMessage(count, (int) contentLength);
}
} finally {
instream.close();
}
responseBody = buffer.toByteArray();
} catch (OutOfMemoryError e) {
System.gc();
throw new IOException("File too large to fit into available memory");
}
}
}
return responseBody;
}
use of org.apache.http.util.ByteArrayBuffer in project SmartAndroidSource by jaychou2012.
the class DataAsyncHttpResponseHandler method getResponseData.
/**
* Returns byte array of response HttpEntity contents
*
* @param entity can be null
* @return response entity body or null
* @throws java.io.IOException if reading entity or creating byte array failed
*/
@Override
byte[] getResponseData(HttpEntity entity) throws IOException {
byte[] responseBody = null;
if (entity != null) {
InputStream instream = entity.getContent();
if (instream != null) {
long contentLength = entity.getContentLength();
if (contentLength > Integer.MAX_VALUE) {
throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
}
if (contentLength < 0) {
contentLength = BUFFER_SIZE;
}
try {
ByteArrayBuffer buffer = new ByteArrayBuffer((int) contentLength);
try {
byte[] tmp = new byte[BUFFER_SIZE];
int l;
// do not send messages if request has been cancelled
while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) {
buffer.append(tmp, 0, l);
sendProgressDataMessage(copyOfRange(tmp, 0, l));
}
} finally {
AsyncHttpClient.silentCloseInputStream(instream);
}
responseBody = buffer.toByteArray();
} catch (OutOfMemoryError e) {
System.gc();
throw new IOException("File too large to fit into available memory");
}
}
}
return responseBody;
}
use of org.apache.http.util.ByteArrayBuffer in project SeaStar by 13120241790.
the class AsyncHttpResponseHandler method getResponseData.
byte[] getResponseData(HttpEntity entity) throws IOException {
byte[] responseBody = null;
if (entity != null) {
InputStream instream = entity.getContent();
if (instream != null) {
long contentLength = entity.getContentLength();
if (contentLength > Integer.MAX_VALUE) {
throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
}
if (contentLength < 0) {
contentLength = BUFFER_SIZE;
}
try {
ByteArrayBuffer buffer = new ByteArrayBuffer((int) contentLength);
try {
byte[] tmp = new byte[BUFFER_SIZE];
int l, count = 0;
// do not send messages if request has been cancelled
while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) {
count += l;
buffer.append(tmp, 0, l);
sendProgressMessage(count, (int) contentLength);
}
} finally {
instream.close();
}
responseBody = buffer.toByteArray();
} catch (OutOfMemoryError e) {
System.gc();
throw new IOException("File too large to fit into available memory");
}
}
}
return responseBody;
}
Aggregations