src/Pure/General/bytes.scala
changeset 80351 dbbe26afc319
parent 80350 96843eb96493
child 80352 7a6cba7c77c9
equal deleted inserted replaced
80350:96843eb96493 80351:dbbe26afc319
   110 
   110 
   111   def append(file: JFile, bytes: Bytes): Unit =
   111   def append(file: JFile, bytes: Bytes): Unit =
   112     using(new FileOutputStream(file, true))(bytes.write_stream(_))
   112     using(new FileOutputStream(file, true))(bytes.write_stream(_))
   113 
   113 
   114   def append(path: Path, bytes: Bytes): Unit = append(path.file, bytes)
   114   def append(path: Path, bytes: Bytes): Unit = append(path.file, bytes)
       
   115 
       
   116 
       
   117   /* vector of unsigned integers */
       
   118 
       
   119   trait Vec {
       
   120     def size: Long
       
   121     def apply(i: Long): Int
       
   122   }
       
   123 
       
   124   class Vec_String(string: String) extends Vec {
       
   125     override def size: Long = string.length.toLong
       
   126     override def apply(i: Long): Int =
       
   127       if (0 <= i && i < size) string(i.toInt).toInt
       
   128       else throw new IndexOutOfBoundsException
       
   129   }
   115 }
   130 }
   116 
   131 
   117 final class Bytes private(
   132 final class Bytes private(
   118   protected val bytes: Array[Byte],
   133   protected val bytes: Array[Byte],
   119   protected val offset: Int,
   134   protected val offset: Int,
   120   val length: Int) extends CharSequence {
   135   val length: Int) extends Bytes.Vec with CharSequence {
   121   /* equality */
   136   /* equality */
   122 
   137 
   123   override def equals(that: Any): Boolean = {
   138   override def equals(that: Any): Boolean = {
   124     that match {
   139     that match {
   125       case other: Bytes =>
   140       case other: Bytes =>
   193       System.arraycopy(other.bytes, other.offset, new_bytes, length, other.length)
   208       System.arraycopy(other.bytes, other.offset, new_bytes, length, other.length)
   194       new Bytes(new_bytes, 0, new_bytes.length)
   209       new Bytes(new_bytes, 0, new_bytes.length)
   195     }
   210     }
   196 
   211 
   197 
   212 
       
   213   /* Vec operations */
       
   214 
       
   215   def size: Long = length.toLong
       
   216 
       
   217   def apply(i: Long): Int =
       
   218     if (0 <= i && i < size) bytes((offset + i).toInt).asInstanceOf[Int] & 0xFF
       
   219     else throw new IndexOutOfBoundsException
       
   220 
       
   221 
   198   /* CharSequence operations */
   222   /* CharSequence operations */
   199 
   223 
   200   def charAt(i: Int): Char =
   224   def charAt(i: Int): Char = apply(i).asInstanceOf[Char]
   201     if (0 <= i && i < length) (bytes(offset + i).asInstanceOf[Int] & 0xFF).asInstanceOf[Char]
       
   202     else throw new IndexOutOfBoundsException
       
   203 
   225 
   204   def subSequence(i: Int, j: Int): Bytes = {
   226   def subSequence(i: Int, j: Int): Bytes = {
   205     if (0 <= i && i <= j && j <= length) new Bytes(bytes, offset + i, j - i)
   227     if (0 <= i && i <= j && j <= length) new Bytes(bytes, offset + i, j - i)
   206     else throw new IndexOutOfBoundsException
   228     else throw new IndexOutOfBoundsException
   207   }
   229   }