src/Pure/General/bytes.scala
changeset 68094 0b66aca9c965
parent 68087 dac267cd51fe
child 68106 a514e29db980
equal deleted inserted replaced
68093:b98c5877b0f3 68094:0b66aca9c965
     8 
     8 
     9 
     9 
    10 import java.io.{File => JFile, ByteArrayOutputStream, ByteArrayInputStream,
    10 import java.io.{File => JFile, ByteArrayOutputStream, ByteArrayInputStream,
    11   OutputStream, InputStream, FileInputStream, FileOutputStream}
    11   OutputStream, InputStream, FileInputStream, FileOutputStream}
    12 import java.net.URL
    12 import java.net.URL
       
    13 import java.util.Base64
    13 
    14 
    14 import org.tukaani.xz.{XZInputStream, XZOutputStream}
    15 import org.tukaani.xz.{XZInputStream, XZOutputStream}
    15 
    16 
    16 
    17 
    17 object Bytes
    18 object Bytes
   149   lazy val sha1_digest: SHA1.Digest = SHA1.digest(bytes)
   150   lazy val sha1_digest: SHA1.Digest = SHA1.digest(bytes)
   150 
   151 
   151   def text: String =
   152   def text: String =
   152     UTF8.decode_chars(s => s, bytes, offset, offset + length).toString
   153     UTF8.decode_chars(s => s, bytes, offset, offset + length).toString
   153 
   154 
       
   155   def base64: String =
       
   156   {
       
   157     val b =
       
   158       if (offset == 0 && length == bytes.length) bytes
       
   159       else Bytes(bytes, offset, length).bytes
       
   160     Base64.getEncoder.encodeToString(b)
       
   161   }
       
   162 
   154   override def toString: String =
   163   override def toString: String =
   155   {
   164   {
   156     val str = text
   165     val str = text
   157     if (str.contains('\uFFFD')) "Bytes(" + length + ")" else str
   166     if (str.contains('\uFFFD')) "Bytes(" + length + ")" else str
   158   }
   167   }