/**
* Your Web Toolkit (YWT) - MVC Framework for web based applications. Copyright
* (C) 2008 Quentin Anciaux
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.allcolor.alc.utils.io;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import org.allcolor.alc.filesystem.File;
/**
*
DOCUMENT ME!
*
* @author Quentin Anciaux
* @version 0.1.0
*/
public class ReaderUtils {
// ~ Methods
// ------------------------------------------------------------------
/**
* @author Quentin Anciaux
* @version 0.1.0
*/
public static interface LineListener {
// ~ Methods
// ----------------------------------------------------------------
/**
* DOCUMENT ME!
*
* @param ioe
* DOCUMENT ME!
*/
public abstract void exception(final IOException ioe);
/**
* DOCUMENT ME!
*
* @param line
* DOCUMENT ME!
*
* @return DOCUMENT ME!
*
* @throws IOException
* DOCUMENT ME!
*/
public abstract boolean line(final String line) throws IOException;
} // end LineListener
public static abstract class LineListenerImpl implements LineListener {
protected final StringBuilder internalBuffer = new StringBuilder();
public void exception(final IOException ioe) {
throw new RuntimeException(ioe);
}
@Override
public String toString() {
return this.internalBuffer.toString();
}
}
/**
* DOCUMENT ME!
*
* @param file
* DOCUMENT ME!
* @param lit
* DOCUMENT ME!
*/
public static void forEachLine(final File file, final LineListener lit) {
try {
ReaderUtils.forEachLine(file.getInputStream(), lit);
} // end try
catch (final IOException ioe) {
lit.exception(ioe);
} // end catch
} // end forEachLine()
/**
* DOCUMENT ME!
*
* @param file
* DOCUMENT ME!
* @param lit
* DOCUMENT ME!
* @param encoding
* DOCUMENT ME!
*/
public static void forEachLine(final File file, final LineListener lit,
final String encoding) {
try {
ReaderUtils.forEachLine(file.getInputStream(), lit, encoding);
} // end try
catch (final IOException ioe) {
lit.exception(ioe);
} // end catch
} // end forEachLine()
/**
* DOCUMENT ME!
*
* @param in
* DOCUMENT ME!
* @param lit
* DOCUMENT ME!
*/
public static void forEachLine(final InputStream in, final LineListener lit) {
ReaderUtils.forEachLine(in, lit, "utf-8");
} // end forEachLine()
/**
* DOCUMENT ME!
*
* @param in
* DOCUMENT ME!
* @param lit
* DOCUMENT ME!
* @param encoding
* DOCUMENT ME!
*/
public static void forEachLine(final InputStream in,
final LineListener lit, final String encoding) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(in, encoding));
String line = null;
while ((line = reader.readLine()) != null) {
if (!lit.line(line)) {
break;
} // end if
} // end while
} // end try
catch (final IOException ioe) {
lit.exception(ioe);
} // end catch
finally {
try {
reader.close();
} // end try
catch (final Exception ignore) {
;
} // end catch
} // end finally
} // end forEachLine()
/**
* DOCUMENT ME!
*
* @param file
* DOCUMENT ME!
* @param lit
* DOCUMENT ME!
*/
public static void forEachLine(final java.io.File file,
final LineListener lit) {
try {
ReaderUtils.forEachLine(new FileInputStream(file), lit);
} // end try
catch (final IOException ioe) {
lit.exception(ioe);
} // end catch
} // end forEachLine()
/**
* DOCUMENT ME!
*
* @param file
* DOCUMENT ME!
* @param lit
* DOCUMENT ME!
* @param encoding
* DOCUMENT ME!
*/
public static void forEachLine(final java.io.File file,
final LineListener lit, final String encoding) {
try {
ReaderUtils.forEachLine(new FileInputStream(file), lit, encoding);
} // end try
catch (final IOException ioe) {
lit.exception(ioe);
} // end catch
} // end forEachLine()
// ~ Inner Interfaces
// ---------------------------------------------------------
/**
* DOCUMENT ME!
*
* @param url
* DOCUMENT ME!
* @param lit
* DOCUMENT ME!
*/
public static void forEachLine(final URL url, final LineListener lit) {
try {
ReaderUtils.forEachLine(url.openStream(), lit);
} // end try
catch (final IOException ioe) {
lit.exception(ioe);
} // end catch
} // end forEachLine()
// ~ Inner Interfaces
// -------------------------------------------------------
/**
* DOCUMENT ME!
*
* @param url
* DOCUMENT ME!
* @param lit
* DOCUMENT ME!
* @param encoding
* DOCUMENT ME!
*/
public static void forEachLine(final URL url, final LineListener lit,
final String encoding) {
try {
ReaderUtils.forEachLine(url.openStream(), lit);
} // end try
catch (final IOException ioe) {
lit.exception(ioe);
} // end catch
} // end forEachLine()
} // end ReaderUtils
/**
* Your Web Toolkit (YWT) - MVC Framework for web based applications. Copyright
* (C) 2008 Quentin Anciaux
*/
|