private void writeToFromFile(OutputStream os, RequestParams.FileWrapper wrapper) throws IOException { // Send the meta data. writeMetaData(os, wrapper.file.getName(), wrapper.contentType); int bytesRead; long bytesWritten = 0, totalSize = wrapper.file.length(); // Open the file for reading. FileInputStream in = new FileInputStream(wrapper.file); // Upload the file's contents in Base64. Base64OutputStream bos = new Base64OutputStream(os, Base64.NO_CLOSE | Base64.NO_WRAP); // Read from file until no more data's left to read. while ((bytesRead = in.read(buffer)) != -1) { bos.write(buffer, 0, bytesRead); bytesWritten += bytesRead; progressHandler.sendProgressMessage(bytesWritten, totalSize); } // Close the Base64 output stream. AsyncHttpClient.silentCloseOutputStream(bos); // End the meta data. endMetaData(os); // Safely close the input stream. AsyncHttpClient.silentCloseInputStream(in); }
/** * Deconstructs response into given content handler * * @param entity returned HttpEntity * @return deconstructed response * @throws java.io.IOException * @see org.apache.http.HttpEntity */ @Override protected byte[] getResponseData(HttpEntity entity) throws IOException { if (entity != null) { InputStream instream = entity.getContent(); InputStreamReader inputStreamReader = null; if (instream != null) { try { SAXParserFactory sfactory = SAXParserFactory.newInstance(); SAXParser sparser = sfactory.newSAXParser(); XMLReader rssReader = sparser.getXMLReader(); rssReader.setContentHandler(handler); inputStreamReader = new InputStreamReader(instream, DEFAULT_CHARSET); rssReader.parse(new InputSource(inputStreamReader)); } catch (SAXException e) { Log.e(LOG_TAG, "getResponseData exception", e); } catch (ParserConfigurationException e) { Log.e(LOG_TAG, "getResponseData exception", e); } finally { AsyncHttpClient.silentCloseInputStream(instream); if (inputStreamReader != null) { try { inputStreamReader.close(); } catch (IOException e) { /*ignore*/ } } } } } return null; }
private void writeToFromStream(OutputStream os, RequestParams.StreamWrapper entry) throws IOException { // Send the meta data. writeMetaData(os, entry.name, entry.contentType); int bytesRead; // Upload the file's contents in Base64. Base64OutputStream bos = new Base64OutputStream(os, Base64.NO_CLOSE | Base64.NO_WRAP); // Read from input stream until no more data's left to read. while ((bytesRead = entry.inputStream.read(buffer)) != -1) { bos.write(buffer, 0, bytesRead); } // Close the Base64 output stream. AsyncHttpClient.silentCloseOutputStream(bos); // End the meta data. endMetaData(os); // Close input stream. if (entry.autoClose) { // Safely close the input stream. AsyncHttpClient.silentCloseInputStream(entry.inputStream); } }