本文整理匯總了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());
}