src/Pure/General/bytes.scala
changeset 78243 0e221a8128e4
parent 78194 da721ba809a4
child 78855 6fdcd6c8c97a
equal deleted inserted replaced
78242:633ae08625d1 78243:0e221a8128e4
    50   }
    50   }
    51 
    51 
    52 
    52 
    53   /* read */
    53   /* read */
    54 
    54 
    55   def read_stream(stream: InputStream, limit: Int = Integer.MAX_VALUE, hint: Int = 1024): Bytes =
    55   def read_stream(stream: InputStream, limit: Int = Int.MaxValue, hint: Int = 1024): Bytes =
    56     if (limit == 0) empty
    56     if (limit == 0) empty
    57     else {
    57     else {
    58       val out_size = (if (limit == Integer.MAX_VALUE) hint else limit) max 1024
    58       val out_size = (if (limit == Int.MaxValue) hint else limit) max 1024
    59       val out = new ByteArrayOutputStream(out_size)
    59       val out = new ByteArrayOutputStream(out_size)
    60       val buf = new Array[Byte](8192)
    60       val buf = new Array[Byte](8192)
    61       var m = 0
    61       var m = 0
    62 
    62 
    63       while ({
    63       while ({
    72   def read_url(name: String): Bytes = using(Url(name).openStream)(read_stream(_))
    72   def read_url(name: String): Bytes = using(Url(name).openStream)(read_stream(_))
    73 
    73 
    74   def read_file(file: JFile, offset: Long = 0L, limit: Long = Long.MaxValue): Bytes = {
    74   def read_file(file: JFile, offset: Long = 0L, limit: Long = Long.MaxValue): Bytes = {
    75     val start = offset.max(0L)
    75     val start = offset.max(0L)
    76     val len = (file.length - start).max(0L).min(limit)
    76     val len = (file.length - start).max(0L).min(limit)
    77     if (len > Integer.MAX_VALUE) error("Cannot read large file slice: " + Space.bytes(len).print)
    77     if (len > Int.MaxValue) error("Cannot read large file slice: " + Space.bytes(len).print)
    78     else if (len == 0L) empty
    78     else if (len == 0L) empty
    79     else {
    79     else {
    80       using(FileChannel.open(file.toPath, StandardOpenOption.READ)) { java_path =>
    80       using(FileChannel.open(file.toPath, StandardOpenOption.READ)) { java_path =>
    81         java_path.position(start)
    81         java_path.position(start)
    82         val n = len.toInt
    82         val n = len.toInt
   244     using(new xz.XZInputStream(stream(), cache.for_xz))(Bytes.read_stream(_, hint = length))
   244     using(new xz.XZInputStream(stream(), cache.for_xz))(Bytes.read_stream(_, hint = length))
   245 
   245 
   246   def uncompress_zstd(cache: Compress.Cache = Compress.Cache.none): Bytes = {
   246   def uncompress_zstd(cache: Compress.Cache = Compress.Cache.none): Bytes = {
   247     Zstd.init()
   247     Zstd.init()
   248     val n = zstd.Zstd.decompressedSize(bytes, offset, length)
   248     val n = zstd.Zstd.decompressedSize(bytes, offset, length)
   249     if (n > 0 && n < Integer.MAX_VALUE) {
   249     if (n > 0 && n < Int.MaxValue) {
   250       Bytes(zstd.Zstd.decompress(array, n.toInt))
   250       Bytes(zstd.Zstd.decompress(array, n.toInt))
   251     }
   251     }
   252     else {
   252     else {
   253       using(new zstd.ZstdInputStream(stream(), cache.for_zstd))(Bytes.read_stream(_, hint = length))
   253       using(new zstd.ZstdInputStream(stream(), cache.for_zstd))(Bytes.read_stream(_, hint = length))
   254     }
   254     }