PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/scala/grizzled/io/RichReader.scala

http://github.com/bmc/grizzled-scala
Scala | 82 lines | 22 code | 7 blank | 53 comment | 2 complexity | e05d5f9c8ede73a41668bbc5c72bbf44 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. ---------------------------------------------------------------------------
  3. This software is released under a BSD license, adapted from
  4. http://opensource.org/licenses/bsd-license.php
  5. Copyright (c) 2009-2010 Brian M. Clapper
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are
  9. met:
  10. * Redistributions of source code must retain the above copyright notice,
  11. this list of conditions and the following disclaimer.
  12. * Redistributions in binary form must reproduce the above copyright
  13. notice, this list of conditions and the following disclaimer in the
  14. documentation and/or other materials provided with the distribution.
  15. * Neither the names "clapper.org", "Grizzled Scala Library", nor the
  16. names of its contributors may be used to endorse or promote products
  17. derived from this software without specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  19. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  20. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  22. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. ---------------------------------------------------------------------------
  30. */
  31. package grizzled.io
  32. import scala.io.Source
  33. import scala.annotation.tailrec
  34. import scala.language.implicitConversions
  35. import java.io.{Reader, Writer}
  36. /** Provides additional methods, over and above those already present in
  37. * the Java `Reader` class. The `implicits` object contains implicit
  38. * conversions between `RichReader` and `Reader`.
  39. *
  40. * @param reader the reader to wrap
  41. */
  42. class RichReader(val reader: Reader) extends PartialReader[Char] {
  43. protected def convert(b: Int) = b.asInstanceOf[Char]
  44. /** Copy the reader to a writer, stopping on EOF. This method does no
  45. * buffering. If you want buffering, make sure you use a
  46. * `java.io.BufferedReader` and a `java.io.BufferedWriter. This method
  47. * does not close either object.
  48. *
  49. * @param out the output stream
  50. */
  51. def copyTo(out: Writer): Unit = {
  52. @tailrec def doCopyTo: Unit = {
  53. val c: Int = reader.read()
  54. if (c != -1) {
  55. out.write(c)
  56. // Tail recursion means never having to use a var.
  57. doCopyTo
  58. }
  59. }
  60. doCopyTo
  61. }
  62. }
  63. /** Companion object to `RichReader` class. Importing this object brings the
  64. * implicit conversations into scope.
  65. */
  66. object RichReader {
  67. implicit def readerToRichReader(reader: Reader) = new RichReader(reader)
  68. implicit def richReaderToReader(richReader: RichReader) = richReader.reader
  69. }