Class OWFileInputStream
- java.lang.Object
-
- java.io.InputStream
-
- com.dalsemi.onewire.application.file.OWFileInputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class OWFileInputStream extends java.io.InputStream
AOWFileInputStream
obtains input bytes from a file in a 1-Wire Filesystem. What files are available depends on the 1-Wire device.Note that the 1-Wire File system can reside across multiple 1-Wire devices. In this case only one of the devices need be supplied to the constructor. Each device in a multi-device file system contains information to reacquire the entire list.
File and directory name limitations
- File/directory names limited to 4 characters not including extension
- File/directory names are not case sensitive and will be automatically changed to all-CAPS
- Only files can have extensions
- Extensions are numberical in the range 0 to 125
- Extensions 100 to 125 are special purpose and not yet implemented or allowed
- Files can have the read-only attribute
- Directories can have the hidden attribute
- It is recommended to limit directory depth to 10 levels to accommodate legacy implementations
Usage
Example
Read from a 1-Wire file on device 'owd':// get an input stream to the 1-Wire file OWFileInputStream instream = new OWFileInputStream(owd, "DEMO.0"); // read some data byte[] data = new byte[2000]; int len = instream.read(data); // close the stream to release system resources instream.close();
- See Also:
OWFile
,OWFileDescriptor
,OWFileOutputStream
-
-
Constructor Summary
Constructors Constructor Description OWFileInputStream(OWFile file)
Creates aOWFileInputStream
by opening a connection to an actual file, the file named by theFile
objectfile
in the Filesystem.OWFileInputStream(OWFileDescriptor fdObj)
Creates aOWFileInputStream
by using the file descriptorfdObj
, which represents an existing connection to an actual file in the Filesystem.OWFileInputStream(OneWireContainer[] owd, java.lang.String name)
Creates aFileInputStream
by opening a connection to an actual file, the file named by the path namename
in the Filesystem.OWFileInputStream(OneWireContainer owd, java.lang.String name)
Creates aFileInputStream
by opening a connection to an actual file, the file named by the path namename
in the Filesystem.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
available()
Returns the number of bytes that can be read from this file input stream without blocking.void
close()
Closes this file input stream and releases any system resources associated with the stream.void
finalize()
Ensures that theclose
method of this file input stream is called when there are no more references to it.OWFileDescriptor
getFD()
Returns theOWFileDescriptor
object that represents the connection to the actual file in the Filesystem being used by thisFileInputStream
.void
mark(int readlimit)
Marks the current position in this input stream.boolean
markSupported()
Tests if this input stream supports themark
andreset
methods.int
read()
Reads a byte of data from this input stream.int
read(byte[] b)
Reads up tob.length
bytes of data from this input stream into an array of bytes.int
read(byte[] b, int off, int len)
Reads up tolen
bytes of data from this input stream into an array of bytes.void
reset()
Repositions this stream to the position at the time themark
method was last called on this input stream.long
skip(long n)
Skips over and discardsn
bytes of data from the input stream.
-
-
-
Constructor Detail
-
OWFileInputStream
public OWFileInputStream(OneWireContainer owd, java.lang.String name) throws OWFileNotFoundException
Creates aFileInputStream
by opening a connection to an actual file, the file named by the path namename
in the Filesystem. A newOWFileDescriptor
object is created to represent this file connection.First, if there is a security manager, its
checkRead
method is called with thename
argument as its argument.If the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading then a
FileNotFoundException
is thrown.- Parameters:
owd
- OneWireContainer that this Filesystem resides onname
- the system-dependent file name.- Throws:
OWFileNotFoundException
- if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
-
OWFileInputStream
public OWFileInputStream(OneWireContainer[] owd, java.lang.String name) throws OWFileNotFoundException
Creates aFileInputStream
by opening a connection to an actual file, the file named by the path namename
in the Filesystem. A newOWFileDescriptor
object is created to represent this file connection.First, if there is a security manager, its
checkRead
method is called with thename
argument as its argument.If the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading then a
FileNotFoundException
is thrown.- Parameters:
owd
- array of OneWireContainers that this Filesystem resides onname
- the system-dependent file name.- Throws:
OWFileNotFoundException
- if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
-
OWFileInputStream
public OWFileInputStream(OWFile file) throws OWFileNotFoundException
Creates aOWFileInputStream
by opening a connection to an actual file, the file named by theFile
objectfile
in the Filesystem. A newOWFileDescriptor
object is created to represent this file connection.If the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading then a
FileNotFoundException
is thrown.- Parameters:
file
- the file to be opened for reading.- Throws:
OWFileNotFoundException
- if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.- See Also:
OWFile.getPath()
-
OWFileInputStream
public OWFileInputStream(OWFileDescriptor fdObj)
Creates aOWFileInputStream
by using the file descriptorfdObj
, which represents an existing connection to an actual file in the Filesystem.If
fdObj
is null then aNullPointerException
is thrown.- Parameters:
fdObj
- the file descriptor to be opened for reading.
-
-
Method Detail
-
read
public int read() throws java.io.IOException
Reads a byte of data from this input stream. This method blocks if no input is yet available.- Specified by:
read
in classjava.io.InputStream
- Returns:
- the next byte of data, or
-1
if the end of the file is reached. - Throws:
java.io.IOException
- if an I/O error occurs.
-
read
public int read(byte[] b) throws java.io.IOException
Reads up tob.length
bytes of data from this input stream into an array of bytes. This method blocks until some input is available.- Overrides:
read
in classjava.io.InputStream
- Parameters:
b
- the buffer into which the data is read.- Returns:
- the total number of bytes read into the buffer, or
-1
if there is no more data because the end of the file has been reached. - Throws:
java.io.IOException
- if an I/O error occurs.
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException
Reads up tolen
bytes of data from this input stream into an array of bytes. This method blocks until some input is available.- Overrides:
read
in classjava.io.InputStream
- Parameters:
b
- the buffer into which the data is read.off
- the start offset of the data.len
- the maximum number of bytes read.- Returns:
- the total number of bytes read into the buffer, or
-1
if there is no more data because the end of the file has been reached. - Throws:
java.io.IOException
- if an I/O error occurs.
-
skip
public long skip(long n) throws java.io.IOException
Skips over and discardsn
bytes of data from the input stream. Theskip
method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly0
. The actual number of bytes skipped is returned.- Overrides:
skip
in classjava.io.InputStream
- Parameters:
n
- the number of bytes to be skipped.- Returns:
- the actual number of bytes skipped.
- Throws:
java.io.IOException
- if an I/O error occurs.
-
available
public int available() throws java.io.IOException
Returns the number of bytes that can be read from this file input stream without blocking.- Overrides:
available
in classjava.io.InputStream
- Returns:
- the number of bytes that can be read from this file input stream without blocking.
- Throws:
java.io.IOException
- if an I/O error occurs.
-
close
public void close() throws java.io.IOException
Closes this file input stream and releases any system resources associated with the stream.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.InputStream
- Throws:
java.io.IOException
- if an I/O error occurs.
-
getFD
public final OWFileDescriptor getFD() throws java.io.IOException
Returns theOWFileDescriptor
object that represents the connection to the actual file in the Filesystem being used by thisFileInputStream
.- Returns:
- the file descriptor object associated with this stream.
- Throws:
java.io.IOException
- if an I/O error occurs.- See Also:
OWFileDescriptor
-
finalize
public void finalize() throws java.io.IOException
Ensures that theclose
method of this file input stream is called when there are no more references to it.- Overrides:
finalize
in classjava.lang.Object
- Throws:
java.io.IOException
- if an I/O error occurs.- See Also:
close()
-
mark
public void mark(int readlimit)
Marks the current position in this input stream. A subsequent call to thereset
method repositions this stream at the last marked position so that subsequent reads re-read the same bytes.The
readlimit
arguments tells this input stream to allow that many bytes to be read before the mark position gets invalidated.The general contract of
mark
is that, if the methodmarkSupported
returnstrue
, the stream somehow remembers all the bytes read after the call tomark
and stands ready to supply those same bytes again if and whenever the methodreset
is called. However, the stream is not required to remember any data at all if more thanreadlimit
bytes are read from the stream beforereset
is called.The
mark
method ofInputStream
does nothing.- Overrides:
mark
in classjava.io.InputStream
- Parameters:
readlimit
- the maximum limit of bytes that can be read before the mark position becomes invalid.- See Also:
InputStream.reset()
-
reset
public void reset() throws java.io.IOException
Repositions this stream to the position at the time themark
method was last called on this input stream.The general contract of
reset
is:- If the method
markSupported
returnstrue
, then:- If the method
mark
has not been called since the stream was created, or the number of bytes read from the stream sincemark
was last called is larger than the argument tomark
at that last call, then anIOException
might be thrown. - If such an
IOException
is not thrown, then the stream is reset to a state such that all the bytes read since the most recent call tomark
(or since the start of the file, ifmark
has not been called) will be resupplied to subsequent callers of theread
method, followed by any bytes that otherwise would have been the next input data as of the time of the call toreset
.
- If the method
- If the method
markSupported
returnsfalse
, then:- The call to
reset
may throw anIOException
. - If an
IOException
is not thrown, then the stream is reset to a fixed state that depends on the particular type of the input stream and how it was created. The bytes that will be supplied to subsequent callers of theread
method depend on the particular type of the input stream.
- The call to
The method
reset
for classInputStream
does nothing and always throws anIOException
.- Overrides:
reset
in classjava.io.InputStream
- Throws:
java.io.IOException
- if this stream has not been marked or if the mark has been invalidated.- See Also:
InputStream.mark(int)
,IOException
- If the method
-
markSupported
public boolean markSupported()
Tests if this input stream supports themark
andreset
methods. ThemarkSupported
method ofInputStream
returnsfalse
.- Overrides:
markSupported
in classjava.io.InputStream
- Returns:
true
if this true type supports the mark and reset method;false
otherwise.- See Also:
InputStream.mark(int)
,InputStream.reset()
-
-