All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scalax.io.ReadableByteChannelResource.scala Maven / Gradle / Ivy

The newest version!
package scalax.io

import java.io.BufferedInputStream
import java.nio.channels.{Channels, ReadableByteChannel}
import scalax.io.ResourceAdapting.{ChannelReaderAdapter, ChannelInputStreamAdapter}

/**
 * A ManagedResource for accessing and using ByteChannels.  Class can be created using the [[scalax.io.Resource]] object.
 */
class ReadableByteChannelResource[+A <: ReadableByteChannel] protected[io](opener: => A, closeAction:CloseAction[A]) extends BufferableInputResource[A, BufferedInputStream]
    with ResourceOps[A, ReadableByteChannelResource[A]] {
  def open() = opener
  override def acquireFor[B](f: (A) => B) = new CloseableResourceAcquirer(open,f,closeAction)()

  def prependCloseAction[B >: A](newAction: CloseAction[B]) = new ReadableByteChannelResource(opener,newAction :+ closeAction)
  def appendCloseAction[B >: A](newAction: CloseAction[B]) = new ReadableByteChannelResource(opener,closeAction +: newAction)

  def buffered = inputStream.buffered
  def inputStream = {
    val nResource = new ChannelInputStreamAdapter(opener)
    val closer = ResourceAdapting.closeAction(closeAction)
    Resource.fromInputStream(nResource)(closer)
  }
  def reader(implicit sourceCodec: Codec) = {
    val nResource = new ChannelReaderAdapter(opener,sourceCodec)
    val closer = ResourceAdapting.closeAction(closeAction)
    Resource.fromReader(nResource)(closer)
  }
  def readableByteChannel = this
  def bytesAsInts = inputStream.bytesAsInts // TODO optimize for byteChannel
  def chars(implicit codec: Codec) = reader(codec).chars  // TODO optimize for byteChannel
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy