View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  /*
22   * Licensed under the Apache License, Version 2.0 (the "License");
23   * you may not use this file except in compliance with the License.
24   * You may obtain a copy of the License at
25   *
26   *     http://www.apache.org/licenses/LICENSE-2.0
27   *
28   * Unless required by applicable law or agreed to in writing, software
29   * distributed under the License is distributed on an "AS IS" BASIS,
30   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31   * See the License for the specific language governing permissions and
32   * limitations under the License.
33   */
34  package org.waarp.compress.zstdsafe;
35  
36  import static org.waarp.compress.zstdsafe.Constants.*;
37  import static org.waarp.compress.zstdsafe.UnsafeUtil.*;
38  import static org.waarp.compress.zstdsafe.Util.*;
39  
40  class BitOutputStream {
41    private static final int[] BIT_MASK = {
42        0x0, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF,
43        0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0x1FFFF, 0x3FFFF, 0x7FFFF, 0xFFFFF,
44        0x1FFFFF, 0x3FFFFF, 0x7FFFFF, 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF, 0x7FFFFFF,
45        0xFFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF
46    }; // up to 31 bits
47  
48    private final byte[] outputBase;
49    private final int outputAddress;
50    private final int outputLimit;
51  
52    private long container;
53    private int bitCount;
54    private int currentAddress;
55  
56    public BitOutputStream(final byte[] outputBase, final int outputAddress,
57                           final int outputSize) {
58      checkArgument(outputSize >= SIZE_OF_LONG, "Output buffer too small");
59  
60      this.outputBase = outputBase;
61      this.outputAddress = outputAddress;
62      outputLimit = this.outputAddress + outputSize - SIZE_OF_LONG;
63  
64      currentAddress = this.outputAddress;
65    }
66  
67    public void addBits(final int value, final int bits) {
68      container |= (long) (value & BIT_MASK[bits]) << bitCount;
69      bitCount += bits;
70    }
71  
72    /**
73     * Note: leading bits of value must be 0
74     */
75    public void addBitsFast(final int value, final int bits) {
76      container |= ((long) value) << bitCount;
77      bitCount += bits;
78    }
79  
80    public void flush() {
81      final int bytes = bitCount >>> 3;
82  
83      putLong(outputBase, currentAddress, container);
84      currentAddress += bytes;
85  
86      if (currentAddress > outputLimit) {
87        currentAddress = outputLimit;
88      }
89  
90      bitCount &= 7;
91      container >>>= bytes * 8L;
92    }
93  
94    public int close() {
95      addBitsFast(1, 1); // end mark
96      flush();
97  
98      if (currentAddress >= outputLimit) {
99        return 0;
100     }
101 
102     return (currentAddress - outputAddress) + (bitCount > 0? 1 : 0);
103   }
104 }