/** * @return The full binary content of this URL's stream. */ public static byte[] getBinaryContent( @This URL thiz ) { try( InputStream stream = thiz.openStream() ) { return StreamUtil.getContent( stream ); } catch( IOException e ) { throw new RuntimeException( e ); } }
/** * @return The full text content of this URL's stream. */ public static String getTextContent( @This URL thiz ) { try( Reader reader = StreamUtil.getInputStreamReader( thiz.openStream() ) ) { return StreamUtil.getContent( reader ); } catch( IOException e ) { throw new RuntimeException( e ); } }
/** * Closes the specified streams, suppressing any IOExceptions for inputstreams and readers. * Even if an I/O exception is thrown, all streams can be considered closed. * * @param streams the streams to close * * @throws IOException if an i/o exception occurs while closing any outputstream or writer */ public static void close( Closeable... streams ) throws IOException { close( streams, 0 ); }
/** * Copies the content of a reader to an output stream. * * @param reader the reader to read * @param out the output stream to write * * @throws IOException if an I/O error occurs */ public static void copy( Reader reader, OutputStream out ) throws IOException { copy( reader, getOutputStreamWriter( out ) ); out.flush(); }
/** * Copies the content of an input stream to a writer. * * @param in the input stream to read * @param writer the writer to write * * @throws IOException if an I/O error occurs */ public static void copy( InputStream in, Writer writer ) throws IOException { copy( getInputStreamReader( in ), writer ); writer.flush(); }
/** * Returns a reader for the specified input stream, using UTF-8 encoding. * * @param in the input stream to wrap * * @return a reader for this input stream */ public static Reader getInputStreamReader( InputStream in ) { return getInputStreamReader( in, "UTF-8" ); }
/** * Recursively copy a file or directory to a directory. */ public static void copy( File fileOrDirectory, File toDir ) { copy( fileOrDirectory, toDir, null ); }
/** * Returns the content of the specified input stream. The stream will be closed after calling this method. * * @param in the input stream to read * * @return the content of the input stream * * @throws IOException if an I/O error occurs */ public static byte[] getContent( InputStream in ) throws IOException { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); copy( in, baos ); return baos.toByteArray(); } finally { in.close(); } }
public static String getContent( IFile file ) { if( file != null ) { try( InputStream inputStream = file.openInputStream() ) { return StreamUtil.getContent( new InputStreamReader( inputStream ) ); //.replace( "\r\n", "\n" ); } catch( Exception e ) { throw new RuntimeException( e ); } } return null; }
try( Reader in = StreamUtil.getInputStreamReader( conn.getInputStream() ) ) return StreamUtil.getContent( in );
/** * Returns the content of the specified reader. The reader will be closed after calling this method. * * @param in the reader to read * * @return the content of the reader * * @throws IOException if an I/O error occurs */ public static String getContent( Reader in ) throws IOException { try { StringWriter sw = new StringWriter(); copy( in, sw ); return sw.toString(); } finally { in.close(); } }
private static void close( Closeable[] streams, int idx ) throws IOException { if( idx >= streams.length ) { return; // done } Closeable stream = streams[idx]; try { if( stream != null ) { if( stream instanceof Flushable ) { ((Flushable)stream).flush(); } stream.close(); } } catch( IOException ex ) { if( !(stream instanceof InputStream || stream instanceof Reader) ) { throw ex; // ignore io exceptions for input streams and readers } } finally { close( streams, idx + 1 ); } }
private String assignContent() { if( _file != null ) { try( InputStream inputStream = _file.openInputStream() ) { return StreamUtil.getContent( new InputStreamReader( inputStream ) ); //.replace( "\r\n", "\n" ); } catch( Exception e ) { throw new RuntimeException( e ); } } return null; } }
public static void copy( File fileOrDirectory, File toDir, Predicate<File> filter ) { if( filter == null || filter.test( fileOrDirectory ) ) { File copy = new File( toDir, fileOrDirectory.getName() ); if( fileOrDirectory.isDirectory() ) { //noinspection ResultOfMethodCallIgnored copy.mkdir(); for( File child : fileOrDirectory.listFiles() ) { copy( child, copy, filter ); } } else { //noinspection ResultOfMethodCallIgnored try (InputStream is = new FileInputStream( fileOrDirectory ); OutputStream os = new FileOutputStream( copy )) { StreamUtil.copy( is, os ); } catch( Exception e ) { throw new RuntimeException( e ); } } } }
private String assignContent() { if( _file != null ) { try( InputStream inputStream = _file.openInputStream() ) { return StreamUtil.getContent( new InputStreamReader( inputStream ) ); //.replace( "\r\n", "\n" ); } catch( Exception e ) { throw new RuntimeException( e ); } } return null; } }
private String getSource( Model model ) { try { return StreamUtil.getContent( new InputStreamReader( model.getFile().openInputStream() ) ); } catch( IOException ioe ) { throw new RuntimeException( ioe ); } }
private String getSource( Model model ) { try { return StreamUtil.getContent( new InputStreamReader( model.getFile().openInputStream() ) ); } catch( IOException ioe ) { throw new RuntimeException( ioe ); } }
private int findOffset( IFile file, int lineNum, int column ) { try { int offset = 0; String content = StreamUtil.getContent( new InputStreamReader( file.openInputStream() ) ); for( int i = 1; i < lineNum; i++ ) { if( content.length() > offset ) { offset = content.indexOf( '\n', offset ) + 1; } } return offset + column - 1; } catch( IOException e ) { throw new RuntimeException( e ); } }
private void addResourcePaths( File file, Set<String> resourcePath ) { try { String content = StreamUtil.getContent( new FileReader( file ) ); int index = content.indexOf( RESOURCE_ROOTS ); if( index >= 0 ) { int iEol = content.indexOf( '\n', index ); String paths = content.substring( index + RESOURCE_ROOTS.length(), iEol ); paths = paths.trim(); for( StringTokenizer tokenizer = new StringTokenizer( paths, File.pathSeparator ); tokenizer.hasMoreTokens(); ) { String path = tokenizer.nextToken(); if( new File( path ).isDirectory() ) { resourcePath.add( path ); } } } } catch( IOException e ) { throw new RuntimeException( e ); } }