/** * Read the content of the Path and returns it as a byte[]. * * @param self the file whose content we want to read * @return a String containing the content of the file * @throws java.io.IOException if an IOException occurs. * @since 2.3.0 */ public static byte[] getBytes(Path self) throws IOException { return IOGroovyMethods.getBytes(Files.newInputStream(self)); }
/** * Overloads the left shift operator to provide an append mechanism to * add things to the output stream of a socket * * @param self a Socket * @param value a value to append * @return a Writer * @throws IOException if an IOException occurs. * @since 1.0 */ public static Writer leftShift(Socket self, Object value) throws IOException { return IOGroovyMethods.leftShift(self.getOutputStream(), value); }
/** * Write the bytes from the byte array to the Path. * * @param self the file to write to * @param bytes the byte[] to write to the file * @throws java.io.IOException if an IOException occurs. * @since 2.3.0 */ public static void setBytes(Path self, byte[] bytes) throws IOException { IOGroovyMethods.setBytes(Files.newOutputStream(self), bytes); }
/** * Reads the stream into a list, with one element for each line. * * @param stream a stream * @param charset opens the stream with a specified charset * @return a List of lines * @throws IOException if an IOException occurs. * @see #readLines(java.io.Reader) * @since 1.6.8 */ public static List<String> readLines(InputStream stream, String charset) throws IOException { return readLines(newReader(stream, charset)); }
/** * Create a new ObjectInputStream for this file and pass it to the closure. * This method ensures the stream is closed after the closure returns. * * @param inputStream an input stream * @param classLoader the class loader to use when loading the class * @param closure a closure * @return the value returned by the closure * @throws IOException if an IOException occurs. * @see #withStream(java.io.InputStream, groovy.lang.Closure) * @since 1.5.0 */ public static <T> T withObjectInputStream(InputStream inputStream, ClassLoader classLoader, @ClosureParams(value=SimpleType.class, options="java.io.ObjectInputStream") Closure<T> closure) throws IOException { return withStream(newObjectInputStream(inputStream, classLoader), closure); }
/** * Filter lines from an input stream using a closure predicate. The closure * will be passed each line as a String, and it should return * <code>true</code> if the line should be passed to the writer. * * @param self an input stream * @param predicate a closure which returns boolean and takes a line * @return a writable which writes out the filtered lines * @see #filterLine(java.io.Reader, groovy.lang.Closure) * @since 1.0 */ public static Writable filterLine(InputStream self, @ClosureParams(value=SimpleType.class, options="java.lang.String") Closure predicate) { return filterLine(newReader(self), predicate); }
@Deprecated public static String getText(InputStream is) throws IOException { return IOGroovyMethods.getText(is); }
@Override public void save(Client client, InputStream stream) throws IOException { String path = client.getFixtureLocation(fileName); String testResourcesFromSystem = System.getProperty(AbstractContentMinion.TEST_RESOURCES_FOLDER_PROPERTY_NAME); File file = new File(testResourcesFromSystem != null ? testResourcesFromSystem : "src/test/resources", path); file.getParentFile().mkdirs(); try (FileOutputStream out = new FileOutputStream(file)) { IOGroovyMethods.setBytes(out, IOGroovyMethods.getBytes(stream)); } }
@Deprecated public static <T> T splitEachLine(Reader self, String regex, Closure<T> closure) throws IOException { return IOGroovyMethods.splitEachLine(self, regex, closure); }
@Deprecated public static <T> T eachLine(InputStream stream, String charset, Closure<T> closure) throws IOException { return IOGroovyMethods.eachLine(stream, charset, closure); }
@Deprecated public static BufferedReader newReader(InputStream self) { return IOGroovyMethods.newReader(self); }
/** * Create an object input stream for this file using the given class loader. * * @param file a file * @param classLoader the class loader to use when loading the class * @return an object input stream * @throws IOException if an IOException occurs. * @since 1.5.0 */ public static ObjectInputStream newObjectInputStream(File file, final ClassLoader classLoader) throws IOException { return IOGroovyMethods.newObjectInputStream(new FileInputStream(file), classLoader); }
/** * Iterates through the given file object by object. * * @param self a {@code Path} object * @param closure a closure * @throws java.io.IOException if an IOException occurs. * @throws ClassNotFoundException if the class is not found. * @see org.codehaus.groovy.runtime.IOGroovyMethods#eachObject(java.io.ObjectInputStream, groovy.lang.Closure) * @since 2.3.0 */ public static void eachObject(Path self, Closure closure) throws IOException, ClassNotFoundException { IOGroovyMethods.eachObject(newObjectInputStream(self), closure); }
@Deprecated public static void eachByte(InputStream is, Closure closure) throws IOException { IOGroovyMethods.eachByte(is, closure); }
@Deprecated public static String getText(InputStream is, String charset) throws IOException { return IOGroovyMethods.getText(is, charset); }
/** * Create a new ObjectInputStream for this file and pass it to the closure. * This method ensures the stream is closed after the closure returns. * * @param inputStream an input stream * @param closure a closure * @return the value returned by the closure * @throws IOException if an IOException occurs. * @see #withStream(java.io.InputStream, groovy.lang.Closure) * @since 1.5.0 */ public static <T> T withObjectInputStream(InputStream inputStream, @ClosureParams(value=SimpleType.class, options="java.io.ObjectInputStream") Closure<T> closure) throws IOException { return withStream(newObjectInputStream(inputStream), closure); }
/** * Saves the fixture to the appropriate location. * * The root for the fixtures is either specified with `TEST_RESOURCES_FOLDER` system property or defaults to * <code>src/test/resources</code>. * * @param fileName relative name of the file * @param stream the stream with the content for the new file */ public void writeStream(String fileName, InputStream stream) throws IOException { String path = getFixtureLocation(fileName); File file = new File(getTestResourcesLocation(), path); file.getParentFile().mkdirs(); try (FileOutputStream out = new FileOutputStream(file)) { IOGroovyMethods.setBytes(out, IOGroovyMethods.getBytes(stream)); } }
@Deprecated public static <T> T splitEachLine(InputStream stream, String regex, Closure<T> closure) throws IOException { return IOGroovyMethods.splitEachLine(stream, regex, closure); }
@Deprecated public static <T> T eachLine(InputStream stream, int firstLine, Closure<T> closure) throws IOException { return IOGroovyMethods.eachLine(stream, firstLine, closure); }
/** * Filter lines from an input stream using a closure predicate. The closure * will be passed each line as a String, and it should return * <code>true</code> if the line should be passed to the writer. * * @param self an input stream * @param charset opens the stream with a specified charset * @param predicate a closure which returns boolean and takes a line * @return a writable which writes out the filtered lines * @throws UnsupportedEncodingException if the encoding specified is not supported * @see #filterLine(java.io.Reader, groovy.lang.Closure) * @since 1.6.8 */ public static Writable filterLine(InputStream self, String charset, @ClosureParams(value=SimpleType.class, options="java.lang.String") Closure predicate) throws UnsupportedEncodingException { return filterLine(newReader(self, charset), predicate); }