Class FileLinesMutableStringIterable
- java.lang.Object
-
- it.unimi.dsi.io.FileLinesMutableStringIterable
-
- All Implemented Interfaces:
it.unimi.dsi.fastutil.Size64
,java.lang.Iterable<MutableString>
public class FileLinesMutableStringIterable extends java.lang.Object implements java.lang.Iterable<MutableString>, it.unimi.dsi.fastutil.Size64
A wrapper exhibiting the lines of a file as anIterable
of mutable strings.Warning: the lines returned by iterators generated by instances of this class are not cacheable. The returned value is a
MutableString
instance that is reused at each call, and that is modified by a call tohasNext()
. Thus, for instance,ObjectIterators.unwrap(fileLinesMutableStringIterable.iterator());
will not give the expected results. UseallLines()
orallLinesBig()
for that purpose.The result of a call to
iterator()
can be used to scan the file; each call will open an independent input stream. The returned iterator type (FileLinesIterator
) isCloseable
, and should be closed after usage. Exhausted iterators, however, will be closed automagically.Using a suitable constructor it is possible to specify a decompression class, which must extend
InputStream
and provide a constructor accepting anInputStream
(e.g.,GZIPInputStream
if the file is compressed ingzip
format).Convenience static methods makes it possible to build on the fly an iterator over an input stream using the same conventions.
This class implements
size64()
, which will return the number of lines of the file, computed with a full scan at the first invocation. However, it is also possible to specify at construction time the number of lines in the file to skip the first scan. It is responsibility of the caller that the specified size and the actual number of lines in the file do match.- Since:
- 2.6.17
- Author:
- Sebastiano Vigna
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FileLinesMutableStringIterable.FileLinesIterator
An iterator over the lines of aFileLinesMutableStringIterable
.
-
Constructor Summary
Constructors Constructor Description FileLinesMutableStringIterable(java.lang.String filename)
Creates a file-lines iterable for the specified filename using the platform encoding.FileLinesMutableStringIterable(java.lang.String filename, long size)
Creates a file-lines iterable for the specified filename and size using the platform encoding.FileLinesMutableStringIterable(java.lang.String filename, long size, java.lang.Class<? extends java.io.InputStream> decompressor)
Creates a file-lines iterable for the specified filename and size using the platform encoding, optionally assuming that the file is compressed.FileLinesMutableStringIterable(java.lang.String filename, long size, java.lang.String encoding)
Creates a file-lines iterable for the specified filename and size using the specified encoding.FileLinesMutableStringIterable(java.lang.String filename, long size, java.nio.charset.Charset encoding)
Creates a file-lines iterable for the specified filename and size using the specified encoding.FileLinesMutableStringIterable(java.lang.String filename, long size, java.nio.charset.Charset encoding, java.lang.Class<? extends java.io.InputStream> decompressor)
Creates a file-lines iterable for the specified filename and size with the specified encoding, optionally assuming that the file is compressed.FileLinesMutableStringIterable(java.lang.String filename, java.lang.Class<? extends java.io.InputStream> decompressor)
Creates a file-lines iterable for the specified filename using the platform encoding, optionally assuming that the file is compressed.FileLinesMutableStringIterable(java.lang.String filename, java.lang.String encoding)
Creates a file-lines iterable for the specified filename using the specified encoding.FileLinesMutableStringIterable(java.lang.String filename, java.lang.String encoding, java.lang.Class<? extends java.io.InputStream> decompressor)
Creates a file-lines iterable for the specified filename with the specified encoding, optionally assuming that the file is compressed.FileLinesMutableStringIterable(java.lang.String filename, java.nio.charset.Charset encoding)
Creates a file-lines iterable for the specified filename using the specified encoding.FileLinesMutableStringIterable(java.lang.String filename, java.nio.charset.Charset encoding, java.lang.Class<? extends java.io.InputStream> decompressor)
Creates a file-lines iterable for the specified filename with the specified encoding, optionally assuming that the file is compressed.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description it.unimi.dsi.fastutil.objects.ObjectList<java.lang.String>
allLines()
Returns all lines as a list.it.unimi.dsi.fastutil.objects.ObjectBigList<java.lang.String>
allLinesBig()
Returns all lines as a big list.FileLinesMutableStringIterable.FileLinesIterator
iterator()
static FileLinesMutableStringIterable.FileLinesIterator
iterator(java.io.InputStream inputStream, java.nio.charset.Charset encoding)
A convenience method returning a one-offFileLinesMutableStringIterable.FileLinesIterator
reading from an input stream.static FileLinesMutableStringIterable.FileLinesIterator
iterator(java.io.InputStream inputStream, java.nio.charset.Charset encoding, java.lang.Class<? extends java.io.InputStream> decompressor)
A convenience method returning a one-offFileLinesMutableStringIterable.FileLinesIterator
reading from an input stream.long
size64()
-
-
-
Constructor Detail
-
FileLinesMutableStringIterable
public FileLinesMutableStringIterable(java.lang.String filename)
Creates a file-lines iterable for the specified filename using the platform encoding.- Parameters:
filename
- a filename.
-
FileLinesMutableStringIterable
public FileLinesMutableStringIterable(java.lang.String filename, long size)
Creates a file-lines iterable for the specified filename and size using the platform encoding.- Parameters:
filename
- a filename.size
- the number of lines in the file.
-
FileLinesMutableStringIterable
public FileLinesMutableStringIterable(java.lang.String filename, java.nio.charset.Charset encoding)
Creates a file-lines iterable for the specified filename using the specified encoding.- Parameters:
filename
- a filename.encoding
- an encoding.
-
FileLinesMutableStringIterable
public FileLinesMutableStringIterable(java.lang.String filename, long size, java.nio.charset.Charset encoding)
Creates a file-lines iterable for the specified filename and size using the specified encoding.- Parameters:
filename
- a filename.size
- the number of lines in the file.encoding
- an encoding.
-
FileLinesMutableStringIterable
public FileLinesMutableStringIterable(java.lang.String filename, java.lang.String encoding)
Creates a file-lines iterable for the specified filename using the specified encoding.- Parameters:
filename
- a filename.encoding
- an encoding.
-
FileLinesMutableStringIterable
public FileLinesMutableStringIterable(java.lang.String filename, long size, java.lang.String encoding)
Creates a file-lines iterable for the specified filename and size using the specified encoding.- Parameters:
filename
- a filename.size
- the number of lines in the file.encoding
- an encoding.
-
FileLinesMutableStringIterable
public FileLinesMutableStringIterable(java.lang.String filename, java.lang.Class<? extends java.io.InputStream> decompressor) throws java.lang.NoSuchMethodException, java.lang.SecurityException
Creates a file-lines iterable for the specified filename using the platform encoding, optionally assuming that the file is compressed.- Parameters:
filename
- a filename.decompressor
- a class extendingInputStream
that will be used as a decompressor, ornull
for no decompression.- Throws:
java.lang.NoSuchMethodException
java.lang.SecurityException
-
FileLinesMutableStringIterable
public FileLinesMutableStringIterable(java.lang.String filename, long size, java.lang.Class<? extends java.io.InputStream> decompressor) throws java.lang.NoSuchMethodException, java.lang.SecurityException
Creates a file-lines iterable for the specified filename and size using the platform encoding, optionally assuming that the file is compressed.- Parameters:
filename
- a filename.size
- the number of lines in the file.decompressor
- a class extendingInputStream
that will be used as a decompressor, ornull
for no decompression.- Throws:
java.lang.NoSuchMethodException
java.lang.SecurityException
-
FileLinesMutableStringIterable
public FileLinesMutableStringIterable(java.lang.String filename, java.nio.charset.Charset encoding, java.lang.Class<? extends java.io.InputStream> decompressor) throws java.lang.NoSuchMethodException, java.lang.SecurityException
Creates a file-lines iterable for the specified filename with the specified encoding, optionally assuming that the file is compressed.- Parameters:
filename
- a filename.encoding
- an encoding.decompressor
- a class extendingInputStream
that will be used as a decompressor, ornull
for no decompression.- Throws:
java.lang.NoSuchMethodException
java.lang.SecurityException
-
FileLinesMutableStringIterable
public FileLinesMutableStringIterable(java.lang.String filename, java.lang.String encoding, java.lang.Class<? extends java.io.InputStream> decompressor) throws java.lang.NoSuchMethodException, java.lang.SecurityException
Creates a file-lines iterable for the specified filename with the specified encoding, optionally assuming that the file is compressed.- Parameters:
filename
- a filename.encoding
- an encoding.decompressor
- a class extendingInputStream
that will be used as a decompressor, ornull
for no decompression.- Throws:
java.lang.NoSuchMethodException
java.lang.SecurityException
-
FileLinesMutableStringIterable
public FileLinesMutableStringIterable(java.lang.String filename, long size, java.nio.charset.Charset encoding, java.lang.Class<? extends java.io.InputStream> decompressor) throws java.lang.NoSuchMethodException, java.lang.SecurityException
Creates a file-lines iterable for the specified filename and size with the specified encoding, optionally assuming that the file is compressed.- Parameters:
filename
- a filename.size
- the number of lines in the file.encoding
- an encoding.decompressor
- a class extendingInputStream
that will be used as a decompressor, ornull
for no decompression.- Throws:
java.lang.NoSuchMethodException
java.lang.SecurityException
-
-
Method Detail
-
iterator
public FileLinesMutableStringIterable.FileLinesIterator iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<MutableString>
-
iterator
public static FileLinesMutableStringIterable.FileLinesIterator iterator(java.io.InputStream inputStream, java.nio.charset.Charset encoding)
A convenience method returning a one-offFileLinesMutableStringIterable.FileLinesIterator
reading from an input stream.- Parameters:
inputStream
- an input stream.encoding
- an encoding.- Returns:
- an iterator returning the lines contained in the provided input stream.
-
iterator
public static FileLinesMutableStringIterable.FileLinesIterator iterator(java.io.InputStream inputStream, java.nio.charset.Charset encoding, java.lang.Class<? extends java.io.InputStream> decompressor)
A convenience method returning a one-offFileLinesMutableStringIterable.FileLinesIterator
reading from an input stream.- Parameters:
inputStream
- an input stream.encoding
- an encoding.decompressor
- a class extendingInputStream
that will be used as a decompressor, ornull
for no decompression.- Returns:
- an iterator returning the lines contained in the provided input stream.
-
size64
public long size64()
- Specified by:
size64
in interfaceit.unimi.dsi.fastutil.Size64
-
allLines
public it.unimi.dsi.fastutil.objects.ObjectList<java.lang.String> allLines()
Returns all lines as a list.- Returns:
- all lines of the file wrapped by this file-lines collection.
- See Also:
allLinesBig()
- Implementation Specification:
- This method iterates over the lines of the file and accumulates the resulting strings
in a standard list. Thus, it will throw an exception on files with more than
Integer.MAX_VALUE
lines.
-
allLinesBig
public it.unimi.dsi.fastutil.objects.ObjectBigList<java.lang.String> allLinesBig()
Returns all lines as a big list.- Returns:
- all lines of the file wrapped by this file-lines collection.
- See Also:
allLines()
- Implementation Specification:
- This method iterates over the lines of the file and accumulates the resulting strings
in a big list. Thus, it supports files with more than
Integer.MAX_VALUE
lines.
-
-