@Override public Reader getReader(Resource o) throws IOException { InputStream is = o.getInputStream(); return new BufferedReader(encoding == null ? new InputStreamReader(is) : new InputStreamReader(is, encoding)); } };
/** * zip a resource to an output stream * @param resource the resource to zip * @param zOut the output stream * @throws IOException on error */ protected void zipResource(Resource resource, OutputStream zOut) throws IOException { try (InputStream rIn = resource.getInputStream()) { zipFile(rIn, zOut); } }
/** * Get an InputStream for the Resource. * @return an InputStream containing this Resource's content. * @throws IOException if unable to provide the content of this * Resource as a stream. * @throws UnsupportedOperationException if InputStreams are not * supported for this Resource type. */ @Override public InputStream getInputStream() throws IOException { return getResource().getInputStream(); }
/** * Binary compares the contents of two Resources. * <p> * simple but sub-optimal comparison algorithm. written for working * rather than fast. Better would be a block read into buffers followed * by long comparisons apart from the final 1-7 bytes. * </p> * * @param r1 the Resource whose content is to be compared. * @param r2 the other Resource whose content is to be compared. * @return a negative integer, zero, or a positive integer as the first * argument is less than, equal to, or greater than the second. * @throws IOException if the Resources cannot be read. * @since Ant 1.7 */ private static int binaryCompare(final Resource r1, final Resource r2) throws IOException { try (InputStream in1 = new BufferedInputStream(r1.getInputStream()); InputStream in2 = new BufferedInputStream(r2.getInputStream())) { for (int b1 = in1.read(); b1 != -1; b1 = in1.read()) { final int b2 = in2.read(); if (b1 != b2) { return b1 > b2 ? 1 : -1; } } return in2.read() == -1 ? 0 : -1; } }
/** * Text compares the contents of two Resources. * Ignores different kinds of line endings. * @param r1 the Resource whose content is to be compared. * @param r2 the other Resource whose content is to be compared. * @return a negative integer, zero, or a positive integer as the first * argument is less than, equal to, or greater than the second. * @throws IOException if the Resources cannot be read. * @since Ant 1.7 */ private static int textCompare(final Resource r1, final Resource r2) throws IOException { try (BufferedReader in1 = new BufferedReader(new InputStreamReader(r1.getInputStream())); BufferedReader in2 = new BufferedReader( new InputStreamReader(r2.getInputStream()))) { String expected = in1.readLine(); while (expected != null) { final String actual = in2.readLine(); if (!expected.equals(actual)) { if (actual == null) { return 1; } return expected.compareTo(actual); } expected = in1.readLine(); } return in2.readLine() == null ? 0 : -1; //NOSONAR } }
/** * Load a properties resource. * @param propertyResource the resource to load the properties from. * @return loaded <code>Properties</code> object. * @throws BuildException if the resource could not be found or read. * @since Ant 1.8.0 */ public Properties getProperties(Resource propertyResource) throws BuildException { Properties props = new Properties(); try (InputStream in = propertyResource.getInputStream()) { props.load(in); } catch (IOException e) { throw new BuildException("Property resource (%s) cannot be loaded.", propertyResource.getName()); } return props; }
/** * Get an InputStream for the Resource. * @return an InputStream containing this Resource's content. * @throws IOException if unable to provide the content of this * Resource as a stream. * @throws UnsupportedOperationException if InputStreams are not * supported for this Resource type. * @since Ant 1.7 */ public InputStream getInputStream() throws IOException { if (isReference()) { return getCheckedRef().getInputStream(); } throw new UnsupportedOperationException(); }
/** * Implement the gunzipping. */ @Override protected void extract() { if (srcResource.getLastModified() > dest.lastModified()) { log("Expanding " + srcResource.getName() + " to " + dest.getAbsolutePath()); try (OutputStream out = Files.newOutputStream(dest.toPath()); GZIPInputStream zIn = new GZIPInputStream(srcResource.getInputStream())) { byte[] buffer = new byte[BUFFER_SIZE]; int count = 0; do { out.write(buffer, 0, count); count = zIn.read(buffer, 0, buffer.length); } while (count != -1); } catch (IOException ioe) { String msg = "Problem expanding gzip " + ioe.getMessage(); throw new BuildException(msg, ioe, getLocation()); } } }
/** * Get an InputStream for the Resource. * @return an InputStream containing this Resource's content. * @throws IOException if unable to provide the content of this * Resource as a stream. * @throws UnsupportedOperationException if InputStreams are not * supported for this Resource type. */ @Override public InputStream getInputStream() throws IOException { InputStream in = getResource().getInputStream(); if (in != null) { in = wrapStream(in); } return in; }
/** * Get an InputStream for the Resource. * @return an InputStream containing this Resource's content. * @throws IOException if unable to provide the content of this * Resource as a stream. * @throws UnsupportedOperationException if InputStreams are not * supported for this Resource type. */ @Override public InputStream getInputStream() throws IOException { if (isReferenceOrProxy()) { return getReferencedOrProxied().getInputStream(); } Object o = getObjectValue(); return o == null ? UNSET : new ByteArrayInputStream(String.valueOf(o).getBytes()); }
private Reader open(Resource r) throws IOException { ChainReaderHelper crh = new ChainReaderHelper(); crh.setPrimaryReader(new InputStreamReader( new BufferedInputStream(r.getInputStream()), encoding == null ? Charset.defaultCharset() : Charset.forName(encoding))); crh.setFilterChains(filterChains); crh.setProject(getProject()); return crh.getAssembledReader(); }
/** * Read in templates from the stylesheet */ private void readTemplates() throws IOException, TransformerConfigurationException, ParserConfigurationException, SAXException { // Use a stream so that you can close it yourself quickly // and avoid keeping the handle until the object is garbaged. // (always keep control), otherwise you won't be able to delete // the file quickly on windows. try (InputStream xslStream = new BufferedInputStream(stylesheet.getInputStream())) { templatesModTime = stylesheet.getLastModified(); final Source src = getSource(xslStream, stylesheet); templates = getFactory().newTemplates(src); } }
private static void copyUsingStreams(final Resource source, final Resource dest, final boolean append, final Project project) throws IOException { if (areSame(source, dest)) { // copying the "same" file to itself will corrupt the file, so we skip it log(project, "Skipping (self) copy of " + source + " to " + dest); return; } try (InputStream in = source.getInputStream(); OutputStream out = getOutputStream(dest, append, project)) { final byte[] buffer = new byte[FileUtils.BUF_SIZE]; int count = 0; do { out.write(buffer, 0, count); count = in.read(buffer, 0, buffer.length); } while (count != -1); } }
/** * Returns properties from a specified properties file. * * @param resource The resource to load properties from. */ private Properties getProperties(Resource resource) { InputStream in = null; Properties props = new Properties(); try { in = resource.getInputStream(); props.load(in); } catch (IOException ioe) { if (getProject() != null) { getProject().log("getProperties failed, " + ioe.getMessage(), Project.MSG_ERR); } else { ioe.printStackTrace(); //NOSONAR } } finally { FileUtils.close(in); } return props; }
private void runTransaction(PrintStream out) throws IOException, SQLException { if (!tSqlCommand.isEmpty()) { log("Executing commands", Project.MSG_INFO); runStatements(new StringReader(tSqlCommand), out); } if (tSrcResource != null) { log("Executing resource: " + tSrcResource.toString(), Project.MSG_INFO); Charset charset = encoding == null ? Charset.defaultCharset() : Charset.forName(encoding); try (Reader reader = new InputStreamReader( tSrcResource.getInputStream(), charset)) { runStatements(reader, out); } } } }
/** * Get an InputStream for the Resource. * @return an InputStream containing this Resource's content. * @throws IOException if unable to provide the content of this * Resource as a stream. * @throws UnsupportedOperationException if InputStreams are not * supported for this Resource type. */ public synchronized InputStream getInputStream() throws IOException { if (isReference()) { return getCheckedRef().getInputStream(); } connect(); try { return conn.getInputStream(); } finally { conn = null; } }
private void nextResource() throws IOException { closeCurrent(); while (iter.hasNext()) { Resource r = iter.next(); if (!r.isExists()) { continue; } log("Concatenating " + r.toLongString(), Project.MSG_VERBOSE); try { currentStream = new BufferedInputStream(r.getInputStream()); return; } catch (IOException eyeOhEx) { if (!ignoreErrors) { log("Failed to get input stream for " + r, Project.MSG_ERR); throw eyeOhEx; } } } eof = true; }
/** * fetches information from the named entry inside the archive. */ @Override protected void fetchEntry() { Resource archive = getArchive(); try (TarInputStream i = new TarInputStream(archive.getInputStream())) { TarEntry te = null; while ((te = i.getNextEntry()) != null) { if (te.getName().equals(getName())) { setEntry(te); return; } } } catch (IOException e) { log(e.getMessage(), Project.MSG_DEBUG); throw new BuildException(e); } setEntry(null); }
/** * This method is to be overridden by extending unarchival tasks. * * @param srcR the source resource * @param dir the destination directory * @since Ant 1.7 */ @Override protected void expandResource(Resource srcR, File dir) { if (!srcR.isExists()) { throw new BuildException("Unable to untar " + srcR.getName() + " as the it does not exist", getLocation()); } try (InputStream i = srcR.getInputStream()) { expandStream(srcR.getName(), i, dir); } catch (IOException ioe) { throw new BuildException("Error while expanding " + srcR.getName(), ioe, getLocation()); } }
/** * Return an InputStream for reading the contents of this Resource. * @return an InputStream object. * @throws IOException if the tar file cannot be opened, * or the entry cannot be read. */ @Override public InputStream getInputStream() throws IOException { if (isReference()) { return getCheckedRef().getInputStream(); } Resource archive = getArchive(); final TarInputStream i = new TarInputStream(archive.getInputStream()); TarEntry te; while ((te = i.getNextEntry()) != null) { if (te.getName().equals(getName())) { return i; } } FileUtils.close(i); throw new BuildException("no entry " + getName() + " in " + getArchive()); }