src/Pure/General/bytes.scala
changeset 80373 dc220d47b85e
parent 80372 6e74f0fc8a52
child 80374 4f1374f56c0b
equal deleted inserted replaced
80372:6e74f0fc8a52 80373:dc220d47b85e
    39     if (bytes.length <= chunk_size) new Bytes(None, bytes, 0L, bytes.length.toLong)
    39     if (bytes.length <= chunk_size) new Bytes(None, bytes, 0L, bytes.length.toLong)
    40     else apply(bytes)
    40     else apply(bytes)
    41 
    41 
    42   val empty: Bytes = reuse_array(new Array(0))
    42   val empty: Bytes = reuse_array(new Array(0))
    43 
    43 
    44   def apply(s: CharSequence): Bytes = {
    44   def apply(s: CharSequence): Bytes =
    45     val str = s.toString
    45     if (s.isEmpty) empty
    46     if (str.isEmpty) empty
    46     else Builder.use(hint = s.length) { builder => builder += s }
    47     else Builder.use(hint = str.length) { builder => builder += str }
       
    48   }
       
    49 
    47 
    50   def apply(a: Array[Byte]): Bytes = apply(a, 0, a.length)
    48   def apply(a: Array[Byte]): Bytes = apply(a, 0, a.length)
    51 
    49 
    52   def apply(a: Array[Byte], offset: Int, length: Int): Bytes =
    50   def apply(a: Array[Byte], offset: Int, length: Int): Bytes =
    53     Builder.use(hint = length) { builder => builder += (a, offset, length) }
    51     Builder.use(hint = length) { builder => builder += (a, offset, length) }
   166       if (buffer_free() == 0) {
   164       if (buffer_free() == 0) {
   167         chunks += buffer.toByteArray
   165         chunks += buffer.toByteArray
   168         buffer = new ByteArrayOutputStream
   166         buffer = new ByteArrayOutputStream
   169       }
   167       }
   170 
   168 
   171     def += (string: String): Unit =
   169     def += (s: CharSequence): Unit = {
   172       if (string.nonEmpty) {
   170       val n = s.length
   173         if (UTF8.relevant(string)) { this += UTF8.bytes(string) }
   171       if (n > 0) {
       
   172         if (UTF8.relevant(s)) { this += UTF8.bytes(s.toString) }
   174         else {
   173         else {
   175           synchronized {
   174           synchronized {
   176             for (c <- string) {
   175             for (i <- 0 until n) {
   177               buffer.write(c.toByte)
   176               buffer.write(s.charAt(i).toByte)
   178               buffer_check()
   177               buffer_check()
   179             }
   178             }
   180           }
   179           }
   181         }
   180         }
   182       }
   181       }
       
   182     }
   183 
   183 
   184     def += (array: Array[Byte], offset: Int, length: Int): Unit = {
   184     def += (array: Array[Byte], offset: Int, length: Int): Unit = {
   185       if (offset < 0 || length < 0 || offset.toLong + length.toLong > array.length) {
   185       if (offset < 0 || length < 0 || offset.toLong + length.toLong > array.length) {
   186         throw new IndexOutOfBoundsException
   186         throw new IndexOutOfBoundsException
   187       }
   187       }