Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */package scala
package io
import scala.collection.AbstractIteratorimport java.io.{ FileInputStream, InputStream, PrintStream, File => JFile, Closeable }
import java.net.{ URI, URL }
/** This object provides convenience methods to create an iterable
* representation of a source file.
*
* @author Burak Emir, Paul Phillips
* @version 1.0, 19/08/2004
*/objectSource{
valDefaultBufSize = 2048/** Creates a `Source` from System.in.
*/defstdin= fromInputStream(System.in)
/** Creates a Source from an Iterable.
*
* @param iterable the Iterable
* @return the Source
*/deffromIterable(iterable: Iterable[Char]): Source = newSource {
val iter = iterable.iterator
} withReset(() => fromIterable(iterable))
/** Creates a Source instance from a single character.
*/deffromChar(c: Char): Source = fromIterable(Array(c))
/** creates Source from array of characters, with empty description.
*/deffromChars(chars: Array[Char]): Source = fromIterable(chars)
/** creates Source from a String, with no description.
*/deffromString(s: String): Source = fromIterable(s)
/** creates Source from file with given name, setting its description to
* filename.
*/deffromFile(name: String)(implicit codec: Codec): BufferedSource =
fromFile(newJFile(name))(codec)
/** creates Source from file with given name, using given encoding, setting
* its description to filename.
*/deffromFile(name: String, enc: String): BufferedSource =
fromFile(name)(Codec(enc))
/** creates `ource` from file with given file `URI`.
*/deffromFile(uri: URI)(implicit codec: Codec): BufferedSource =
fromFile(newJFile(uri))(codec)
/** creates Source from file with given file: URI
*/deffromFile(uri: URI, enc: String): BufferedSource =
fromFile(uri)(Codec(enc))
/** creates Source from file, using default character encoding, setting its
* description to filename.
*/deffromFile(file: JFile)(implicit codec: Codec): BufferedSource =
fromFile(file, Source.DefaultBufSize)(codec)
/** same as fromFile(file, enc, Source.DefaultBufSize)
*/deffromFile(file: JFile, enc: String): BufferedSource =
fromFile(file)(Codec(enc))
deffromFile(file: JFile, enc: String, bufferSize: Int): BufferedSource =
fromFile(file, bufferSize)(Codec(enc))
/** Creates Source from `file`, using given character encoding, setting
* its description to filename. Input is buffered in a buffer of size
* `bufferSize`.
*/deffromFile(file: JFile, bufferSize: Int)(implicit codec: Codec): BufferedSource = {
val inputStream = newFileInputStream(file)
createBufferedSource(
inputStream,
bufferSize,
() => fromFile(file, bufferSize)(codec),
() => inputStream.close()
)(codec) withDescription ("file:" + file.getAbsolutePath)
}
/** Create a `Source` from array of bytes, decoding
* the bytes according to codec.
*
* @return the created `Source` instance.
*/deffromBytes(bytes: Array[Byte])(implicit codec: Codec): Source =
fromString(newString(bytes, codec.name))
deffromBytes(bytes: Array[Byte], enc: String): Source =
fromBytes(bytes)(Codec(enc))
/** Create a `Source` from array of bytes, assuming
* one byte per character (ISO-8859-1 encoding.)
*/deffromRawBytes(bytes: Array[Byte]): Source =
fromString(newString(bytes, Codec.ISO8859.name))
/** creates `Source` from file with given file: URI
*/deffromURI(uri: URI)(implicit codec: Codec): BufferedSource =
fromFile(newJFile(uri))(codec)
/** same as fromURL(new URL(s))(Codec(enc))
*/deffromURL(s: String, enc: String): BufferedSource =
fromURL(s)(Codec(enc))
/** same as fromURL(new URL(s))
*/deffromURL(s: String)(implicit codec: Codec): BufferedSource =
fromURL(newURL(s))(codec)
/** same as fromInputStream(url.openStream())(Codec(enc))
*/deffromURL(url: URL, enc: String): BufferedSource =
fromURL(url)(Codec(enc))
/** same as fromInputStream(url.openStream())(codec)
*/deffromURL(url: URL)(implicit codec: Codec): BufferedSource =
fromInputStream(url.openStream())(codec)
/** Reads data from inputStream with a buffered reader, using the encoding
* in implicit parameter codec.
*
* @param inputStream the input stream from which to read
* @param bufferSize buffer size (defaults to Source.DefaultBufSize)
* @param reset a () => Source which resets the stream (if unset, reset() will throw an Exception)
* @param close a () => Unit method which closes the stream (if unset, close() will do nothing)
* @param codec (implicit) a scala.io.Codec specifying behavior (defaults to Codec.default)
* @return the buffered source
*/defcreateBufferedSource(
inputStream: InputStream,
bufferSize: Int = DefaultBufSize,
reset: () => Source = null,
close: () => Unit = null
)(implicit codec: Codec): BufferedSource = {
// workaround for default arguments being unable to refer to other parametersval resetFn = if (reset == null) () => createBufferedSource(inputStream, bufferSize, reset, close)(codec) else reset
newBufferedSource(inputStream, bufferSize)(codec) withReset resetFn withClose close
}
deffromInputStream(is: InputStream, enc: String): BufferedSource =
fromInputStream(is)(Codec(enc))
deffromInputStream(is: InputStream)(implicit codec: Codec): BufferedSource =
createBufferedSource(is, reset = () => fromInputStream(is)(codec), close = () => is.close())(codec)
/** Reads data from a classpath resource, using either a context classloader (default) or a passed one.
*
* @param resource name of the resource to load from the classpath
* @param classLoader classloader to be used, or context classloader if not specified
* @return the buffered source
*/deffromResource(resource: String, classLoader: ClassLoader = Thread.currentThread().getContextClassLoader())(implicit codec: Codec): BufferedSource =
fromInputStream(classLoader.getResourceAsStream(resource))
}
/** An iterable representation of source data.
* It may be reset with the optional [[reset]] method.
*
* Subclasses must supply [[scala.io.Source.iter the underlying iterator]].
*
* Error handling may be customized by overriding the [[scala.io.Source.report report]] method.
*
* The [[scala.io.Source.ch current input]] and [[scala.io.Source.pos position]],
* as well as the [[scala.io.Source.next next character]] methods delegate to
* [[scala.io.Source#Positioner the positioner]].
*
* The default positioner encodes line and column numbers in the position passed to [[report]].
* This behavior can be changed by supplying a
* [[scala.io.Source.withPositioning(pos:* custom positioner]].
*
*/abstractclassSourceextendsIterator[Char] withCloseable{
/** the actual iterator */protectedval iter: Iterator[Char]
// ------ public values/** description of this source, default empty */var descr: String = ""var nerrors = 0var nwarnings = 0privatedeflineNum(line: Int): String = (getLines() drop (line - 1) take 1).mkString
classLineIteratorextendsAbstractIterator[String] withIterator[String] {
private[this] val sb = newStringBuilderlazyval iter: BufferedIterator[Char] = Source.this.iter.buffered
defisNewline(ch: Char) = ch == '\r' || ch == '\n'
defgetc() = iter.hasNext && {
val ch = iter.next()
if (ch == '\n') falseelseif (ch == '\r') {
if (iter.hasNext && iter.head == '\n')
iter.next()
false
}
else {
sb append ch
true
}
}
defhasNext= iter.hasNext
defnext= {
sb.clear()
while (getc()) { }
sb.toString
}
}
/** Returns an iterator who returns lines (NOT including newline character(s)).
* It will treat any of \r\n, \r, or \n as a line separator (longest match) - if
* you need more refined behavior you can subclass Source#LineIterator directly.
*/defgetLines(): Iterator[String] = newLineIterator()
/** Returns `'''true'''` if this source has more characters.
*/defhasNext= iter.hasNext
/** Returns next character.
*/defnext(): Char = positioner.next()
classPositioner(encoder: Position) {
defthis() = this(RelaxedPosition)
/** the last character returned by next. */var ch: Char = _
/** position of last character returned by next */var pos = 0/** current line and column */var cline = 1var ccol = 1/** default col increment for tabs '\t', set to 4 initially */var tabinc = 4defnext(): Char = {
ch = iter.next()
pos = encoder.encode(cline, ccol)
ch match {
case '\n' =>
ccol = 1
cline += 1case '\t' =>
ccol += tabinc
case _ =>
ccol += 1
}
ch
}
}
/** A Position implementation which ignores errors in
* the positions.
*/objectRelaxedPositionextendsPosition{
defcheckInput(line: Int, column: Int): Unit = ()
}
objectRelaxedPositionerextendsPositioner(RelaxedPosition) { }
objectNoPositionerextendsPositioner(Position) {
overridedefnext(): Char = iter.next()
}
defch= positioner.ch
defpos= positioner.pos
/** Reports an error message to the output stream `out`.
*
* @param pos the source position (line/column)
* @param msg the error message to report
* @param out PrintStream to use (optional: defaults to `Console.err`)
*/defreportError(
pos: Int,
msg: String,
out: PrintStream = Console.err)
{
nerrors += 1
report(pos, msg, out)
}
privatedefspaces(n: Int) = List.fill(n)(' ').mkString
/**
* @param pos the source position (line/column)
* @param msg the error message to report
* @param out PrintStream to use
*/defreport(pos: Int, msg: String, out: PrintStream) {
val line = Position line pos
val col = Position column pos
out println "%s:%d:%d: %s%s%s^".format(descr, line, col, msg, lineNum(line), spaces(col - 1))
}
/**
* @param pos the source position (line/column)
* @param msg the warning message to report
* @param out PrintStream to use (optional: defaults to `Console.out`)
*/defreportWarning(
pos: Int,
msg: String,
out: PrintStream = Console.out)
{
nwarnings += 1
report(pos, "warning! " + msg, out)
}
private[this] var resetFunction: () => Source = nullprivate[this] var closeFunction: () => Unit = nullprivate[this] var positioner: Positioner = RelaxedPositionerdefwithReset(f: () => Source): this.type = {
resetFunction = f
this
}
defwithClose(f: () => Unit): this.type = {
closeFunction = f
this
}
defwithDescription(text: String): this.type = {
descr = text
this
}
/** Change or disable the positioner. */defwithPositioning(on: Boolean): this.type = {
positioner = if (on) RelaxedPositionerelseNoPositionerthis
}
defwithPositioning(pos: Positioner): this.type = {
positioner = pos
this
}
/** The close() method closes the underlying resource. */defclose() {
if (closeFunction != null) closeFunction()
}
/** The reset() method creates a fresh copy of this Source. */defreset(): Source =
if (resetFunction != null) resetFunction()
elsethrownewUnsupportedOperationException("Source's reset() method was not set.")
}