List of usage examples for org.apache.http.util ByteArrayBuffer clear
public void clear()
From source file:crow.weibo.util.WeiboUtil.java
public static String multipartPost(HttpURLConnection conn, List<PostParameter> params) throws IOException { OutputStream os;//from w ww .j a v a 2 s. c o m List<PostParameter> dataparams = new ArrayList<PostParameter>(); for (PostParameter key : params) { if (key.isFile()) { dataparams.add(key); } } String BOUNDARY = Util.md5(String.valueOf(System.currentTimeMillis())); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Charsert", "UTF-8"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + BOUNDARY); ByteArrayBuffer buff = new ByteArrayBuffer(1000); for (PostParameter p : params) { byte[] arr = p.toMultipartByte(BOUNDARY, "UTF-8"); buff.append(arr, 0, arr.length); } String end = "--" + BOUNDARY + "--" + "\r\n"; byte[] endArr = end.getBytes(); buff.append(endArr, 0, endArr.length); conn.setRequestProperty("Content-Length", buff.length() + ""); conn.connect(); os = new BufferedOutputStream(conn.getOutputStream()); os.write(buff.toByteArray()); buff.clear(); os.flush(); String response = ""; response = Util.inputStreamToString(conn.getInputStream()); return response; }
From source file:org.simalliance.openmobileapi.service.security.AccessControlApplet.java
public boolean readAPCertificate(ByteArrayBuffer bytes, int offset, int length) throws AccessControlException, CardException { CommandApdu apdu = mReadAPCertificate.clone(); apdu.setP1(offset >> 8 & 0xFF); apdu.setP2(offset & 0xFF);//from www . jav a 2 s .c om apdu.setLe(length & 0xFF); ResponseApdu response = send(apdu); byte[] data = response.getData(); bytes.clear(); bytes.append(data, 0, data.length); if (response.getSW1SW2() == 0x6A86) { return true; } response.checkStatus(new int[] { 0x9000, 0x6A82 }, "READ AP CERTIFICATE"); return false; }
From source file:com.vkassin.mtrade.Common.java
public static String generalWebServiceCall(String urlStr, ContentHandler handler) { String errorMsg = ""; try {//from w w w. j a v a 2 s. com URL url = new URL(urlStr); HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); urlc.setRequestProperty("User-Agent", "Android Application: aMTrade"); urlc.setRequestProperty("Connection", "close"); // urlc.setRequestProperty("Accept-Charset", "windows-1251"); // urlc.setRequestProperty("Accept-Charset", // "windows-1251,utf-8;q=0.7,*;q=0.7"); urlc.setRequestProperty("Accept-Charset", "utf-8"); urlc.setConnectTimeout(1000 * 5); // mTimeout is in seconds urlc.setDoInput(true); urlc.connect(); if (urlc.getResponseCode() == HttpURLConnection.HTTP_OK) { // Get a SAXParser from the SAXPArserFactory. SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); // Get the XMLReader of the SAXParser we created. XMLReader xr = sp.getXMLReader(); // Apply the handler to the XML-Reader xr.setContentHandler(handler); // Parse the XML-data from our URL. InputStream is = urlc.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(500); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } ByteArrayInputStream bais = new ByteArrayInputStream(baf.toByteArray()); // Reader isr = new InputStreamReader(bais, "windows-1251"); Reader isr = new InputStreamReader(bais, "utf-8"); InputSource ist = new InputSource(); // ist.setEncoding("UTF-8"); ist.setCharacterStream(isr); xr.parse(ist); // Parsing has finished. bis.close(); baf.clear(); bais.close(); is.close(); } urlc.disconnect(); } catch (SAXException e) { // All is OK :) } catch (MalformedURLException e) { Log.e(TAG, errorMsg = "MalformedURLException"); } catch (IOException e) { Log.e(TAG, errorMsg = "IOException"); } catch (ParserConfigurationException e) { Log.e(TAG, errorMsg = "ParserConfigurationException"); } catch (ArrayIndexOutOfBoundsException e) { Log.e(TAG, errorMsg = "ArrayIndexOutOfBoundsException"); } return errorMsg; }
From source file:com.juick.android.WsClient.java
public void readLoop() { try {/*from ww w . j a v a2s . c o m*/ int b; int byteCnt = 0; boolean bigPacket = false; int PacketLength = 0; ByteArrayBuffer buf = new ByteArrayBuffer(16); boolean flagInside = false; while (!terminated) { try { b = is.read(); if (b == -1) break; if (terminated) break; } catch (SocketTimeoutException e) { if (beforePausedCounter-- < 0) { sock.setSoTimeout(3 * 60 * 1000); } Log.w("UgnichWS", "inst=" + toString() + ": read sotimeout, terminated=" + terminated); continue; } if (flagInside) { byteCnt++; if (byteCnt == 1) { if (b < 126) { PacketLength = b + 1; bigPacket = false; } else { bigPacket = true; } } else { if (byteCnt == 2 && bigPacket) { PacketLength = b << 8; } if (byteCnt == 3 && bigPacket) { PacketLength |= b; PacketLength += 3; } if (byteCnt > 3 || !bigPacket) { buf.append((char) b); } } if (byteCnt == PacketLength && listener != null) { if (PacketLength > 2) { if (listener != null) { String incomingData = new String(buf.toByteArray(), "utf-8"); final ArrayList<JuickMessage> messages = convertMessages(incomingData); if (messages.size() > 0) { listener.onNewMessages(messages); } } } else { os.write(keepAlive); os.flush(); } flagInside = false; } } else if (b == 0x81) { buf.clear(); flagInside = true; byteCnt = 0; } } } catch (Exception e) { Log.e("UgnichWS", "inst=" + toString(), e); } finally { Log.w("UgnichWS", "inst=" + toString() + " DISCONNECTED readLoop"); } }