src/Pure/General/bytes.scala
changeset 80509 2a9abd6a164e
parent 80508 8585399f26f6
child 80511 11ca26978dd4
equal deleted inserted replaced
80508:8585399f26f6 80509:2a9abd6a164e
    30   private val chunk_size: Long = Space.MiB(100).bytes
    30   private val chunk_size: Long = Space.MiB(100).bytes
    31 
    31 
    32   private def make_size(chunks: Array[Array[Byte]], buffer: Array[Byte]): Long =
    32   private def make_size(chunks: Array[Array[Byte]], buffer: Array[Byte]): Long =
    33     chunks.foldLeft(buffer.length.toLong)((n, chunk) => n + chunk.length)
    33     chunks.foldLeft(buffer.length.toLong)((n, chunk) => n + chunk.length)
    34 
    34 
    35   class Too_Large(size: Long) extends IndexOutOfBoundsException {
    35   class Too_Large(size: Long, limit: Long) extends IndexOutOfBoundsException {
    36     override def getMessage: String =
    36     override def getMessage: String =
    37       "Bytes too large for particular operation: " +
    37       "Bytes too large for particular operation: " +
    38         Space.bytes(size).print + " > " + Space.bytes(array_size).print
    38         Space.bytes(size).print + " > " + Space.bytes(limit).print
    39   }
    39   }
    40 
    40 
    41 
    41 
    42   /* main constructors */
    42   /* main constructors */
    43 
    43 
   292     (chunks.isEmpty ||
   292     (chunks.isEmpty ||
   293       chunks.get.nonEmpty &&
   293       chunks.get.nonEmpty &&
   294       chunks.get.forall(chunk => chunk.length == Bytes.chunk_size)) &&
   294       chunks.get.forall(chunk => chunk.length == Bytes.chunk_size)) &&
   295     chunk0.length < Bytes.chunk_size)
   295     chunk0.length < Bytes.chunk_size)
   296 
   296 
   297   def small_size: Int =
       
   298     if (size > Bytes.array_size) throw new Bytes.Too_Large(size)
       
   299     else size.toInt
       
   300 
       
   301   override def is_empty: Boolean = size == 0
   297   override def is_empty: Boolean = size == 0
   302 
   298 
   303   def proper: Option[Bytes] = if (is_empty) None else Some(bytes)
   299   def proper: Option[Bytes] = if (is_empty) None else Some(bytes)
   304 
   300 
   305   def is_sliced: Boolean =
   301   def is_sliced: Boolean =
   477         }
   473         }
   478       }
   474       }
   479     }
   475     }
   480 
   476 
   481   def make_array: Array[Byte] = {
   477   def make_array: Array[Byte] = {
   482     val buf = new ByteArrayOutputStream(small_size)
   478     val n =
       
   479       if (size <= Bytes.array_size) size.toInt
       
   480       else throw new Bytes.Too_Large(size, Bytes.array_size)
       
   481     val buf = new ByteArrayOutputStream(n)
   483     for (a <- subarray_iterator) { buf.write(a.array, a.offset, a.length) }
   482     for (a <- subarray_iterator) { buf.write(a.array, a.offset, a.length) }
   484     buf.toByteArray
   483     buf.toByteArray
   485   }
   484   }
   486 
   485 
   487   def text: String =
   486   def text: String =