jdk/src/share/classes/com/sun/java/util/jar/pack/AdaptiveCoding.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 7192 445c518364c4
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.java.util.jar.pack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Adaptive coding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * See the section "Adaptive Encodings" in the Pack200 spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @author John Rose
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
class AdaptiveCoding implements Constants, CodingMethod {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    CodingMethod headCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    int          headLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    CodingMethod tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public AdaptiveCoding(int headLength, CodingMethod headCoding, CodingMethod tailCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        assert(isCodableLength(headLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        this.headLength = headLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        this.headCoding = headCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        this.tailCoding = tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public void setHeadCoding(CodingMethod headCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        this.headCoding = headCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public void setHeadLength(int headLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        assert(isCodableLength(headLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        this.headLength = headLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public void setTailCoding(CodingMethod tailCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        this.tailCoding = tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public boolean isTrivial() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        return headCoding == tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // CodingMethod methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public void writeArrayTo(OutputStream out, int[] a, int start, int end) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        writeArray(this, out, a, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // writeArrayTo must be coded iteratively, not recursively:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static void writeArray(AdaptiveCoding run, OutputStream out, int[] a, int start, int end) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            int mid = start+run.headLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            assert(mid <= end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            run.headCoding.writeArrayTo(out, a, start, mid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            start = mid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            if (run.tailCoding instanceof AdaptiveCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                run = (AdaptiveCoding) run.tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        run.tailCoding.writeArrayTo(out, a, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public void readArrayFrom(InputStream in, int[] a, int start, int end) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        readArray(this, in, a, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static void readArray(AdaptiveCoding run, InputStream in, int[] a, int start, int end) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            int mid = start+run.headLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            assert(mid <= end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            run.headCoding.readArrayFrom(in, a, start, mid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            start = mid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (run.tailCoding instanceof AdaptiveCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                run = (AdaptiveCoding) run.tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        run.tailCoding.readArrayFrom(in, a, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public static final int KX_MIN = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public static final int KX_MAX = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public static final int KX_LG2BASE = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public static final int KX_BASE = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public static final int KB_MIN = 0x00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public static final int KB_MAX = 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public static final int KB_OFFSET = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public static final int KB_DEFAULT = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    static int getKXOf(int K) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        for (int KX = KX_MIN; KX <= KX_MAX; KX++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (((K - KB_OFFSET) & ~KB_MAX) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                return KX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            K >>>= KX_LG2BASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static int getKBOf(int K) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        int KX = getKXOf(K);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (KX < 0)  return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        K >>>= (KX * KX_LG2BASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return K-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    static int decodeK(int KX, int KB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        assert(KX_MIN <= KX && KX <= KX_MAX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        assert(KB_MIN <= KB && KB <= KB_MAX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return (KB+KB_OFFSET) << (KX * KX_LG2BASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    static int getNextK(int K) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (K <= 0)  return 1;  // 1st K value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        int KX = getKXOf(K);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (KX < 0)  return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        // This is the increment we expect to apply:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        int unit = 1      << (KX * KX_LG2BASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        int mask = KB_MAX << (KX * KX_LG2BASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        int K1 = K + unit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        K1 &= ~(unit-1);  // cut off stray low-order bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (((K1 - unit) & ~mask) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            assert(getKXOf(K1) == KX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return K1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (KX == KX_MAX)  return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        KX += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        int unit2 = 1      << (KX * KX_LG2BASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        int mask2 = KB_MAX << (KX * KX_LG2BASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        K1 |= (mask & ~mask2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        K1 += unit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        assert(getKXOf(K1) == KX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return K1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    // Is K of the form ((KB:[0..255])+1) * 16^(KX:{0..3])?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public static boolean isCodableLength(int K) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        int KX = getKXOf(K);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (KX < 0)  return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        int unit = 1      << (KX * KX_LG2BASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        int mask = KB_MAX << (KX * KX_LG2BASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return ((K - unit) & ~mask) == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public byte[] getMetaCoding(Coding dflt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        //assert(!isTrivial()); // can happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // See the isCodableLength restriction in CodingChooser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        ByteArrayOutputStream bytes = new ByteArrayOutputStream(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            makeMetaCoding(this, dflt, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            throw new RuntimeException(ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return bytes.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    private static void makeMetaCoding(AdaptiveCoding run, Coding dflt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                       ByteArrayOutputStream bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                      throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            CodingMethod headCoding = run.headCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            int          headLength = run.headLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            CodingMethod tailCoding = run.tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            int K = headLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            assert(isCodableLength(K));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            int ADef   = (headCoding == dflt)?1:0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            int BDef   = (tailCoding == dflt)?1:0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (ADef+BDef > 1)  BDef = 0;  // arbitrary choice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            int ABDef  = 1*ADef + 2*BDef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            assert(ABDef < 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            int KX     = getKXOf(K);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            int KB     = getKBOf(K);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            assert(decodeK(KX, KB) == K);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            int KBFlag = (KB != KB_DEFAULT)?1:0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            bytes.write(_meta_run + KX + 4*KBFlag + 8*ABDef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if (KBFlag != 0)    bytes.write(KB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            if (ADef == 0)  bytes.write(headCoding.getMetaCoding(dflt));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (tailCoding instanceof AdaptiveCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                run = (AdaptiveCoding) tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                continue; // tail call, to avoid deep stack recursion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (BDef == 0)  bytes.write(tailCoding.getMetaCoding(dflt));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public static int parseMetaCoding(byte[] bytes, int pos, Coding dflt, CodingMethod res[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        int op = bytes[pos++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (op < _meta_run || op >= _meta_pop)  return pos-1; // backup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        AdaptiveCoding prevc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        for (boolean keepGoing = true; keepGoing; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            keepGoing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            assert(op >= _meta_run);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            op -= _meta_run;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            int KX = op % 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            int KBFlag = (op / 4) % 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            int ABDef = (op / 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            assert(ABDef < 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            int ADef = (ABDef & 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            int BDef = (ABDef & 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            CodingMethod[] ACode = {dflt}, BCode = {dflt};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            int KB = KB_DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if (KBFlag != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                KB = bytes[pos++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            if (ADef == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                pos = BandStructure.parseMetaCoding(bytes, pos, dflt, ACode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            if (BDef == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                ((op = bytes[pos] & 0xFF) >= _meta_run) && op < _meta_pop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                keepGoing = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            } else if (BDef == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                pos = BandStructure.parseMetaCoding(bytes, pos, dflt, BCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            AdaptiveCoding newc = new AdaptiveCoding(decodeK(KX, KB),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                                     ACode[0], BCode[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            if (prevc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                res[0] = newc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                prevc.tailCoding = newc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            prevc = newc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    private String keyString(CodingMethod m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (m instanceof Coding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return ((Coding)m).keyString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return m.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        StringBuffer res = new StringBuffer(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        AdaptiveCoding run = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        res.append("run(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            res.append(run.headLength).append("*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            res.append(keyString(run.headCoding));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (run.tailCoding instanceof AdaptiveCoding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                run = (AdaptiveCoding) run.tailCoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                res.append(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        res.append(" **").append(keyString(run.tailCoding));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        res.append(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return res.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public static void main(String av[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        int[][] samples = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            {1,2,3,4,5},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            {254,255,256,256+1*16,256+2*16},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            {0xfd,0xfe,0xff,0x100,0x110,0x120,0x130},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            {0xfd0,0xfe0,0xff0,0x1000,0x1100,0x1200,0x1300},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            {0xfd00,0xfe00,0xff00,0x10000,0x11000,0x12000,0x13000},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            {0xfd000,0xfe000,0xff000,0x100000}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        for (int i = 0; i < samples.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            for (int j = 0; j < samples[i].length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                int K = samples[i][j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                int KX = getKXOf(K);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                int KB = getKBOf(K);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                System.out.println("K="+Integer.toHexString(K)+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                   " KX="+KX+" KB="+KB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                assert(isCodableLength(K));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                assert(K == decodeK(KX, KB));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                if (j == 0)  continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                int K1 = samples[i][j-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                assert(K == getNextK(K1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
//*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
}