@MinDoc(copyright="Copyright 1998 - 2009, 2016, 2019 A. Weinert", author="Albrecht Weinert", version="V.65", lastModified="3.06.2024", usage="use instead of an InputStream", purpose="a byte input with useful extra capabilities") public class Input extends InputStream
Files,InputStreamInput is an extension of InputStream with
optional (build in) buffering. The ("rewind") methods
mark() and reset() being at start without
function (as usual) get their full effect if all input is
recorded.read() and
read(byte[], int, int) are overridden.Input object is itself an InputStream. The data source
is set on construction being either a File
(dataFile), a URL (url) (the not used choice is null)
or another InputStream (like the standard input System.in or
another process' normal or error output).InputStream methods Input
features:File.Input features this concurrent reading with all
comfort and threadsafe (so avoiding the often seen home bread
solutions).Input is much older than
Streams and delivered
among others some of their features then, already. Now, as Frame4J
requires Java8 Input might get obsolete. The removal of
Input from Frame4J or the use of
Stream in this class Input
might be considered in future.| Modifier and Type | Field and Description |
|---|---|
protected byte[] |
buf
The result of recording.
|
protected boolean |
buffered
It's buffered.
|
protected boolean |
closed
Was closed.
|
protected int |
count
Total number of bytes of this Input or its buffer.
|
File |
dataFile
The File.
|
protected long |
date
Date and time of the last modification.
|
protected InputStream |
in
The (actual) underlying input stream.
|
protected int |
markPos
Reset position.
|
String |
name
The "pure" file name.
|
protected boolean |
partlyReadUnBuffered
Impossible to become totally buffered.
|
protected int |
pos
The number of bytes read from this Input.
|
protected IOException |
readError
Error while reading
|
protected boolean |
recording
Recording in progress.
|
URL |
url
The URL.
|
protected URLConnection |
urlConnection
The URL connection.
|
| Constructor and Description |
|---|
Input(byte[] ba,
String name,
long date)
Constructor for a named byte array.
|
Input(File inFile)
Constructor for a File.
|
Input(InputStream is)
Constructor for an InputStream.
|
Input(InputStream is,
String name)
Constructor for a named InputStream.
|
Input(URL url,
boolean open)
Constructor for an URL.
|
| Modifier and Type | Method and Description |
|---|---|
int |
available() |
void |
close()
Closing the Input.
|
int |
copyTo(OutputStream out)
Write / copy to an OutputStream.
|
byte[] |
getAsBytes()
Getting the Input's content as byte[].
|
byte[] |
getAsBytesAndClose(boolean reopen)
Getting the Input's content as byte[] and close.
|
static InputStream |
getAsResourceStream(String fileName,
Class<?> baseClass)
Get an input stream for a file name as resource.
|
String |
getAsString(Charset encoding)
Getting the Input's content as String.
|
String |
getAsString(String encoding)
Getting the Input's content as String.
|
int |
getGesLen()
Total number of bytes of this Input or its buffer.
|
String |
getName()
The "pure" file name.
|
IOException |
getReadError()
Error while reading.
|
boolean |
isBuffered()
It's buffered..
|
boolean |
isClosed()
Was closed.
|
boolean |
isRecording()
Recording in progress.
|
boolean |
isTotallyBuffered()
It's totally buffered.
|
long |
lastModified()
Date and time of the last modification.
|
String |
listLine()
Generate a describing line.
|
void |
mark(int readlimit) |
boolean |
markSupported() |
static Input |
openFileOrURL(String path)
Fetch an Input object by complete path specification.
|
static Input |
openFileOrURL(String path,
boolean threadRec)
Fetch an Input object by complete path specification.
|
int |
read()
Reading one byte.
|
int |
read(byte[] buf,
int off,
int len)
Reading multiple bytes.
|
int |
readStartOfContent(byte[] buf)
Reading the begin of the Input into a buffer.
|
boolean |
record(boolean multiThread)
Recording the input.
|
Input |
recycle(byte[] ba,
long date)
Re-utilise with a byte array.
|
boolean |
reopen(boolean bufThread)
Re-open the source.
|
void |
reset() |
long |
skip(long n) |
boolean |
testDescribe(PrintWriter out)
Output a test description of this Input.
|
boolean |
wasActive()
The Input has delivered something.
|
readpublic final File dataFile
File or one URL as
Input's base (or none of both).url,
urlConnectionpublic final URL url
File or one URL as
Input's base.dataFile,
urlConnectionprotected volatile URLConnection urlConnection
url is not null, the input is via an URL connection,
reopened at every re-activation.urlprotected volatile InputStream in
protected volatile boolean closed
protected volatile boolean recording
protected volatile boolean buffered
protected boolean partlyReadUnBuffered
protected int count
getGesLen()protected volatile long date
lastModified()protected volatile byte[] buf
protected int pos
buf.protected int markPos
isBuffered() true) this is
the rewind position for reset(). protected volatile IOException readError
getReadError()public Input(File inFile) throws FileNotFoundException
inFile - must represent a readable fileFileNotFoundException - as by FileInputStream(File)public Input(URL url, boolean open) throws IOException
url - An URL object to get the input fromopen - true: try to open the connection at once (in the constructor)IOException - as by url.openStreampublic Input(InputStream is, String name)
dataFile, url and urlConnection will be
null.is's kind or date.is - An InputStream to be used as inputname - A simple name to tag to it; may be nullpublic Input(InputStream is)
public Input(byte[] ba,
String name,
long date)
dataFile, url and urlConnection will be
null.Input is (not as surprise) buffered / recorded from the
beginning.ba - the content to read, must not be emptyname - A simple name to tag to it; may be nullpublic final String getName()
public boolean isClosed()
public boolean isRecording()
public final boolean isBuffered()
public final boolean isTotallyBuffered()
public final int getGesLen()
public long lastModified()
public final IOException getReadError()
record(), getAsString(),
getAsBytes(), copyTo() etc. If all
went well null is returned.public static Input openFileOrURL(String path) throws IOException
path (stripped from surrounding white
space) it will be tried to make an Input object.File of that name for reading.path as URL and open
a connection.path is empty System.in will be wrapped as Input object.openFileOrURL(path, false).path - the name of the file or the URLIOException - no construction possible.name,
dataFile,
url,
Input(File),
Input(URL, boolean),
Input(InputStream, String)public static Input openFileOrURL(String path, boolean threadRec) throws IOException
openFileOrURL(String path)
with the extra function to immediately start reading and recording if
thraedRec is true. (This is done by an anonymous
inner Thread.)path - names a file or resource for input (null gets standard in)threadRec - true: use an extra thread for reading and recordingIOException - file or stream problemspublic Input recycle(byte[] ba, long date)
dataFile, url keep their value. urlConnection
will be null and date will be set by the parameter
date.Input is buffered / recorded from then on.ba - the content to read, must not be empty.
No copy is made. If null the the internal buffer used so
far is taken. (Know what you do using this efficiency
option.)date - the modification date to use (0L = undefined or invalid)public int available()
throws IOException
available in class InputStreamIOExceptionpublic void mark(int readlimit)
mark in class InputStreampublic boolean markSupported()
markSupported in class InputStreampublic void reset()
throws IOException
reset in class InputStreamIOExceptionpublic int read()
throws IOException
read in class InputStreamIOException - as by the underlying Stream (if any)read(byte[], int, int)public int read(byte[] buf,
int off,
int len)
read in class InputStreamInput.read(...)public long skip(long n)
skip in class InputStreampublic int readStartOfContent(byte[] buf)
buf will be filled with this Input's start of
content respectively start of content not yet read.read(...) is used perhaps
multiple times until either the array supplied is filled, EOF is
reached or a problem occurred. The multiplicity of the tries may be
necessary for some streams with diffuse buffering strategies or which
never know how long the will be.buf is ofzero length or
null and -1 if the very first read shows EOF.read(byte[], int, int.buf - to put the read content inread(byte[], int, int),
read(),
close()public void close()
name as well as all buffered
content of gesLen bytes. That can be used and
retrieved / copied as String or byte[].close in interface Closeableclose in interface AutoCloseableclose in class InputStreampublic boolean reopen(boolean bufThread)
throws IOException
close().File dataFile or an URL
url that will be re-opened for reading. This can be quite
sensible for files and URLs having a dynamically changing content and it
is than cheaper than the re-making of all objects involved. If that
re-connecting / -opening succeeds true is returned.bufThread is true, the "new" Input will be completely
read / buffered by a concurrent anonymous thread.bufThread - true: use an extra thread for reading and recordingIOException - file or stream problemsrecord()public boolean testDescribe(PrintWriter out)
out.out - the Writer to put the descriptive lines topublic String listLine()
dataFile.infoLine(dataFile,null, false, false).url's description is
returned, if url is not null.name or
"Input: " + in.toString() is returned.FileHelper.infoLine(File),
dataFile,
urlpublic boolean wasActive()
public int copyTo(OutputStream out)
Input was buffered() the whole
content will be written to out. This works even if this
Input was closed in between. That means
multiple calls again transport the same content.Input was not
buffered() all content not yet read will be copied
to out. This Input will be closed
afterwards.out - the stream to copy toclose()public boolean record(boolean multiThread)
Input's content respectively the yet unread rest will be
buffered internally. Afterwards, the buffer content can be retrieved (even
multiple times) by the methods getAsString(),
getAsBytes() or copyTo().multiThread as true). The method then
returns immediately.multiThread - true: use another thread for recordinggetGesLen() then gives the number of read / buffered
bytes.public String getAsString(String encoding) throws UnsupportedEncodingException, IOException
Input's content respectively the yet unread rest will be
buffered internally using record(), if that was not yet
done. If nothing is to be read and nothing was buffered null is
returned.encoding. If encoding is empty or
"defaultEncoding",
ComVar.FILE_ENCODING
will be used (most often ISO-8859-1 or Cp1252 on Windows).encoding - the encoding from byte to UnicodeUnsupportedEncodingException - if the parameter encoding is wrongIOException - if one happened during record()close(),
copyTo(InputStream),
ComVar.FILE_ENCODINGpublic String getAsString(Charset encoding) throws UnsupportedEncodingException, IOException
Input's content respectively the yet unread rest will be
buffered internally using record(), if that was not yet
done. If nothing is to be read and nothing was buffered null is
returned.encoding. If encoding is null
ComVar.FILE_ENCODING
will be used (most often ISO-8859-1 or Cp1252 on Windows).encoding - the encoding from byte to UnicodeUnsupportedEncodingException - if the parameter encoding is wrongIOException - if one happened during record()close(),
copyTo(InputStream),
getAsString(String)public byte[] getAsBytes()
throws IOException
Input's content respectively the yet unread rest will be
buffered internally using record(), if that was not yet
done. If nothing is to be read and nothing was buffered null is
returned.IOException - if one happened during record()close(),
copyTo(InputStream)public byte[] getAsBytesAndClose(boolean reopen)
throws IOException
reopen(true).Input's content respectively the yet unread rest will be
buffered internally using record(), if that was not yet
done. If nothing is to be read and nothing was buffered null is
returned.reopen()) should that be planned.IOException - if one happened during record()close(),
copyTo(InputStream)public static InputStream getAsResourceStream(String fileName, Class<?> baseClass)
InputStream)from the fileName provided, that must
contain no path (not even .\).