diff options
-rw-r--r-- | src/library/scala/Iterator.scala | 6 | ||||
-rw-r--r-- | src/library/scala/io/Source.scala | 32 | ||||
-rw-r--r-- | src/library/scala/xml/NodeBuffer.scala | 30 |
3 files changed, 52 insertions, 16 deletions
diff --git a/src/library/scala/Iterator.scala b/src/library/scala/Iterator.scala index ad71b86232..e652cc4ded 100644 --- a/src/library/scala/Iterator.scala +++ b/src/library/scala/Iterator.scala @@ -536,4 +536,10 @@ trait Iterator[+A] { res.toList } + /** Transforms the elements of this iterator into a string, thereby destroying it. + * Has the same effect as toList.mkString(start,sep,end) + */ + def mkString(start: String, sep: String, end: String): String = + toList.mkString(start, sep, end) + } diff --git a/src/library/scala/io/Source.scala b/src/library/scala/io/Source.scala index dfdbd86849..cef8fd25a8 100644 --- a/src/library/scala/io/Source.scala +++ b/src/library/scala/io/Source.scala @@ -12,7 +12,7 @@ package scala.io -import java.io.{File, FileInputStream, PrintStream} +import java.io.{File, FileInputStream, InputStream, PrintStream} import compat.StringBuilder @@ -164,6 +164,36 @@ object Source { s } + /** reads data from inputstream into a byte array, and calls fromBytes with given encoding. + * If maxlen is given, reads not more bytes than maxlen. if maxlen was not given, or was <= 0, then + * whole inputstream is read and closed afterwards. + * + * @param istream the input stream from which to read + * @param enc the encoding to apply to the bytes + * @param maxlen optionally, a positive int specifying maximum number of bytes to read + */ + def fromInputStream(istream: InputStream, enc: String, maxlen: Option[Int]): Source = { + val BUFSIZE = 1024 + val limit = maxlen match { case Some(i) => i; case None => 0 } + val bi = new java.io.BufferedInputStream(istream, BUFSIZE) + val bytes = new collection.mutable.ArrayBuffer[Byte]() + var b = 0 + var i = 0 + while( {b = bi.read; i = i + 1; b} != -1 && (limit <= 0 || i < limit)) { + bytes += b.toByte; + } + if(limit <= 0) bi.close + fromBytes(bytes.toArray, enc) + } + + /** same as fromInputStream(is, enc, None) */ + def fromInputStream(is: InputStream, enc: String): Source = + fromInputStream(is, enc, None) + + /** same as fromInputStream(is, "utf-8", None) */ + def fromInputStream(is: InputStream): Source = + fromInputStream(is, "utf-8", None) + } /** The class <code>Source</code> implements an iterable representation diff --git a/src/library/scala/xml/NodeBuffer.scala b/src/library/scala/xml/NodeBuffer.scala index 15a81d8e3d..4bc4b61ab1 100644 --- a/src/library/scala/xml/NodeBuffer.scala +++ b/src/library/scala/xml/NodeBuffer.scala @@ -26,20 +26,25 @@ package scala.xml class NodeBuffer extends scala.collection.mutable.ArrayBuffer[Node] { /** - * Append a single node to this buffer, returns reference on this - * NodeBuffer for convenience. + * Append given object to this buffer, returns reference on this NodeBuffer + * for convenience. Some rules apply: If o is null, it is ignored. If it is + * an Iterator or Iterable, its elements will be added. If o is a node, it is + * added as it is. If it is anything else, it gets wrapped in an Atom. * - * Append an iterable object to this buffer, returns reference on - * this NodeBuffer for convenience. * - * Append given string as a <code>scala.xml.Text</code> node to this - * buffer, returns reference on this NodeBuffer for convenience. - * - * @param o ... - * @return ... + * @param o converts to an xml node and adds to this node buffer + * @return this nodebuffer */ def &+(o: Any): NodeBuffer = { o match { + case null => // ignore null + + case _:Unit => + // ignore + case it:Iterator[_] => + while(it.hasNext) { + this &+ it.next + } case n:Node => super.+(n) case ns:Iterable[_] => @@ -55,10 +60,5 @@ class NodeBuffer extends scala.collection.mutable.ArrayBuffer[Node] { } this } - /* - def +(o: AnyVal): NodeBuffer = { - super.+(Text(o.toString())); - this - } - */ + } |