directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1732827 - in /directory/shared/trunk/util/src/main/java/org/apache/directory/api/util: FileUtils.java IOUtils.java
Date Mon, 29 Feb 2016 07:05:37 GMT
Author: elecharny
Date: Mon Feb 29 07:05:37 2016
New Revision: 1732827

URL: http://svn.apache.org/viewvc?rev=1732827&view=rev
Log:
Added the readLines() method for Studio

Modified:
    directory/shared/trunk/util/src/main/java/org/apache/directory/api/util/FileUtils.java
    directory/shared/trunk/util/src/main/java/org/apache/directory/api/util/IOUtils.java

Modified: directory/shared/trunk/util/src/main/java/org/apache/directory/api/util/FileUtils.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/util/src/main/java/org/apache/directory/api/util/FileUtils.java?rev=1732827&r1=1732826&r2=1732827&view=diff
==============================================================================
--- directory/shared/trunk/util/src/main/java/org/apache/directory/api/util/FileUtils.java
(original)
+++ directory/shared/trunk/util/src/main/java/org/apache/directory/api/util/FileUtils.java
Mon Feb 29 07:05:37 2016
@@ -30,6 +30,7 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.nio.channels.FileChannel;
 import java.nio.charset.Charset;
+import java.util.List;
 
 
 /**
@@ -819,4 +820,47 @@ public final class FileUtils
     {
         return openOutputStream( file, false );
     }
+    
+    
+    /**
+     * Reads the contents of a file line by line to a List of Strings using the default encoding
for the VM.
+     * The file is always closed.
+     *
+     * @param file  the file to read, must not be {@code null}
+     * @return the list of Strings representing each line in the file, never {@code null}
+     * @throws IOException in case of an I/O error
+     * @since 1.3
+     * @deprecated 2.5 use {@link #readLines(File, Charset)} instead
+     */
+    @Deprecated
+    public static List<String> readLines( File file ) throws IOException 
+    {
+        return readLines( file, Charset.defaultCharset() );
+    }
+    
+    
+    /**
+     * Reads the contents of a file line by line to a List of Strings.
+     * The file is always closed.
+     *
+     * @param file  the file to read, must not be {@code null}
+     * @param encoding  the encoding to use, {@code null} means platform default
+     * @return the list of Strings representing each line in the file, never {@code null}
+     * @throws IOException in case of an I/O error
+     * @since 2.3
+     */
+    public static List<String> readLines( File file, Charset encoding ) throws IOException

+    {
+        InputStream in = null;
+        
+        try 
+        {
+            in = openInputStream( file );
+            return IOUtils.readLines( in, IOUtils.toCharset( encoding ) );
+        } 
+        finally 
+        {
+            IOUtils.closeQuietly( in );
+        }
+    }
 }

Modified: directory/shared/trunk/util/src/main/java/org/apache/directory/api/util/IOUtils.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/util/src/main/java/org/apache/directory/api/util/IOUtils.java?rev=1732827&r1=1732826&r2=1732827&view=diff
==============================================================================
--- directory/shared/trunk/util/src/main/java/org/apache/directory/api/util/IOUtils.java (original)
+++ directory/shared/trunk/util/src/main/java/org/apache/directory/api/util/IOUtils.java Mon
Feb 29 07:05:37 2016
@@ -21,6 +21,7 @@
 package org.apache.directory.api.util;
 
 
+import java.io.BufferedReader;
 import java.io.Closeable;
 import java.io.IOException;
 import java.io.InputStream;
@@ -29,6 +30,8 @@ import java.io.OutputStream;
 import java.io.Reader;
 import java.io.Writer;
 import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.List;
 
 
 /**
@@ -545,4 +548,72 @@ public final class IOUtils
 
       return toByteArray( input, ( int ) size );
     }
+    
+    
+    /**
+     * Gets the contents of an <code>InputStream</code> as a list of Strings,
+     * one entry per line, using the specified character encoding.
+     * <p>
+     * This method buffers the input internally, so there is no need to use a
+     * <code>BufferedInputStream</code>.
+     *
+     * @param input  the <code>InputStream</code> to read from, not null
+     * @param encoding  the encoding to use, null means platform default
+     * @return the list of Strings, never null
+     * @throws NullPointerException if the input is null
+     * @throws IOException if an I/O error occurs
+     * @since 2.3
+     */
+    public static List<String> readLines( InputStream input, Charset encoding ) throws
IOException 
+    {
+        InputStreamReader reader = new InputStreamReader( input, toCharset( encoding ) );
+        
+        return readLines( reader );
+    }
+    
+    
+    /**
+     * Gets the contents of a <code>Reader</code> as a list of Strings,
+     * one entry per line.
+     * <p>
+     * This method buffers the input internally, so there is no need to use a
+     * <code>BufferedReader</code>.
+     *
+     * @param input  the <code>Reader</code> to read from, not null
+     * @return the list of Strings, never null
+     * @throws NullPointerException if the input is null
+     * @throws IOException if an I/O error occurs
+     * @since 1.1
+     */
+    public static List<String> readLines( Reader input ) throws IOException 
+    {
+        BufferedReader reader = toBufferedReader( input );
+        List<String> list = new ArrayList<String>();
+        String line = reader.readLine();
+        
+        while ( line != null ) 
+        {
+            list.add( line );
+            line = reader.readLine();
+        }
+        
+        return list;
+    }
+
+    
+    /**
+     * Returns the given reader if it is a {@link BufferedReader}, otherwise creates a BufferedReader
from the given
+     * reader.
+     *
+     * @param reader
+     *            the reader to wrap or return (not null)
+     * @return the given reader or a new {@link BufferedReader} for the given reader
+     * @since 2.2
+     * @see #buffer(Reader)
+     * @throws NullPointerException if the input parameter is null
+     */
+    public static BufferedReader toBufferedReader( Reader reader ) 
+    {
+        return reader instanceof BufferedReader ? ( BufferedReader ) reader : new BufferedReader(
reader );
+    }
 }



Mime
View raw message