private byte[] downloadByteArray(URL url) throws IOException { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestProperty("Cookie", cookies); if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) return (null); InputStream in = conn.getInputStream(); byte[] buf = new byte[conn.getContentLength()]; int read, offset = 0; while ((read = in.read(buf, offset, buf.length - offset)) != -1) { offset += read; } return (buf); }
/** * Given a DICOM object encoded as an XML document in a named file convert it to a list of * attributes. * * @param name the input file containing the XML document * @return the list of DICOM attributes * @exception IOException * @exception SAXException * @exception DicomException */ public AttributeList getAttributeList(String name) throws IOException, SAXException, DicomException { InputStream fi = new FileInputStream(name); BufferedInputStream bi = new BufferedInputStream(fi); AttributeList list = null; try { list = getAttributeList(bi); } finally { bi.close(); fi.close(); } return list; }
/** * Read a URL into SAX events. * * @param systemId system id of the document * @param xmlReceiver receiver to output to * @param parserConfiguration parser configuration * @param handleLexical whether the XML parser must output SAX LexicalHandler events, including * comments */ public static void urlToSAX( String systemId, XMLReceiver xmlReceiver, XMLUtils.ParserConfiguration parserConfiguration, boolean handleLexical) { try { final URL url = URLFactory.createURL(systemId); final InputStream is = url.openStream(); final InputSource inputSource = new InputSource(is); inputSource.setSystemId(systemId); try { inputSourceToSAX(inputSource, xmlReceiver, parserConfiguration, handleLexical); } finally { is.close(); } } catch (IOException e) { throw new OXFException(e); } }
public void downloadFile(String url, String savePath, String fileName) { try { URL theURL = new URL(url); InputStream input = theURL.openStream(); OutputStream output = new FileOutputStream(new File(savePath, fileName)); try { byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) { output.write(buffer, 0, bytesRead); } } catch (Exception e) { debug(e.toString()); } finally { output.close(); } } catch (Exception e) { debug(e.toString()); } }
private int countBytes(InputStream is) { int nrBytes = 0; byte[] buf = new byte[10000]; int len = 0; // StringBuffer sb = new StringBuffer(1000); try { while ((len = is.read(buf)) != -1) { nrBytes += len; // resultCount counts the returned bytes // String temp = new String(buf,0,len); // temp = "\n\n" + temp + "\n\n"; // logger.log(Level.ALL, temp); // sb.append(temp); } } catch (IOException e) { System.err.println("Could not read result from input stream"); } // System.out.println(sb.toString()); return nrBytes; }