/** * copy file to output stream, specify internal buffer size * * @param fileIn copy this file * @param out copy to this stream * @param bufferSize internal buffer size. * @throws java.io.IOException on io error */ static public void copyFileB(File fileIn, OutputStream out, int bufferSize) throws IOException { try (FileInputStream fin = new FileInputStream(fileIn)) { InputStream in = new BufferedInputStream(fin); IO.copyB(in, out, bufferSize); } }
/** * Read the contents from the named URL and place into a String. * * @param urlString the URL to read from. * @return String holding the contents. * @throws IOException if fails */ static public String readURLcontentsWithException(String urlString) throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(20000); copyUrlB(urlString, bout, 20000); return bout.toString(); }
/** * Add an entity for resolution. Specify a local resource, and/or a URL. Look * for the local Resource first. * * @param entityName name of entity, eg the namespace String * @param resourceName resolve using this Resource, found on the class path * @param urlName resolve using this Resource, found on the class path */ static public void initEntity( String entityName, String resourceName, String urlName) { String entity = null; try (InputStream is = ucar.nc2.util.IO.getFileResource( resourceName)) { // try to read from local file resource, eg from catalog.jar ByteArrayOutputStream sbuff = new ByteArrayOutputStream(3000); if (is != null) { IO.copy(is, sbuff); entity = new String(sbuff.toByteArray(), CDM.utf8Charset); logger.debug(" *** entity " + entityName + " mapped to local resource at " + resourceName); } else if (urlName != null) { // otherwise, get from network entity = IO.readURLcontentsWithException(urlName); logger.debug(" *** entity "+entityName+" mapped to remote URL at "+urlName); } } catch (IOException e) { System.out.println(" *** FAILED to map entity "+entityName+" locally at "+resourceName+" or remotely at "+urlName); // e.printStackTrace(); } entityHash.put( entityName, entity); entityHash.put( urlName, entity); // also map it to the remote URL }
/** * Copy an entire directory tree. * * @param fromDirName from this directory (do nothing if not exist) * @param toDirName to this directory (will create if not exist) * @throws java.io.IOException on io error */ static public void copyDirTree(String fromDirName, String toDirName) throws IOException { File fromDir = new File(fromDirName); File toDir = new File(toDirName); if (!fromDir.exists()) return; if (!toDir.exists()) toDir.mkdirs(); for (File f : fromDir.listFiles()) { if (f.isDirectory()) copyDirTree(f.getAbsolutePath(), toDir.getAbsolutePath() + "/" + f.getName()); else copyFile(f.getAbsolutePath(), toDir.getAbsolutePath() + "/" + f.getName()); } }
static private String readContents(InputStream is, String charset, int maxKbytes) throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(1000 * maxKbytes); IO.copy(is, bout, 1000 * maxKbytes); return bout.toString(charset); }
if (is != null) { try (FileOutputStream fout = new FileOutputStream(file)) { IO.copyB(is, fout, buffer_size); // cache it IO.readURLtoFileWithExceptions(uriString, file, buffer_size); try (InputStream fin = new BufferedInputStream( new FileInputStream(file), 50000)) {
private void readHDR() throws IOException { String location = raf.getLocation(); int pos = location.lastIndexOf('.'); String HDRname = location.substring(0, pos) + ".HDR"; String HDRcontents = IO.readFile(HDRname); StringTokenizer stoke = new StringTokenizer(HDRcontents); while (stoke.hasMoreTokens()) { String key = stoke.nextToken(); if (key.equals("ULXMAP")) startx = Float.parseFloat(stoke.nextToken()); else if (key.equals("ULYMAP")) starty = Float.parseFloat(stoke.nextToken()); else stoke.nextToken(); } }
/** * Read the contents from the inputStream and place into a String, * with any error messages put in the return String. * Assume UTF-8 encoding. * * @param is the inputStream to read from. * @return String holding the contents, or an error message. * @throws java.io.IOException on io error */ static public String readContents(InputStream is) throws IOException { return readContents(is, "UTF-8"); }
/** * Read the contents from the named URL and place into a String, * with any error messages put in the return String. * * @param urlString the URL to read from. * @return String holding the contents, or an error message. */ static public String readURLcontents(String urlString) { try { return readURLcontentsWithException(urlString); } catch (IOException e) { return e.getMessage(); } }
private static InvCatalogImpl doOne(InvCatalogFactory fac, String urlString, boolean show) { System.out.println("***read " + urlString); if (show) System.out.println(" original catalog=\n" + IO.readURLcontents(urlString)); try { InvCatalogImpl cat = fac.readXML(new URI(urlString)); StringBuilder buff = new StringBuilder(); boolean isValid = cat.check(buff, false); System.out.println("catalog <" + cat.getName() + "> " + (isValid ? "is" : "is not") + " valid"); System.out.println(" validation output=\n" + buff); // if (show) System.out.println(" parsed catalog=\n" + fac.writeXML(cat)); //System.out.println(" -----\n"+cat.dump()); return cat; } catch (Exception e) { e.printStackTrace(); return null; } }
/** * read the contents from the named URL, write to a file. * * @param urlString the URL to read from. * @param file write to this file * @return status or error message. * @throws IOException if failure */ static public String readURLtoFileWithExceptions(String urlString, File file) throws IOException { return readURLtoFileWithExceptions(urlString, file, default_socket_buffersize); }
static public void copyFile2null(String filenameIn, int buffer) throws IOException { long lenIn = new File(filenameIn).length(); if (debug) System.out.println("read " + filenameIn + " len = " + lenIn); InputStream in = new FileInputStream(filenameIn); long start = System.currentTimeMillis(); IO.copy2null(in, buffer); double took = .001 * (System.currentTimeMillis() - start); in.close(); double rate = lenIn / took / (1000 * 1000); System.out.println(" copy (" + filenameIn + ") took = " + took + " sec; rate = " + rate + "Mb/sec"); }
IO.copyFile(summaryFileLocation, System.out);
/** * Wite the contents from the String to a Stream, * * @param contents String holding the contents. * @param os write to this OutputStream * @throws java.io.IOException on io error */ static public void writeContents(String contents, OutputStream os) throws IOException { ByteArrayInputStream bin = new ByteArrayInputStream(contents.getBytes(CDM.utf8Charset)); IO.copy(bin, os); }
if (is != null) { FileOutputStream fout = new FileOutputStream(file); IO.copyB(is, fout, buffer_size); // cache it fout.close(); InputStream fin = new BufferedInputStream( new FileInputStream(file), 50000); IO.readURLtoFileWithExceptions(uriString, file, buffer_size); InputStream fin = new BufferedInputStream( new FileInputStream(file), 50000); try {
private void readHDR() throws IOException { String location = raf.getLocation(); int pos = location.lastIndexOf('.'); String HDRname = location.substring(0, pos) + ".HDR"; String HDRcontents = IO.readFile(HDRname); StringTokenizer stoke = new StringTokenizer(HDRcontents); while (stoke.hasMoreTokens()) { String key = stoke.nextToken(); if (key.equals("ULXMAP")) startx = Float.parseFloat(stoke.nextToken()); else if (key.equals("ULYMAP")) starty = Float.parseFloat(stoke.nextToken()); else stoke.nextToken(); } }
/** * Read the contents from the inputStream and place into a String, * with any error messages put in the return String. * Assume UTF-8 encoding. * * @param is the inputStream to read from. * @return String holding the contents, or an error message. * @throws java.io.IOException on io error */ static public String readContents(InputStream is) throws IOException { return readContents(is, "UTF-8"); }
/** * Read the contents from the named URL and place into a String, * with any error messages put in the return String. * * @param urlString the URL to read from. * @return String holding the contents, or an error message. */ static public String readURLcontents(String urlString) { try { return readURLcontentsWithException(urlString); } catch (IOException e) { return e.getMessage(); } }