- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- java.io.BufferedInputStream
-
- javax.mail.util.SharedFileInputStream
-
- All Implemented Interfaces:
- Closeable, AutoCloseable, SharedInputStream
public class SharedFileInputStream extends BufferedInputStream implements SharedInputStream
ASharedFileInputStream
is aBufferedInputStream
that buffers data from the file and supports themark
andreset
methods. It also supports thenewStream
method that allows you to create other streams that represent subsets of the file. ARandomAccessFile
object is used to access the file data.Note that when the SharedFileInputStream is closed, all streams created with the
newStream
method are also closed. This allows the creator of the SharedFileInputStream object to control access to the underlying file and ensure that it is closed when needed, to avoid leaking file descriptors. Note also that this behavior contradicts the requirements of SharedInputStream and may change in a future release.- Since:
- JavaMail 1.4
- Author:
- Bill Shannon
-
-
Field Summary
Fields Modifier and Type Field and Description protected long
bufpos
The file offset that corresponds to the first byte in the read buffer.protected int
bufsize
The normal size of the read buffer.protected long
datalen
The amount of data in this subset of the file.protected RandomAccessFile
in
The file containing the data.protected long
start
The file offset of the start of data in this subset of the file.
-
Constructor Summary
Constructors Constructor and Description SharedFileInputStream(File file)
Creates aSharedFileInputStream
for the file.SharedFileInputStream(File file, int size)
Creates aSharedFileInputStream
with the specified buffer size.SharedFileInputStream(String file)
Creates aSharedFileInputStream
for the named fileSharedFileInputStream(String file, int size)
Creates aSharedFileInputStream
with the specified buffer size.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description int
available()
Returns the number of bytes that can be read from this input stream without blocking.void
close()
Closes this input stream and releases any system resources associated with the stream.protected void
finalize()
Force this stream to close.long
getPosition()
Return the current position in the InputStream, as an offset from the beginning of the InputStream.void
mark(int readlimit)
See the general contract of themark
method ofInputStream
.boolean
markSupported()
Tests if this input stream supports themark
andreset
methods.InputStream
newStream(long start, long end)
Return a new InputStream representing a subset of the data from this InputStream, starting atstart
(inclusive) up toend
(exclusive).int
read()
See the general contract of theread
method ofInputStream
.int
read(byte[] b, int off, int len)
Reads bytes from this stream into the specified byte array, starting at the given offset.void
reset()
See the general contract of thereset
method ofInputStream
.long
skip(long n)
See the general contract of theskip
method ofInputStream
.-
Methods inherited from class java.io.FilterInputStream
read
-
-
-
-
Field Detail
-
in
protected RandomAccessFile in
The file containing the data. Shared by all related SharedFileInputStreams.
-
bufsize
protected int bufsize
The normal size of the read buffer.
-
bufpos
protected long bufpos
The file offset that corresponds to the first byte in the read buffer.
-
start
protected long start
The file offset of the start of data in this subset of the file.
-
datalen
protected long datalen
The amount of data in this subset of the file.
-
-
Constructor Detail
-
SharedFileInputStream
public SharedFileInputStream(File file) throws IOException
Creates aSharedFileInputStream
for the file.- Parameters:
file
- the file- Throws:
IOException
-
SharedFileInputStream
public SharedFileInputStream(String file) throws IOException
Creates aSharedFileInputStream
for the named file- Parameters:
file
- the file- Throws:
IOException
-
SharedFileInputStream
public SharedFileInputStream(File file, int size) throws IOException
Creates aSharedFileInputStream
with the specified buffer size.- Parameters:
file
- the filesize
- the buffer size.- Throws:
IllegalArgumentException
- if size <= 0.IOException
-
SharedFileInputStream
public SharedFileInputStream(String file, int size) throws IOException
Creates aSharedFileInputStream
with the specified buffer size.- Parameters:
file
- the filesize
- the buffer size.- Throws:
IllegalArgumentException
- if size <= 0.IOException
-
-
Method Detail
-
read
public int read() throws IOException
See the general contract of theread
method ofInputStream
.- Overrides:
read
in classBufferedInputStream
- Returns:
- the next byte of data, or
-1
if the end of the stream is reached. - Throws:
IOException
- if an I/O error occurs.
-
read
public int read(byte[] b, int off, int len) throws IOException
Reads bytes from this stream into the specified byte array, starting at the given offset.This method implements the general contract of the corresponding
method of theread
class.InputStream
- Overrides:
read
in classBufferedInputStream
- Parameters:
b
- destination buffer.off
- offset at which to start storing bytes.len
- maximum number of bytes to read.- Returns:
- the number of bytes read, or
-1
if the end of the stream has been reached. - Throws:
IOException
- if an I/O error occurs.
-
skip
public long skip(long n) throws IOException
See the general contract of theskip
method ofInputStream
.- Overrides:
skip
in classBufferedInputStream
- Parameters:
n
- the number of bytes to be skipped.- Returns:
- the actual number of bytes skipped.
- Throws:
IOException
- if an I/O error occurs.
-
available
public int available() throws IOException
Returns the number of bytes that can be read from this input stream without blocking.- Overrides:
available
in classBufferedInputStream
- Returns:
- the number of bytes that can be read from this input stream without blocking.
- Throws:
IOException
- if an I/O error occurs.
-
mark
public void mark(int readlimit)
See the general contract of themark
method ofInputStream
.- Overrides:
mark
in classBufferedInputStream
- Parameters:
readlimit
- the maximum limit of bytes that can be read before the mark position becomes invalid.- See Also:
reset()
-
reset
public void reset() throws IOException
See the general contract of thereset
method ofInputStream
.If
markpos
is-1
(no mark has been set or the mark has been invalidated), anIOException
is thrown. Otherwise,pos
is set equal tomarkpos
.- Overrides:
reset
in classBufferedInputStream
- Throws:
IOException
- if this stream has not been marked or if the mark has been invalidated.- See Also:
mark(int)
-
markSupported
public boolean markSupported()
Tests if this input stream supports themark
andreset
methods. ThemarkSupported
method ofSharedFileInputStream
returnstrue
.- Overrides:
markSupported
in classBufferedInputStream
- Returns:
- a
boolean
indicating if this stream type supports themark
andreset
methods. - See Also:
InputStream.mark(int)
,InputStream.reset()
-
close
public void close() throws IOException
Closes this input stream and releases any system resources associated with the stream.- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceAutoCloseable
- Overrides:
close
in classBufferedInputStream
- Throws:
IOException
- if an I/O error occurs.
-
getPosition
public long getPosition()
Return the current position in the InputStream, as an offset from the beginning of the InputStream.- Specified by:
getPosition
in interfaceSharedInputStream
- Returns:
- the current position
-
newStream
public InputStream newStream(long start, long end)
Return a new InputStream representing a subset of the data from this InputStream, starting atstart
(inclusive) up toend
(exclusive).start
must be non-negative. Ifend
is -1, the new stream ends at the same place as this stream. The returned InputStream will also implement the SharedInputStream interface.- Specified by:
newStream
in interfaceSharedInputStream
- Parameters:
start
- the starting positionend
- the ending position + 1- Returns:
- the new stream
-
-
Traduction non disponible
Les API Java ne sont pas encore traduites en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Version en cache
21/08/2025 19:05:56 Cette version de la page est en cache (à la date du 21/08/2025 19:05:56) afin d'accélérer le traitement.Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la version plus récente de la page.
Document créé le 19/04/2008, dernière modification le 18/08/2025
Source du document imprimé : https://www.gaudry.be/java-api-javaee-rf-javax/mail/util/sharedfileinputstream.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.