本文整理汇总了Java中org.apache.http.util.ByteArrayBuffer.length方法的典型用法代码示例。如果您正苦于以下问题:Java ByteArrayBuffer.length方法的具体用法?Java ByteArrayBuffer.length怎么用?Java ByteArrayBuffer.length使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.util.ByteArrayBuffer
的用法示例。
在下文中一共展示了ByteArrayBuffer.length方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toByteArray
import org.apache.http.util.ByteArrayBuffer; //导入方法依赖的package包/类
private static final byte[] toByteArray(final HttpEntity entity,
int maxContent, MutableBoolean trimmed) throws IOException {
if (entity == null)
return new byte[] {};
final InputStream instream = entity.getContent();
if (instream == null) {
return null;
}
Args.check(entity.getContentLength() <= Integer.MAX_VALUE,
"HTTP entity too large to be buffered in memory");
int reportedLength = (int) entity.getContentLength();
// set default size for buffer: 100 KB
int bufferInitSize = 102400;
if (reportedLength != -1) {
bufferInitSize = reportedLength;
}
// avoid init of too large a buffer when we will trim anyway
if (maxContent != -1 && bufferInitSize > maxContent) {
bufferInitSize = maxContent;
}
final ByteArrayBuffer buffer = new ByteArrayBuffer(bufferInitSize);
final byte[] tmp = new byte[4096];
int lengthRead;
while ((lengthRead = instream.read(tmp)) != -1) {
// check whether we need to trim
if (maxContent != -1 && buffer.length() + lengthRead > maxContent) {
buffer.append(tmp, 0, maxContent - buffer.length());
trimmed.setValue(true);
break;
}
buffer.append(tmp, 0, lengthRead);
}
return buffer.toByteArray();
}
示例2: doWriteTo
import org.apache.http.util.ByteArrayBuffer; //导入方法依赖的package包/类
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("stop");
}
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);
}
示例3: doWriteTo
import org.apache.http.util.ByteArrayBuffer; //导入方法依赖的package包/类
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);
}
示例4: toByteArray
import org.apache.http.util.ByteArrayBuffer; //导入方法依赖的package包/类
private final byte[] toByteArray(final ResponseBody responseBody,
MutableBoolean trimmed) throws IOException {
if (responseBody == null)
return new byte[] {};
final InputStream instream = responseBody.byteStream();
if (instream == null) {
return null;
}
if (responseBody.contentLength() > Integer.MAX_VALUE) {
throw new IOException(
"Cannot buffer entire body for content length: "
+ responseBody.contentLength());
}
int reportedLength = (int) responseBody.contentLength();
// set default size for buffer: 100 KB
int bufferInitSize = 102400;
if (reportedLength != -1) {
bufferInitSize = reportedLength;
}
// avoid init of too large a buffer when we will trim anyway
if (maxContent != -1 && bufferInitSize > maxContent) {
bufferInitSize = maxContent;
}
long endDueFor = -1;
if (completionTimeout != -1) {
endDueFor = System.currentTimeMillis() + (completionTimeout * 1000);
}
final ByteArrayBuffer buffer = new ByteArrayBuffer(bufferInitSize);
final byte[] tmp = new byte[4096];
int lengthRead;
while ((lengthRead = instream.read(tmp)) != -1) {
// check whether we need to trim
if (maxContent != -1 && buffer.length() + lengthRead > maxContent) {
buffer.append(tmp, 0, maxContent - buffer.length());
trimmed.setValue(true);
break;
}
buffer.append(tmp, 0, lengthRead);
// check whether we hit the completion timeout
if (endDueFor != -1 && endDueFor <= System.currentTimeMillis()) {
trimmed.setValue(true);
break;
}
}
return buffer.toByteArray();
}