Package org.apache.commons.io.build
Class AbstractOrigin<T,B extends AbstractOrigin<T,B>>
- java.lang.Object
-
- org.apache.commons.io.build.AbstractSupplier<T,B>
-
- org.apache.commons.io.build.AbstractOrigin<T,B>
-
- Type Parameters:
T- the type of instances to build.B- the type of builder subclass.
- All Implemented Interfaces:
IOSupplier<T>
- Direct Known Subclasses:
AbstractOrigin.ByteArrayOrigin,AbstractOrigin.CharSequenceOrigin,AbstractOrigin.FileOrigin,AbstractOrigin.InputStreamOrigin,AbstractOrigin.OutputStreamOrigin,AbstractOrigin.PathOrigin,AbstractOrigin.ReaderOrigin,AbstractOrigin.URIOrigin,AbstractOrigin.WriterOrigin
public abstract class AbstractOrigin<T,B extends AbstractOrigin<T,B>> extends AbstractSupplier<T,B>
Abstracts the origin of data for builders like aFile,Path,Reader,Writer,InputStream,OutputStream, andURI.Some methods may throw
UnsupportedOperationExceptionif that method is not implemented in a concrete subclass, seegetFile()andgetPath().- Since:
- 2.12.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAbstractOrigin.ByteArrayOriginAbyte[]origin.static classAbstractOrigin.CharSequenceOriginACharSequenceorigin.static classAbstractOrigin.FileOriginAFileorigin.static classAbstractOrigin.InputStreamOriginAnInputStreamorigin.static classAbstractOrigin.OutputStreamOriginAnOutputStreamorigin.static classAbstractOrigin.PathOriginAPathorigin.static classAbstractOrigin.ReaderOriginAnReaderorigin.static classAbstractOrigin.URIOriginAURIorigin.static classAbstractOrigin.WriterOriginAnWriterorigin.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Tget()Gets the origin.byte[]getByteArray()Gets this origin as a byte array, if possible.byte[]getByteArray(long position, int length)Gets this origin as a byte array, if possible.java.lang.CharSequencegetCharSequence(java.nio.charset.Charset charset)Gets this origin as a byte array, if possible.java.io.FilegetFile()Gets this origin as a Path, if possible.java.io.InputStreamgetInputStream(java.nio.file.OpenOption... options)Gets this origin as an InputStream, if possible.java.io.OutputStreamgetOutputStream(java.nio.file.OpenOption... options)Gets this origin as an OutputStream, if possible.java.nio.file.PathgetPath()Gets this origin as a Path, if possible.java.io.ReadergetReader(java.nio.charset.Charset charset)Gets a new Reader on the origin, buffered by default.java.io.WritergetWriter(java.nio.charset.Charset charset, java.nio.file.OpenOption... options)Gets a new Writer on the origin, buffered by default.longsize()Gets the size of the origin, if possible.java.lang.StringtoString()-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.commons.io.function.IOSupplier
asSupplier
-
-
-
-
Method Detail
-
get
public T get()
Gets the origin.- Returns:
- the origin.
-
getByteArray
public byte[] getByteArray() throws java.io.IOExceptionGets this origin as a byte array, if possible.- Returns:
- this origin as a byte array, if possible.
- Throws:
java.io.IOException- if an I/O error occurs.java.lang.UnsupportedOperationException- if the origin cannot be converted to a Path.
-
getByteArray
public byte[] getByteArray(long position, int length) throws java.io.IOExceptionGets this origin as a byte array, if possible.- Parameters:
position- the initial index of the range to be copied, inclusive.length- How many bytes to copy.- Returns:
- this origin as a byte array, if possible.
- Throws:
java.lang.UnsupportedOperationException- if the origin cannot be converted to a Path.java.lang.ArithmeticException- if thepositionoverflows an intjava.io.IOException- if an I/O error occurs.- Since:
- 2.13.0
-
getCharSequence
public java.lang.CharSequence getCharSequence(java.nio.charset.Charset charset) throws java.io.IOExceptionGets this origin as a byte array, if possible.- Parameters:
charset- The charset to use if conversion from bytes is needed.- Returns:
- this origin as a byte array, if possible.
- Throws:
java.io.IOException- if an I/O error occurs.java.lang.UnsupportedOperationException- if the origin cannot be converted to a Path.
-
getFile
public java.io.File getFile()
Gets this origin as a Path, if possible.- Returns:
- this origin as a Path, if possible.
- Throws:
java.lang.UnsupportedOperationException- if this method is not implemented in a concrete subclass.
-
getInputStream
public java.io.InputStream getInputStream(java.nio.file.OpenOption... options) throws java.io.IOExceptionGets this origin as an InputStream, if possible.- Parameters:
options- options specifying how the file is opened- Returns:
- this origin as an InputStream, if possible.
- Throws:
java.io.IOException- if an I/O error occurs.java.lang.UnsupportedOperationException- if the origin cannot be converted to a Path.
-
getOutputStream
public java.io.OutputStream getOutputStream(java.nio.file.OpenOption... options) throws java.io.IOExceptionGets this origin as an OutputStream, if possible.- Parameters:
options- options specifying how the file is opened- Returns:
- this origin as an OutputStream, if possible.
- Throws:
java.io.IOException- if an I/O error occurs.java.lang.UnsupportedOperationException- if the origin cannot be converted to a Path.
-
getPath
public java.nio.file.Path getPath()
Gets this origin as a Path, if possible.- Returns:
- this origin as a Path, if possible.
- Throws:
java.lang.UnsupportedOperationException- if this method is not implemented in a concrete subclass.
-
getReader
public java.io.Reader getReader(java.nio.charset.Charset charset) throws java.io.IOExceptionGets a new Reader on the origin, buffered by default.- Parameters:
charset- the charset to use for decoding- Returns:
- a new Reader on the origin.
- Throws:
java.io.IOException- if an I/O error occurs opening the file.
-
getWriter
public java.io.Writer getWriter(java.nio.charset.Charset charset, java.nio.file.OpenOption... options) throws java.io.IOExceptionGets a new Writer on the origin, buffered by default.- Parameters:
charset- the charset to use for encodingoptions- options specifying how the file is opened- Returns:
- a new Writer on the origin.
- Throws:
java.io.IOException- if an I/O error occurs opening or creating the file.java.lang.UnsupportedOperationException- if the origin cannot be converted to a Path.
-
size
public long size() throws java.io.IOExceptionGets the size of the origin, if possible.- Returns:
- the size of the origin in bytes or characters.
- Throws:
java.io.IOException- if an I/O error occurs.- Since:
- 2.13.0
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-