01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.harmony.pack200;
18:
19: import java.io.IOException;
20: import java.io.InputStream;
21:
22: /**
23: * A run codec is a grouping of two nested codecs; K values are decoded from
24: * the first codec, and the remaining codes are decoded from the remaining
25: * codec. Note that since this codec maintains state, the instances are
26: * not reusable.
27: */
28: public class RunCodec extends Codec {
29: private int k;
30: private Codec aCodec;
31: private Codec bCodec;
32: private long last;
33:
34: public RunCodec(int k, Codec aCodec, Codec bCodec)
35: throws Pack200Exception {
36: if (k <= 0)
37: throw new Pack200Exception(
38: "Cannot have a RunCodec for a negative number of numbers");
39: if (aCodec == null || bCodec == null)
40: throw new Pack200Exception(
41: "Must supply both codecs for a RunCodec");
42: this .k = k;
43: this .aCodec = aCodec;
44: this .bCodec = bCodec;
45: }
46:
47: public long decode(InputStream in) throws IOException,
48: Pack200Exception {
49: return decode(in, this .last);
50: }
51:
52: public long decode(InputStream in, long last) throws IOException,
53: Pack200Exception {
54: if (--k >= 0) {
55: long value = aCodec.decode(in, last);
56: this .last = (k == 0 ? 0 : value);
57: return value;
58: } else {
59: this .last = bCodec.decode(in, last);
60: return this .last;
61: }
62: }
63:
64: public String toString() {
65: return "RunCodec[k=" + k + ";aCodec=" + aCodec + "bCodec="
66: + bCodec + "]";
67: }
68: }
|