src/Pure/General/bytes.scala
author wenzelm
Mon, 03 Oct 2016 12:28:36 +0200
changeset 64004 b4ece7a3f2ca
parent 64001 7ecb22be8f03
child 64005 f6e965cf1617
permissions -rw-r--r--
clarified stream operations; added XZ data compression;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
54439
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/bytes.scala
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
     2
    Module:     PIDE
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
     4
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
     5
Immutable byte vectors versus UTF8 strings.
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
     6
*/
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
     7
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
     8
package isabelle
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
     9
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    10
64004
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
    11
import java.io.{File => JFile, ByteArrayOutputStream, ByteArrayInputStream,
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
    12
  OutputStream, InputStream, FileInputStream}
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
    13
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
    14
import org.tukaani.xz.{XZInputStream, XZOutputStream}
54440
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    15
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    16
54439
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    17
object Bytes
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    18
{
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    19
  val empty: Bytes = new Bytes(Array[Byte](), 0, 0)
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    20
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    21
  def apply(s: CharSequence): Bytes =
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    22
  {
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    23
    val str = s.toString
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    24
    if (str.isEmpty) empty
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    25
    else {
62527
aae9a2a855e0 tuned signature;
wenzelm
parents: 60833
diff changeset
    26
      val b = UTF8.bytes(str)
54442
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
    27
      new Bytes(b, 0, b.length)
54439
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    28
    }
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    29
  }
54440
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    30
63779
9da65bc75610 more operations;
wenzelm
parents: 62527
diff changeset
    31
  def apply(a: Array[Byte]): Bytes = apply(a, 0, a.length)
9da65bc75610 more operations;
wenzelm
parents: 62527
diff changeset
    32
54442
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
    33
  def apply(a: Array[Byte], offset: Int, length: Int): Bytes =
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
    34
    if (length == 0) empty
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
    35
    else {
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
    36
      val b = new Array[Byte](length)
55618
995162143ef4 tuned imports;
wenzelm
parents: 54512
diff changeset
    37
      System.arraycopy(a, offset, b, 0, length)
54442
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
    38
      new Bytes(b, 0, b.length)
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
    39
    }
c39972ddd672 more specific Protocol_Output: empty message.body, main content via bytes/text;
wenzelm
parents: 54440
diff changeset
    40
54440
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    41
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    42
  /* read */
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    43
64004
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
    44
  def read_stream(stream: InputStream, limit: Int = Integer.MAX_VALUE): Bytes =
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
    45
    if (limit == 0) empty
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
    46
    else {
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
    47
      val out = new ByteArrayOutputStream(if (limit == Integer.MAX_VALUE) 1024 else limit)
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
    48
      val buf = new Array[Byte](1024)
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
    49
      var m = 0
54440
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    50
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    51
      do {
64004
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
    52
        m = stream.read(buf, 0, buf.size min (limit - out.size))
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
    53
        if (m != -1) out.write(buf, 0, m)
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
    54
      } while (m != -1 && limit > out.size)
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
    55
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
    56
      new Bytes(out.toByteArray, 0, out.size)
54440
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    57
    }
64001
7ecb22be8f03 more general read_stream: return actual byte count;
wenzelm
parents: 63779
diff changeset
    58
7ecb22be8f03 more general read_stream: return actual byte count;
wenzelm
parents: 63779
diff changeset
    59
  def read(file: JFile): Bytes =
64004
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
    60
    using(new FileInputStream(file))(read_stream(_, file.length.toInt))
54439
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    61
}
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    62
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    63
final class Bytes private(
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    64
  protected val bytes: Array[Byte],
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    65
  protected val offset: Int,
60833
d201996f72a8 provide CharSequence operations as well;
wenzelm
parents: 55618
diff changeset
    66
  val length: Int) extends CharSequence
54439
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    67
{
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    68
  /* equality */
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    69
54440
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    70
  override def equals(that: Any): Boolean =
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    71
  {
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    72
    that match {
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    73
      case other: Bytes =>
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    74
        if (this eq other) true
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    75
        else if (length != other.length) false
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    76
        else (0 until length).forall(i => bytes(offset + i) == other.bytes(other.offset + i))
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    77
      case _ => false
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    78
    }
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    79
  }
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    80
54439
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    81
  private lazy val hash: Int =
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    82
  {
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    83
    var h = 0
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    84
    for (i <- offset until offset + length) {
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    85
      val b = bytes(i).asInstanceOf[Int] & 0xFF
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    86
      h = 31 * h + b
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    87
    }
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    88
    h
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    89
  }
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    90
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    91
  override def hashCode(): Int = hash
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    92
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    93
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    94
  /* content */
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
    95
54512
7a92ed889da4 persistent value;
wenzelm
parents: 54444
diff changeset
    96
  lazy val sha1_digest: SHA1.Digest = SHA1.digest(bytes)
54440
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
    97
54444
a2290f36d1d6 prefer UTF8.decode_permissive;
wenzelm
parents: 54442
diff changeset
    98
  override def toString: String =
a2290f36d1d6 prefer UTF8.decode_permissive;
wenzelm
parents: 54442
diff changeset
    99
    UTF8.decode_chars(s => s, bytes, offset, offset + length).toString
54439
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
   100
54440
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
   101
  def isEmpty: Boolean = length == 0
54439
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
   102
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
   103
  def +(other: Bytes): Bytes =
54440
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
   104
    if (other.isEmpty) this
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
   105
    else if (isEmpty) other
54439
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
   106
    else {
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
   107
      val new_bytes = new Array[Byte](length + other.length)
55618
995162143ef4 tuned imports;
wenzelm
parents: 54512
diff changeset
   108
      System.arraycopy(bytes, offset, new_bytes, 0, length)
995162143ef4 tuned imports;
wenzelm
parents: 54512
diff changeset
   109
      System.arraycopy(other.bytes, other.offset, new_bytes, length, other.length)
54439
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
   110
      new Bytes(new_bytes, 0, new_bytes.length)
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
   111
    }
54440
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
   112
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
   113
60833
d201996f72a8 provide CharSequence operations as well;
wenzelm
parents: 55618
diff changeset
   114
  /* CharSequence operations */
d201996f72a8 provide CharSequence operations as well;
wenzelm
parents: 55618
diff changeset
   115
d201996f72a8 provide CharSequence operations as well;
wenzelm
parents: 55618
diff changeset
   116
  def charAt(i: Int): Char =
d201996f72a8 provide CharSequence operations as well;
wenzelm
parents: 55618
diff changeset
   117
    if (0 <= i && i < length) (bytes(offset + i).asInstanceOf[Int] & 0xFF).asInstanceOf[Char]
d201996f72a8 provide CharSequence operations as well;
wenzelm
parents: 55618
diff changeset
   118
    else throw new IndexOutOfBoundsException
d201996f72a8 provide CharSequence operations as well;
wenzelm
parents: 55618
diff changeset
   119
d201996f72a8 provide CharSequence operations as well;
wenzelm
parents: 55618
diff changeset
   120
  def subSequence(i: Int, j: Int): Bytes =
d201996f72a8 provide CharSequence operations as well;
wenzelm
parents: 55618
diff changeset
   121
  {
d201996f72a8 provide CharSequence operations as well;
wenzelm
parents: 55618
diff changeset
   122
    if (0 <= i && i <= j && j <= length) new Bytes(bytes, offset + i, j - i)
d201996f72a8 provide CharSequence operations as well;
wenzelm
parents: 55618
diff changeset
   123
    else throw new IndexOutOfBoundsException
d201996f72a8 provide CharSequence operations as well;
wenzelm
parents: 55618
diff changeset
   124
  }
d201996f72a8 provide CharSequence operations as well;
wenzelm
parents: 55618
diff changeset
   125
d201996f72a8 provide CharSequence operations as well;
wenzelm
parents: 55618
diff changeset
   126
64004
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
   127
  /* streams */
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
   128
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
   129
  def stream(): ByteArrayInputStream = new ByteArrayInputStream(bytes, offset, length)
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
   130
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
   131
  def write_stream(stream: OutputStream): Unit = stream.write(bytes, offset, length)
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
   132
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
   133
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
   134
  /* XZ data compression */
54440
2c4940d2edf7 tuned signature;
wenzelm
parents: 54439
diff changeset
   135
64004
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
   136
  def uncompress(): Bytes = using(new XZInputStream(stream()))(Bytes.read_stream(_))
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
   137
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
   138
  def compress(options: XZ.Options = XZ.options()): Bytes =
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
   139
  {
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
   140
    val result = new ByteArrayOutputStream(length)
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
   141
    using(new XZOutputStream(result, options))(write_stream(_))
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
   142
    new Bytes(result.toByteArray, 0, result.size)
b4ece7a3f2ca clarified stream operations;
wenzelm
parents: 64001
diff changeset
   143
  }
54439
621a155c7715 immutable byte vectors versus UTF8 strings;
wenzelm
parents:
diff changeset
   144
}