本文整理汇总了Java中org.apache.http.util.ByteArrayBuffer.buffer方法的典型用法代码示例。如果您正苦于以下问题:Java ByteArrayBuffer.buffer方法的具体用法?Java ByteArrayBuffer.buffer怎么用?Java ByteArrayBuffer.buffer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.util.ByteArrayBuffer
的用法示例。
在下文中一共展示了ByteArrayBuffer.buffer方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: hexToBytes
import org.apache.http.util.ByteArrayBuffer; //导入方法依赖的package包/类
public static byte[] hexToBytes(String hex) {
ByteArrayBuffer bytes = new ByteArrayBuffer(hex.length() / 2);
for (int i = 0; i < hex.length(); i++) {
if (hex.charAt(i) == ' ') {
continue;
}
String hexByte;
if (i + 1 < hex.length()) {
hexByte = hex.substring(i, i + 2).trim();
i++;
} else {
hexByte = hex.substring(i, i + 1);
}
bytes.append(Integer.parseInt(hexByte, 16));
}
return bytes.buffer();
}
示例2: toByteArray
import org.apache.http.util.ByteArrayBuffer; //导入方法依赖的package包/类
public static byte[] toByteArray(ProtocolPacket packet) throws UnsupportedEncodingException {
ByteArrayBuffer buffer = new ByteArrayBuffer(0);
putBytesInBuffer(buffer, toLimitLengthString(packet.getSerialId()));
putBytesInBuffer(buffer, toLEByteArray(packet.getProtocolId()));
putBytesInBuffer(buffer, toLEByteArray(packet.getDateTime()));
putBytesInBuffer(buffer, toLEByteArray(packet.getLongitude()));
putBytesInBuffer(buffer, toLEByteArray(packet.getLatitude()));
putBytesInBuffer(buffer, toLEByteArray(packet.getSpeed()));
putBytesInBuffer(buffer, toLEByteArray(packet.getCourse()));
putBytesInBuffer(buffer, packet.getDigitIO());
putBytesInBuffer(buffer, packet.getAnalogChannel1());
putBytesInBuffer(buffer, packet.getAnalogChannel2());
putBytesInBuffer(buffer, packet.getStatus0());
putBytesInBuffer(buffer, packet.getStatus1());
putBytesInBuffer(buffer, getDataLength(packet.getData()));
putBytesInBuffer(buffer, packet.getData());
return buffer.buffer();
}
示例3: toByteArray
import org.apache.http.util.ByteArrayBuffer; //导入方法依赖的package包/类
/**
* HttpEntity到 byte数组的转换
*
* @param entity HttpEntity对象
* @return byte[] byte数组对象
* @throws IOException
*/
private byte[] toByteArray(final HttpEntity entity) throws IOException {
if (entity == null) {
throw new LibRuntimeException(LibResultCode.E_PARAM_ERROR, "", "Null HttpEntity", ServiceProvider.None);
}
InputStream instream = entity.getContent();
if (instream == null) {
return new byte[] {};
}
if (entity.getContentLength() > Integer.MAX_VALUE) {
throw new LibRuntimeException(LibResultCode.E_PARAM_ERROR, "", "HTTP entity too large to be buffered in memory", ServiceProvider.None);
}
Logger.verbose("ByteArrayResponseHandler: content-type{}", entity.getContentType());
int i = (int) entity.getContentLength();
if (i < 0) {
i = 4096;
}
ByteArrayBuffer buffer = new ByteArrayBuffer(i);
try {
byte[] tmp = new byte[4096];
int l;
while ((l = instream.read(tmp)) != -1) {
buffer.append(tmp, 0, l);
}
} finally {
instream.close();
}
return buffer.buffer();
}
示例4: toByteArray
import org.apache.http.util.ByteArrayBuffer; //导入方法依赖的package包/类
/**
* HttpEntity到 byte数组的转换
*
* @param entity HttpEntity对象
* @return byte[] byte数组对象
* @throws IOException
*/
private byte[] toByteArray(final HttpEntity entity) throws IOException {
if (entity == null) {
throw new LibRuntimeException(ExceptionCode.PARAMETER_ERROR, "", "Null HttpEntity", ServiceProvider.None);
}
InputStream instream = entity.getContent();
if (instream == null) {
return new byte[] {};
}
if (entity.getContentLength() > Integer.MAX_VALUE) {
throw new LibRuntimeException(ExceptionCode.PARAMETER_ERROR, "", "HTTP entity too large to be buffered in memory", ServiceProvider.None);
}
if (Constants.DEBUG) {
logger.debug("{}", entity.getContentType().toString());
}
int i = (int) entity.getContentLength();
if (i < 0) {
i = 4096;
}
ByteArrayBuffer buffer = new ByteArrayBuffer(i);
try {
byte[] tmp = new byte[4096];
int l;
while ((l = instream.read(tmp)) != -1) {
buffer.append(tmp, 0, l);
}
} finally {
instream.close();
}
return buffer.buffer();
}
示例5: internalExecute
import org.apache.http.util.ByteArrayBuffer; //导入方法依赖的package包/类
protected InputStream internalExecute(URL url, ProgressListener progressListener)
throws IOException
{
// create and configure connection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
configureConnection(conn);
conn.connect();
// manage possible redirections
responseCode = conn.getResponseCode();
headerFields = conn.getHeaderFields();
if (responseCode / 100 != 2) {
conn.disconnect();
if(responseCode / 100 == 3) { // try redirection
redirectionUrl = conn.getHeaderField("Location");
final String redirectionString = "Redirection code " + responseCode + " received -> " + redirectionUrl;
Logger.debug(getClass(), redirectionString);
if(followRedirects) {
URL newURL = new URL(redirectionUrl);
return internalExecute(newURL, progressListener);
} else {
throw new HttpResponseException(responseCode, redirectionString);
}
}
throw new HttpResponseException(responseCode, "Error connecting to ["+url+"], response code: "+responseCode);
}
totalBytes = conn.getContentLength(); // could be 0 or -1, be careful!!!
int bufferSize = DEFAULT_BUFFER_SIZE;
if(totalBytes > 0 && totalBytes<bufferSize) {
bufferSize = totalBytes;
}
ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(bufferSize);
InputStream remoteStream = conn.getInputStream();
byte [] tempArray = new byte[REMOTE_READ_BYTES];
int readBytes = remoteStream.read(tempArray);
totalBytes = readBytes>=0 ? readBytes : 0;
while(readBytes>0) {
byteArrayBuffer.append(tempArray, 0, readBytes);
readBytes = remoteStream.read(tempArray);
if(readBytes > 0) {
totalBytes += readBytes;
}
if(progressListener != null) {
progressListener.onProgressChanged(totalBytes, totalBytes);
}
}
tempArray = null;
remoteStream.close();
conn.disconnect();
return new ByteArrayInputStream(byteArrayBuffer.buffer());
}