Top Description Fields Constructors Methods
java.nio.file

pack-priv final Class FileChannelLinesSpliterator

extends Object
implements Spliterator<String>
Class Inheritance
All Implemented Interfaces
java.util.Spliterator
Imports
sun.nio.cs.ISO_8859_1, .UTF_8, .US_ASCII, java.io.BufferedReader, .IOException, .UncheckedIOException, java.nio.ByteBuffer, java.nio.channels.Channels, .FileChannel, .ReadableByteChannel, java.nio.charset.Charset, java.util.Set, .Spliterator, java.util.concurrent.atomic.AtomicInteger, java.util.function.Consumer, jdk.internal.access.SharedSecrets, .JavaNioAccess

A file-based lines spliterator, leveraging a shared mapped byte buffer and associated file channel, covering lines of a file for character encodings where line feed characters can be easily identified from character encoded bytes.

When the root spliterator is first split a mapped byte buffer will be created over the file for its size that was observed when the stream was created. Thus a mapped byte buffer is only required for parallel stream execution. Sub-spliterators will share that mapped byte buffer. Splitting will use the mapped byte buffer to find the closest line feed characters(s) to the left or right of the mid-point of covered range of bytes of the file. If a line feed is found then the spliterator is split with returned spliterator containing the identified line feed characters(s) at the end of its covered range of bytes.

Traversing will create a buffered reader, derived from the file channel, for the range of bytes of the file. The lines are then read from that buffered reader. Once traversing commences no further splitting can be performed and the reference to the mapped byte buffer will be set to null.

Field Summary

Modifier and TypeField and Description
private ByteBuffer
private final AtomicInteger
private final Charset
private final FileChannel
private final int
private int
private BufferedReader
pack-priv static final Set<Charset>

Constructor Summary

AccessConstructor and Description
pack-priv
FileChannelLinesSpliterator(FileChannel fc, Charset cs, int index, int fence)

private
FileChannelLinesSpliterator(FileChannel fc, Charset cs, int index, int fence, ByteBuffer buffer, AtomicInteger bufRefCount)

Method Summary

Modifier and TypeMethod and Description
public int
characteristics()

Implements java.util.Spliterator.characteristics.

Returns a set of characteristics of this Spliterator and its elements.

pack-priv void
public long
estimateSize()

Implements java.util.Spliterator.estimateSize.

Returns an estimate of the number of elements that would be encountered by a forEachRemaining traversal, or returns Long#MAX_VALUE if infinite, unknown, or too expensive to compute.

public void
forEachRemaining(Consumer<? super String>
The action
action
)

Overrides default java.util.Spliterator.forEachRemaining.

Performs the given action for each remaining element, sequentially in the current thread, until all elements have been processed or the action throws an exception.

private BufferedReader
public long
getExactSizeIfKnown()

Overrides default java.util.Spliterator.getExactSizeIfKnown.

Convenience method that returns estimateSize() if this Spliterator is SIZED, else -1.

private ByteBuffer
private String
public boolean
tryAdvance(Consumer<? super String>
The action whose operation is performed at-most once
action
)

Implements java.util.Spliterator.tryAdvance.

If a remaining element exists: performs the given action on it, returning true; else returns false.

public Spliterator<String>
trySplit()

Implements java.util.Spliterator.trySplit.

If this spliterator can be partitioned, returns a Spliterator covering elements, that will, upon return from this method, not be covered by this Spliterator.

private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bufferback to summary
private ByteBuffer buffer
bufRefCountback to summary
private final AtomicInteger bufRefCount
csback to summary
private final Charset cs
fcback to summary
private final FileChannel fc
fenceback to summary
private final int fence
indexback to summary
private int index
readerback to summary
private BufferedReader reader
SUPPORTED_CHARSETSback to summary
pack-priv static final Set<Charset> SUPPORTED_CHARSETS

Constructor Detail

FileChannelLinesSpliteratorback to summary
pack-priv FileChannelLinesSpliterator(FileChannel fc, Charset cs, int index, int fence)
FileChannelLinesSpliteratorback to summary
private FileChannelLinesSpliterator(FileChannel fc, Charset cs, int index, int fence, ByteBuffer buffer, AtomicInteger bufRefCount)

Method Detail

characteristicsback to summary
public int characteristics()

Implements java.util.Spliterator.characteristics.

Doc from java.util.Spliterator.characteristics.

Returns a set of characteristics of this Spliterator and its elements. The result is represented as ORed values from ORDERED, DISTINCT, SORTED, SIZED, NONNULL, IMMUTABLE, CONCURRENT, SUBSIZED. Repeated calls to characteristics() on a given spliterator, prior to or in-between calls to trySplit, should always return the same result.

If a Spliterator reports an inconsistent set of characteristics (either those returned from a single invocation or across multiple invocations), no guarantees can be made about any computation using this Spliterator.

Returns:int

a representation of characteristics

Annotations
@Override
closeback to summary
pack-priv void close()
estimateSizeback to summary
public long estimateSize()

Implements java.util.Spliterator.estimateSize.

Doc from java.util.Spliterator.estimateSize.

Returns an estimate of the number of elements that would be encountered by a forEachRemaining traversal, or returns Long#MAX_VALUE if infinite, unknown, or too expensive to compute.

If this Spliterator is SIZED and has not yet been partially traversed or split, or this Spliterator is SUBSIZED and has not yet been partially traversed, this estimate must be an accurate count of elements that would be encountered by a complete traversal. Otherwise, this estimate may be arbitrarily inaccurate, but must decrease as specified across invocations of trySplit.

Returns:long

the estimated size, or Long.MAX_VALUE if infinite, unknown, or too expensive to compute.

Annotations
@Override
forEachRemainingback to summary
public void forEachRemaining(Consumer<? super String> action)

Overrides default java.util.Spliterator.forEachRemaining.

Doc from java.util.Spliterator.forEachRemaining.

Performs the given action for each remaining element, sequentially in the current thread, until all elements have been processed or the action throws an exception. If this Spliterator is ORDERED, actions are performed in encounter order. Exceptions thrown by the action are relayed to the caller.

Subsequent behavior of a spliterator is unspecified if the action throws an exception.

Parameters
action:Consumer<? super String>

The action

Annotations
@Override
getBufferedReaderback to summary
private BufferedReader getBufferedReader()
getExactSizeIfKnownback to summary
public long getExactSizeIfKnown()

Overrides default java.util.Spliterator.getExactSizeIfKnown.

Doc from java.util.Spliterator.getExactSizeIfKnown.

Convenience method that returns estimateSize() if this Spliterator is SIZED, else -1.

Returns:long

the exact size, if known, else -1.

Annotations
@Override
getMappedByteBufferback to summary
private ByteBuffer getMappedByteBuffer()
readLineback to summary
private String readLine()
tryAdvanceback to summary
public boolean tryAdvance(Consumer<? super String> action)

Implements java.util.Spliterator.tryAdvance.

Doc from java.util.Spliterator.tryAdvance.

If a remaining element exists: performs the given action on it, returning true; else returns false. If this Spliterator is ORDERED the action is performed on the next element in encounter order. Exceptions thrown by the action are relayed to the caller.

Subsequent behavior of a spliterator is unspecified if the action throws an exception.

Parameters
action:Consumer<? super String>

The action whose operation is performed at-most once

Returns:boolean

false if no remaining elements existed upon entry to this method, else true.

Annotations
@Override
trySplitback to summary
public Spliterator<String> trySplit()

Implements java.util.Spliterator.trySplit.

Doc from java.util.Spliterator.trySplit.

If this spliterator can be partitioned, returns a Spliterator covering elements, that will, upon return from this method, not be covered by this Spliterator.

If this Spliterator is ORDERED, the returned Spliterator must cover a strict prefix of the elements.

Unless this Spliterator covers an infinite number of elements, repeated calls to trySplit() must eventually return null. Upon non-null return:

  • the value reported for estimateSize() before splitting, must, after splitting, be greater than or equal to estimateSize() for this and the returned Spliterator; and
  • if this Spliterator is SUBSIZED, then estimateSize() for this spliterator before splitting must be equal to the sum of estimateSize() for this and the returned Spliterator after splitting.

This method may return null for any reason, including emptiness, inability to split after traversal has commenced, data structure constraints, and efficiency considerations.

Returns:Spliterator<String>

a Spliterator covering some portion of the elements, or null if this spliterator cannot be split

Annotations
@Override
unmapback to summary
private void unmap()