/** * Initializes this session input buffer. * * @param instream the source input stream. * @param buffersize the size of the internal buffer. * @param params HTTP parameters. */ protected void init(final InputStream instream, int buffersize, final HttpParams params) { if (instream == null) { throw new IllegalArgumentException("Input stream may not be null"); } if (buffersize <= 0) { throw new IllegalArgumentException("Buffer size may not be negative or zero"); } if (params == null) { throw new IllegalArgumentException("HTTP parameters may not be null"); } this.instream = instream; this.buffer = new byte[buffersize]; this.bufferpos = 0; this.bufferlen = 0; this.linebuffer = new ByteArrayBuffer(buffersize); this.charset = Charset.forName(HttpProtocolParams.getHttpElementCharset(params)); this.ascii = this.charset.equals(ASCII); this.decoder = null; this.maxLineLen = params.getIntParameter(CoreConnectionPNames.MAX_LINE_LENGTH, -1); this.minChunkLimit = params.getIntParameter(CoreConnectionPNames.MIN_CHUNK_LIMIT, 512); this.metrics = createTransportMetrics(); this.onMalformedInputAction = HttpProtocolParams.getMalformedInputAction(params); this.onUnMappableInputAction = HttpProtocolParams.getUnmappableInputAction(params); }
/** * Initializes this session output buffer. * * @param outstream the destination output stream. * @param buffersize the size of the internal buffer. * @param params HTTP parameters. */ protected void init(final OutputStream outstream, int buffersize, final HttpParams params) { if (outstream == null) { throw new IllegalArgumentException("Input stream may not be null"); } if (buffersize <= 0) { throw new IllegalArgumentException("Buffer size may not be negative or zero"); } if (params == null) { throw new IllegalArgumentException("HTTP parameters may not be null"); } this.outstream = outstream; this.buffer = new ByteArrayBuffer(buffersize); this.charset = Charset.forName(HttpProtocolParams.getHttpElementCharset(params)); this.ascii = this.charset.equals(ASCII); this.encoder = null; this.minChunkLimit = params.getIntParameter(CoreConnectionPNames.MIN_CHUNK_LIMIT, 512); this.metrics = createTransportMetrics(); this.onMalformedInputAction = HttpProtocolParams.getMalformedInputAction(params); this.onUnMappableInputAction = HttpProtocolParams.getUnmappableInputAction(params); }
/** * Creates new instance of SessionInputBufferImpl. * * @param metrics HTTP transport metrics. * @param buffersize buffer size. Must be a positive number. * @param minChunkLimit size limit below which data chunks should be buffered in memory * in order to minimize native method invocations on the underlying network socket. * The optimal value of this parameter can be platform specific and defines a trade-off * between performance of memory copy operations and that of native method invocation. * If negative default chunk limited will be used. * @param constraints Message constraints. If <code>null</code> * {@link MessageConstraints#DEFAULT} will be used. * @param chardecoder chardecoder to be used for decoding HTTP protocol elements. * If <code>null</code> simple type cast will be used for byte to char conversion. */ public SessionInputBufferImpl( final HttpTransportMetricsImpl metrics, final int buffersize, final int minChunkLimit, final MessageConstraints constraints, final CharsetDecoder chardecoder) { Args.notNull(metrics, "HTTP transport metrcis"); Args.positive(buffersize, "Buffer size"); this.metrics = metrics; this.buffer = new byte[buffersize]; this.bufferpos = 0; this.bufferlen = 0; this.minChunkLimit = minChunkLimit >= 0 ? minChunkLimit : 512; this.constraints = constraints != null ? constraints : MessageConstraints.DEFAULT; this.linebuffer = new ByteArrayBuffer(buffersize); this.decoder = chardecoder; }
private byte[] extractResponseBytes(InputStream is) throws IOException { BufferedInputStream bis = null; BufferedReader br = null; try { ByteArrayBuffer baf = new ByteArrayBuffer(1024); bis = new BufferedInputStream(is); int b = 0; while ((b = bis.read()) != -1) baf.append((byte) b); byte[] bytes = baf.toByteArray(); return bytes; } finally { if (bis != null) bis.close(); if (br != null) br.close(); } }
void doWriteTo( final OutputStream out, final boolean writeContent) throws IOException { final ByteArrayBuffer boundaryEncoded = encode(this.charset, this.boundary); for (final FormBodyPart part: getBodyParts()) { writeBytes(TWO_DASHES, out); writeBytes(boundaryEncoded, out); writeBytes(CR_LF, out); formatMultipartHeader(part, out); writeBytes(CR_LF, out); if (writeContent) { part.getBody().writeTo(out); } writeBytes(CR_LF, out); } writeBytes(TWO_DASHES, out); writeBytes(boundaryEncoded, out); writeBytes(TWO_DASHES, out); writeBytes(CR_LF, out); }
public static String getResponse(String reqUrl) throws Exception { final URL url = new URL(reqUrl); final URLConnection ucon = url.openConnection(); /* Define InputStreams to read * from the URLConnection. */ final InputStream is = ucon.getInputStream(); final BufferedInputStream bis = new BufferedInputStream(is); /* Read bytes to the Buffer until * there is nothing more to read(-1). */ final ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while((current = bis.read()) != -1){ baf.append((byte)current); } return new String(baf.toByteArray()); }
private byte[] getLogoImage(String url) { try { URL imageUrl = new URL(url); URLConnection ucon = imageUrl.openConnection(); InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(500); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } return baf.toByteArray(); } catch (Exception e) { Log.d("ImageManager", "Error: " + e.toString()); return null; } }
public static String DownloadFromUrl(String u) { try { URL url = new URL(u); URLConnection ucon = url.openConnection(); InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } return new String(baf.toByteArray()); } catch (Exception e) { Log.e(TAG, "Error: " + e); } return null; }
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(); }
/** * 将inputStream 以系统默认编码转换为字符串 * * @param 字节流 * */ public String inputStreamToString(InputStream is) throws IOException { byte arr[] = new byte[1024]; ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(1024); int len = 0; try { while ((len = is.read(arr)) != -1) { byteArrayBuffer.append(arr, 0, len); } } finally { try { is.close(); } catch (IOException e) { // do nothing } } return new String(byteArrayBuffer.toByteArray()); }
public static String getHtmlString(String urlString) { try { URL url = new URL(urlString); URLConnection ucon = url.openConnection(); InputStream instr = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(instr); ByteArrayBuffer baf = new ByteArrayBuffer(500); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } return EncodingUtils.getString(baf.toByteArray(), "utf-8"); } catch (Exception e) { Log.d("win","lllll"+e.toString()); return ""; } }
public static ByteArrayBuffer ʻ(String paramString) { HttpGet localHttpGet = new HttpGet(paramString); DefaultHttpClient localDefaultHttpClient = ˊ(); BufferedHttpEntity localBufferedHttpEntity = new BufferedHttpEntity(localDefaultHttpClient.execute(localHttpGet).getEntity()); BufferedInputStream localBufferedInputStream = new BufferedInputStream(localBufferedHttpEntity.getContent()); ByteArrayBuffer localByteArrayBuffer = new ByteArrayBuffer(50); while (true) { int i = localBufferedInputStream.read(); if (i == -1) break; localByteArrayBuffer.append((byte)i); } localBufferedInputStream.close(); localBufferedHttpEntity.consumeContent(); localDefaultHttpClient.getConnectionManager().shutdown(); return localByteArrayBuffer; }
@ィ(ˊ="DOWNLOAD_ICON_ASSET") public final void downloadIconFromUrl(String paramString, File paramFile) { int i = paramString.hashCode(); if (!this.ˎ.contains(Integer.valueOf(i))) { this.ˎ.add(Integer.valueOf(i)); try { ByteArrayBuffer localByteArrayBuffer = 〳.ʻ(paramString); paramFile.getParentFile().mkdirs(); FileOutputStream localFileOutputStream = new FileOutputStream(paramFile); localFileOutputStream.write(localByteArrayBuffer.toByteArray()); localFileOutputStream.close(); } catch (IOException localIOException) { } this.ˎ.remove(Integer.valueOf(i)); } }
public static void ˊ(Context paramContext, ByteArrayBuffer paramByteArrayBuffer, String paramString1, String paramString2) { File localFile = new File(paramContext.getCacheDir(), String.valueOf((paramString1 + paramString2).hashCode())); try { FileOutputStream localFileOutputStream = new FileOutputStream(localFile); localFileOutputStream.write(paramByteArrayBuffer.toByteArray()); localFileOutputStream.close(); return; } catch (FileNotFoundException localFileNotFoundException) { localFileNotFoundException.printStackTrace(); return; } catch (IOException localIOException) { localIOException.printStackTrace(); } }
private static ByteArrayBuffer ˊ(String[] paramArrayOfString) { try { ByteArrayBuffer localByteArrayBuffer = 〳.ʻ(paramArrayOfString[0]); return localByteArrayBuffer; } catch (IOException localIOException) { localIOException.printStackTrace(); return null; } catch (Exception localException) { localException.printStackTrace(); } return null; }
public byte[] getImageByUrl(String url) { try { URL imageUrl = new URL(url); URLConnection ucon = imageUrl.openConnection(); InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(500); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } return baf.toByteArray(); } catch (Exception e) { Log.d("ImageManager", "Error: " + e.toString()); } return null; }
private void writeFile(File outputFile, InputStream is) throws IOException { Log.d("Downloading", "Writing file to " + outputFile.getAbsoluteFile()); BufferedInputStream bufferinstream = new BufferedInputStream(is); int bufferSize = 1024; ByteArrayBuffer baf = new ByteArrayBuffer(bufferSize); int current = 0; totalSize = 0; while((current = bufferinstream.read()) != -1){ totalSize += 1; baf.append((byte) current); publishProgress(outputFile); //TODO: Need a way to determine if server has suddenly died } bufferinstream.close(); Log.d("Downloading", "Writing fos"); FileOutputStream fos = new FileOutputStream(outputFile); fos.write(baf.toByteArray()); fos.flush(); fos.close(); }
@Override public byte [] externalize(){ //TODO use byte buffer ByteArrayBuffer bb1 = new ByteArrayBuffer(100); bb1.append('C'); byte [] cr = String.valueOf(getCreateTime()).getBytes(); bb1.append(cr, 0 , cr.length); bb1.append(SsTableReader.END_COLUMN_PART); cr = String.valueOf(getTime()).getBytes(); bb1.append(cr, 0 , cr.length); bb1.append(SsTableReader.END_COLUMN_PART); cr = String.valueOf(getTtl()).getBytes(); bb1.append(cr,0,cr.length); bb1.append(SsTableReader.END_COLUMN_PART); cr = String.valueOf(getValue()).getBytes(); bb1.append(SsTableReader.END_COLUMN_PART); bb1.append(cr,0,cr.length); return bb1.toByteArray(); }
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(); }
public String downloadFromUrl(String urlStr) { try { URL url = new URL(urlStr); URLConnection urlConnection = url.openConnection(); InputStream inputStream = urlConnection.getInputStream(); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(50); int current; while ((current = bufferedInputStream.read()) != -1) { byteArrayBuffer.append((byte) current); } return new String(byteArrayBuffer.toByteArray()); } catch (IOException e) { Log.d("DataDownloader", "Error: " + e); } return ""; }
/** * @param in * @throws IOException */ private String readStream(InputStream in) throws IOException { // prepare the input buffer BufferedInputStream bis = new BufferedInputStream(in); ByteArrayBuffer baf = new ByteArrayBuffer(1000); int read = 0; int bufSize = 1024; byte[] buffer = new byte[bufSize]; // read the stream while (true) { read = bis.read(buffer); if (read == -1) { break; } baf.append(buffer, 0, read); } String queryResult = new String(baf.toByteArray()); // return result return queryResult; }
private static ByteArrayBuffer encode( final Charset charset, final String string) { ByteBuffer encoded = charset.encode(CharBuffer.wrap(string)); ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining()); bab.append(encoded.array(), encoded.position(), encoded.remaining()); return bab; }
private void doWriteTo(HttpMultipartMode mode, OutputStream out, boolean writeContent) throws IOException { ByteArrayBuffer boundary = encode(this.charset, getBoundary()); for (FormBodyPart part : this.parts) { writeBytes(TWO_DASHES, out); writeBytes(boundary, out); writeBytes(CR_LF, out); Header header = part.getHeader(); switch (mode) { case STRICT: Iterator i$ = header.iterator(); while (i$.hasNext()) { writeField((MinimalField) i$.next(), out); } break; case BROWSER_COMPATIBLE: writeField(part.getHeader().getField("Content-Disposition"), this.charset, out); if (part.getBody().getFilename() != null) { writeField(part.getHeader().getField("Content-Type"), this.charset, out); break; } break; } writeBytes(CR_LF, out); if (writeContent) { part.getBody().writeTo(out); } writeBytes(CR_LF, out); } writeBytes(TWO_DASHES, out); writeBytes(boundary, out); writeBytes(TWO_DASHES, out); writeBytes(CR_LF, out); }
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 > 2147483647L) { throw new IllegalArgumentException("HTTP entity too large to be buffered in " + "memory"); } if (contentLength < 0) { contentLength = PlaybackStateCompat.ACTION_SKIP_TO_QUEUE_ITEM; } try { ByteArrayBuffer buffer = new ByteArrayBuffer((int) contentLength); byte[] tmp = new byte[4096]; while (true) { int l = instream.read(tmp); if (l == -1 || Thread.currentThread().isInterrupted()) { break; } buffer.append(tmp, 0, l); sendProgressDataMessage(copyOfRange(tmp, 0, l)); sendProgressMessage((long) 0, contentLength); } AsyncHttpClient.silentCloseInputStream(instream); responseBody = buffer.toByteArray(); } catch (OutOfMemoryError e) { System.gc(); throw new IOException("File too large to fit into available memory"); } catch (Throwable th) { AsyncHttpClient.silentCloseInputStream(instream); } } } return responseBody; }
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; }
/** * 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 <= 0 ? 1 : contentLength)); } } finally { AsyncHttpClient.silentCloseInputStream(instream); AsyncHttpClient.endEntityViaReflection(entity); } responseBody = buffer.toByteArray(); } catch (OutOfMemoryError e) { System.gc(); throw new IOException("File too large to fit into available memory"); } } } return responseBody; }
/** * 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; }
/** * Returns byte array of response HttpEntity contents * * @param entity can be null * @return response entity body or null * @throws 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 <= 0 ? 1 : contentLength)); } } finally { AsyncHttpClient.silentCloseInputStream(instream); AsyncHttpClient.endEntityViaReflection(entity); } responseBody = buffer.toByteArray(); } catch (OutOfMemoryError e) { System.gc(); throw new IOException("File too large to fit into available memory"); } } } return responseBody; }
/** * Returns byte array of response HttpEntity contents * * @param entity can be null * @return response entity body or null * @throws 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; }
private void doWriteTo(final HttpMultipartMode mode, final OutputStream out, boolean writeContent) throws IOException { ByteArrayBuffer boundary = encode(this.charset, getBoundary()); for (FormBodyPart part : this.parts) { writeBytes(TWO_DASHES, out); writeBytes(boundary, out); writeBytes(CR_LF, out); Header header = part.getHeader(); switch (mode) { case STRICT: for (MinimalField field : header) { writeField(field, out); } break; case BROWSER_COMPATIBLE: // Only write Content-Disposition // Use content charset MinimalField cd = part.getHeader().getField(MIME.CONTENT_DISPOSITION); writeField(cd, this.charset, out); String filename = part.getBody().getFilename(); if (filename != null) { MinimalField ct = part.getHeader().getField(MIME.CONTENT_TYPE); writeField(ct, this.charset, out); } break; } writeBytes(CR_LF, out); if (writeContent) { part.getBody().writeTo(out); } writeBytes(CR_LF, out); } writeBytes(TWO_DASHES, out); writeBytes(boundary, out); writeBytes(TWO_DASHES, out); writeBytes(CR_LF, out); }