Class RepeatableReader
- java.lang.Object
-
- java.io.Reader
-
- org.zkoss.io.RepeatableReader
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Serializable,java.lang.AutoCloseable,java.lang.Readable,Repeatable
public class RepeatableReader extends java.io.Reader implements Repeatable, java.io.Serializable
RepeatableReaderadds functionality to another reader, the ability to read repeatedly. By repeatable-read we mean, afterclose(), the next invocation ofread(char[], int, int)will re-open the reader.RepeatableInputStreamactually creates a temporary space to buffer the content, so it can be re-opened again after closed. Notice that the temporary space (a.k.a., the buffered reader) is never closed until garbage-collected.If the content size of the given reader is smaller than the value specified in the system property called "org.zkoss.io.memoryLimitSize", the content will be buffered in the memory. If the size exceeds, the content will be buffered in a temporary file. By default, it is 512KB. Note: the maximal value is
Integer.MAX_VALUEIf the content size of the given reader is larger than the value specified in the system property called "org.zkoss.io.bufferLimitSize", the content won't be buffered, and it means the read is not repeatable. By default, it is 20MB. Note: the maximal value is
Integer.MAX_VALUE- Since:
- 3.0.4
- Author:
- tomyeh
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the current access, and the next call ofread(char[], int, int)re-opens the buffered reader.protected voidfinalize()static java.io.ReadergetInstance(java.io.File file)Returns a reader to read a file, encoded in UTF-8, that can be read repeatedly.static java.io.ReadergetInstance(java.io.File file, java.lang.String charset)Returns a reader to read a file that can be read repeatedly.static java.io.ReadergetInstance(java.io.Reader rd)Returns a reader that can be read repeatedly, or null if the given reader is null.static java.io.ReadergetInstance(java.lang.String filename)Returns a reader to read a file, encoded in UTF-8, that can be read repeatedly.static java.io.ReadergetInstance(java.lang.String filename, java.lang.String charset)Returns a reader to read a file that can be read repeatedly.static java.io.ReadergetInstance(java.net.URL url)Returns a reader to read the resource of the specified URL, encoded in UTF-8.static java.io.ReadergetInstance(java.net.URL url, java.lang.String charset)Returns a reader to read the resource of the specified URL.intread(char[] cbuf, int off, int len)
-
-
-
Method Detail
-
getInstance
public static java.io.Reader getInstance(java.io.Reader rd)
Returns a reader that can be read repeatedly, or null if the given reader is null. Note: the returned reader encapsulates the given reader, rd (a.k.a., the buffered reader) to adds the functionality to re-opens the reader onceclose()is called.By repeatable-read we mean, after
close(), the next invocation ofread(char[], int, int)will re-open the reader.Use this method instead of instantiating
RepeatableReaderwith the constructor.- See Also:
getInstance(File)
-
getInstance
public static java.io.Reader getInstance(java.io.File file, java.lang.String charset) throws java.io.FileNotFoundExceptionReturns a reader to read a file that can be read repeatedly. Note: it assumes the file is text (rather than binary).By repeatable-read we mean, after
close(), the next invocation ofread(char[], int, int)will re-open the reader.Note: it is efficient since we don't have to buffer the content of the file to make it repeatable-read.
- Parameters:
charset- the charset. If null, "UTF-8" is assumed.- Throws:
java.lang.IllegalArgumentException- if file is null.java.io.FileNotFoundException- Since:
- 3.0.8
- See Also:
getInstance(Reader),getInstance(String, String)
-
getInstance
public static java.io.Reader getInstance(java.io.File file) throws java.io.FileNotFoundExceptionReturns a reader to read a file, encoded in UTF-8, that can be read repeatedly. Note: it assumes the file is text (rather than binary).By repeatable-read we mean, after
close(), the next invocation ofread(char[], int, int)will re-open the reader.Note: it is efficient since we don't have to buffer the content of the file to make it repeatable-read.
- Throws:
java.lang.IllegalArgumentException- if file is null.java.io.FileNotFoundException- See Also:
getInstance(Reader),getInstance(String)
-
getInstance
public static java.io.Reader getInstance(java.lang.String filename, java.lang.String charset) throws java.io.FileNotFoundExceptionReturns a reader to read a file that can be read repeatedly. Note: it assumes the file is text (rather than binary).By repeatable-read we mean, after
close(), the next invocation ofread(char[], int, int)will re-open the reader.Note: it is efficient since we don't have to buffer the content of the file to make it repeatable-read.
- Parameters:
filename- the file namecharset- the charset. If null, "UTF-8" is assumed.- Throws:
java.lang.IllegalArgumentException- if file is null.java.io.FileNotFoundException- if file is not found.- Since:
- 3.0.8
- See Also:
getInstance(Reader),getInstance(File, String)
-
getInstance
public static java.io.Reader getInstance(java.lang.String filename) throws java.io.FileNotFoundExceptionReturns a reader to read a file, encoded in UTF-8, that can be read repeatedly. Note: it assumes the file is text (rather than binary).By repeatable-read we mean, after
close(), the next invocation ofread(char[], int, int)will re-open the reader.Note: it is efficient since we don't have to buffer the content of the file to make it repeatable-read.
- Parameters:
filename- the file name- Throws:
java.lang.IllegalArgumentException- if file is null.java.io.FileNotFoundException- if file is not found.- See Also:
getInstance(Reader),getInstance(File)
-
getInstance
public static java.io.Reader getInstance(java.net.URL url, java.lang.String charset)Returns a reader to read the resource of the specified URL. The reader can be read repeatedly. Note: it assumes the resource is text (rather than binary).By repeatable-read we mean, after
close(), the next invocation ofread(char[], int, int)will re-open the reader.Note: it is efficient since we don't have to buffer the content of the file to make it repeatable-read.
- Parameters:
charset- the charset. If null, "UTF-8" is assumed.- Throws:
java.lang.IllegalArgumentException- if file is null.- Since:
- 3.0.8
- See Also:
getInstance(Reader),getInstance(String, String)
-
getInstance
public static java.io.Reader getInstance(java.net.URL url)
Returns a reader to read the resource of the specified URL, encoded in UTF-8. The reader can be read repeatedly. Note: it assumes the resource is text (rather than binary).By repeatable-read we mean, after
close(), the next invocation ofread(char[], int, int)will re-open the reader.Note: it is efficient since we don't have to buffer the content of the file to make it repeatable-read.
- Throws:
java.lang.IllegalArgumentException- if file is null.- See Also:
getInstance(Reader),getInstance(String)
-
read
public int read(char[] cbuf, int off, int len) throws java.io.IOException- Specified by:
readin classjava.io.Reader- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOExceptionCloses the current access, and the next call ofread(char[], int, int)re-opens the buffered reader.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein classjava.io.Reader- Throws:
java.io.IOException
-
finalize
protected void finalize() throws java.lang.Throwable- Overrides:
finalizein classjava.lang.Object- Throws:
java.lang.Throwable
-
-