src/Pure/General/bytes.scala
changeset 64001 7ecb22be8f03
parent 63779 9da65bc75610
child 64004 b4ece7a3f2ca
equal deleted inserted replaced
64000:445b3deced8f 64001:7ecb22be8f03
     6 */
     6 */
     7 
     7 
     8 package isabelle
     8 package isabelle
     9 
     9 
    10 
    10 
    11 import java.io.{File => JFile, OutputStream, FileInputStream}
    11 import java.io.{File => JFile, OutputStream, InputStream, FileInputStream}
    12 
    12 
    13 
    13 
    14 object Bytes
    14 object Bytes
    15 {
    15 {
    16   val empty: Bytes = new Bytes(Array[Byte](), 0, 0)
    16   val empty: Bytes = new Bytes(Array[Byte](), 0, 0)
    36     }
    36     }
    37 
    37 
    38 
    38 
    39   /* read */
    39   /* read */
    40 
    40 
    41   def read(file: JFile): Bytes =
    41   def read_stream(stream: InputStream, max_length: Int): Bytes =
    42   {
    42   {
       
    43     val bytes = new Array[Byte](max_length)
    43     var i = 0
    44     var i = 0
    44     var m = 0
    45     var m = 0
    45     val n = file.length.toInt
       
    46     val bytes = new Array[Byte](n)
       
    47 
    46 
    48     val stream = new FileInputStream(file)
       
    49     try {
    47     try {
    50       do {
    48       do {
    51         m = stream.read(bytes, i, n - i)
    49         m = stream.read(bytes, i, max_length - i)
    52         if (m != -1) i += m
    50         if (m != -1) i += m
    53       } while (m != -1 && n > i)
    51       } while (m != -1 && max_length > i)
    54     }
    52     }
    55     finally { stream.close }
    53     finally { stream.close }
    56 
    54 
    57     new Bytes(bytes, 0, bytes.length)
    55     new Bytes(bytes, 0, i)
    58   }
    56   }
       
    57 
       
    58   def read(file: JFile): Bytes =
       
    59     read_stream(new FileInputStream(file), file.length.toInt)
    59 }
    60 }
    60 
    61 
    61 final class Bytes private(
    62 final class Bytes private(
    62   protected val bytes: Array[Byte],
    63   protected val bytes: Array[Byte],
    63   protected val offset: Int,
    64   protected val offset: Int,