Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package it.unimi.dsi.sux4j.io;
  
  /*		 
   * Sux4J: Succinct data structures for Java
   *
   * Copyright (C) 2008-2014 Sebastiano Vigna 
   *
   *  This library is free software; you can redistribute it and/or modify it
   *  under the terms of the GNU Lesser General Public License as published by the Free
  *  Software Foundation; either version 3 of the License, or (at your option)
  *  any later version.
  *
  *  This library is distributed in the hope that it will be useful, but
  *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
  *  for more details.
  *
  *  You should have received a copy of the GNU Lesser General Public License
  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
  *
  */
 
 
 import java.util.List;
A wrapper exhibiting the lines of a file as a list.

An instance of this class allows to access the lines of a file as a java.util.List. Contrarily to a it.unimi.dsi.io.FileLinesCollection, direct access is possible and reasonably efficient, in particular when accessing nearby lines, and all returned mutable strings are separate, independent instances.

Similarly to it.unimi.dsi.io.FileLinesCollection, instead, it.unimi.dsi.fastutil.objects.AbstractObjectList.iterator() can be called any number of times, as it opens an independent input stream at each call. For the same reason, the returned iterator type (FileLinesList.FileLinesIterator) is java.io.Closeable, and should be closed after usage.

Note that toString() will return a single string containing all file lines separated by the string associated to the system property line.separator.

Warning: this class is not synchronised. Separate iterators use separate input streams, and can be accessed concurrently, but all calls to get(int) refer to the same input stream.

Implementation details

Instances of this class perform a full scan of the specified file at construction time, representing the list of pointers to the start of each line using the Elias–Fano representation. The memory occupation per line is thus bounded by 2 + log ℓ bits, where ℓ is the average line length.

Author(s):
Sebastiano Vigna
Since:
1.1
 
 public class FileLinesList extends AbstractObjectList<MutableStringimplements RandomAccessSerializable {
 	private static final long serialVersionUID = 1L;
The filename upon which this file-lines collection is based.
 
 	private final String filename;
The size of the list.
 
 	private final int size;
The buffer size for all instances of it.unimi.dsi.fastutil.io.FastBufferedInputStream.
 
 	private final int bufferSize;
The terminators that must be used to separate the file lines.
 
 	private final EnumSet<LineTerminatorterminators;
A byte buffer for character decoding. It is enough large to hold any line in the file.
 
 	private final ByteBuffer byteBuffer;
A character buffer for character decoding. It is enough large to hold any line in the file.
 
 	private final CharBuffer charBuffer;
A sparse selection structure keeping track of the start of each line in the file.
 
The fast buffered input stream used by get(int).
 
A decoder used by get(int).
 
 	private final CharsetDecoder decoder;
The charset specified at construction time.
 
 	private final Charset charset;

Creates a file-lines collection for the specified filename with the specified encoding, buffer size and terminator set.

Parameters:
filename a filename.
encoding an encoding.
bufferSize the buffer size for it.unimi.dsi.fastutil.io.FastBufferedInputStream.
terminators a set of line terminators.
	public FileLinesListfinal CharSequence filenamefinal String encodingfinal int bufferSizefinal EnumSet<FastBufferedInputStream.LineTerminatorterminators ) throws IOException {
		this. = bufferSize;
		this. = terminators;
		this. = filename.toString();
		 = new FastBufferedInputStreamnew FileInputStreamthis. ), bufferSize );
		 = (  = Charset.forNameencoding ) ).newDecoder();
		byte[] array = new byte[ 16 ];
		int count = 0, startlen;
		for(;;) {
			start = 0;
			while( ( len = .readLinearraystartarray.length - startterminators ) ) == array.length - start ) {
				start += len;
				array = ByteArrays.growarrayarray.length + 1 );
			};
			if ( len != -1 ) count++;
			else break;
		}
		 = count;
		 = ByteBuffer.wraparray );
		 = CharBuffer.wrapnew chararray.length ] );
			long pos = 0;
			byte[] buffer = .array();
			public boolean hasNext() {
				return  < ;
			}
			public long nextLong() {
				if ( ! hasNext() ) throw new NoSuchElementException();
				++;
				try {
					final long result = .position();
					.readLineterminators );
					return result;
				}
				catch ( IOException e ) {
					throw new RuntimeExceptione );
				}
			}
		});
	}

Creates a file-lines collection for the specified filename with the specified encoding, buffer size and with all terminators.

Parameters:
filename a filename.
encoding an encoding.
bufferSize the buffer size for it.unimi.dsi.fastutil.io.FastBufferedInputStream.
	public FileLinesListfinal CharSequence filenamefinal String encodingfinal int bufferSize ) throws IOException {
		thisfilenameencodingbufferSize. );
	}

Creates a file-lines collection for the specified filename with the specified encoding, default buffer size and with all terminators.

Parameters:
filename a filename.
encoding an encoding.
	public FileLinesListfinal CharSequence filenamefinal String encoding ) throws IOException {
		thisfilenameencoding. );
	}
	public int size() {
		return ;
	}
	public MutableString getfinal int index ) {
		return getindex );
	}
	public MutableString getfinal int indexfinal FastBufferedInputStream fastBufferedInputStreamfinal ByteBuffer byteBufferfinal CharBuffer charBufferfinal CharsetDecoder decoder ) {
		try {
			fastBufferedInputStream.position.getLongindex ) );
			byteBuffer.clear();
			byteBuffer.limitfastBufferedInputStream.readLinebyteBuffer.array(),  ) );
			charBuffer.clear();
			decoder.decodebyteBuffercharBuffertrue );
			return new MutableStringcharBuffer.array(), 0, charBuffer.position() );
		}
		catch ( IOException e ) {
			throw new RuntimeExceptione );
		}
	}

An iterator over the lines of a FileLinesList. Instances of this class open an java.io.InputStream, and thus should be closed after usage. A “safety-net” finaliser tries to take care of the cases in which closing an instance is impossible.
	public static final class FileLinesIterator extends AbstractObjectListIterator<MutableStringimplements SafelyCloseable {
An fast buffered input stream used exclusively by this iterator.
A byte buffer used exclusively by this iterator.
		private final ByteBuffer byteBuffer;
A character buffer used exclusively by this iterator.
		private final CharBuffer charBuffer;
A charset decoder used exclusively by this iterator.
		private final CharsetDecoder decoder;
The list of file lines associated to this iterator.
		private final FileLinesList fileLinesList;
The current position (line) in the file.
		private int pos;
		protected FileLinesIteratorfinal FileLinesList fileLinesListfinal int indexfinal FastBufferedInputStream inputStreamfinal CharsetDecoder decoderfinal ByteBuffer byteBufferfinal CharBuffer charBuffer ) {
			this. = inputStream;
			this. = decoder;
			this. = byteBuffer;
			this. = charBuffer;
			this. = fileLinesList;
			 = index;
		}
		public boolean hasNext() {
			return  < .;
		}
		public boolean hasPrevious() {
			return  > 0;
		}
		public MutableString next() {
			if ( !hasNext() ) throw new NoSuchElementException();
		}
		public MutableString previous() {
			if ( !hasPrevious() ) throw new NoSuchElementException();
		}
		public int nextIndex() {
			return ;
		}
		public int previousIndex() {
			return  - 1;
		}
		public synchronized void close() {
			if (  == null ) throw new IllegalStateException();
			try {
			}
			catch ( IOException e ) {
				throw new RuntimeExceptione );
			}
			finally {
				 = null;
			}
		}
		protected synchronized void finalize() throws Throwable {
			try {
				if (  != null ) close();
			}
			finally {
				super.finalize();
			}
		}
	}
	public FileLinesIterator listIteratorfinal int index ) {
		try {
			return new FileLinesIteratorthisindexnew FastBufferedInputStreamnew FileInputStream ),  ), .newDecoder(), ByteBuffer.wrapnew byte.array().length ] ), CharBuffer.wrapnew char.array().length ] ) );
		}
		catch ( FileNotFoundException e ) {
			throw new RuntimeExceptione );
		}
	}
	public String toString() {
		final MutableString separator = new MutableString( System.getProperty"line.separator" ) );
		final MutableString s = new MutableString();
		forMutableString lthis ) s.appendl ).appendseparator );
		return s.toString();
	}
New to GrepCode? Check out our FAQ X