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.zstdunsafe;
35  
36  class BitOutputStream {
37    private static final long[] BIT_MASK = {
38        0x0, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF,
39        0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0x1FFFF, 0x3FFFF, 0x7FFFF, 0xFFFFF,
40        0x1FFFFF, 0x3FFFFF, 0x7FFFFF, 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF, 0x7FFFFFF,
41        0xFFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF
42    }; // up to 31 bits
43  
44    private final Object outputBase;
45    private final long outputAddress;
46    private final long outputLimit;
47  
48    private long container;
49    private int bitCount;
50    private long currentAddress;
51  
52    public BitOutputStream(final Object outputBase, final long outputAddress,
53                           final int outputSize) {
54      Util.checkArgument(outputSize >= Constants.SIZE_OF_LONG,
55                         "Output buffer too small");
56  
57      this.outputBase = outputBase;
58      this.outputAddress = outputAddress;
59      outputLimit = this.outputAddress + outputSize - Constants.SIZE_OF_LONG;
60  
61      currentAddress = this.outputAddress;
62    }
63  
64    public void addBits(final int value, final int bits) {
65      container |= (value & BIT_MASK[bits]) << bitCount;
66      bitCount += bits;
67    }
68  
69    /**
70     * Note: leading bits of value must be 0
71     */
72    public void addBitsFast(final int value, final int bits) {
73      container |= ((long) value) << bitCount;
74      bitCount += bits;
75    }
76  
77    public void flush() {
78      final int bytes = bitCount >>> 3;
79  
80      UnsafeUtil.UNSAFE.putLong(outputBase, currentAddress, container);
81      currentAddress += bytes;
82  
83      if (currentAddress > outputLimit) {
84        currentAddress = outputLimit;
85      }
86  
87      bitCount &= 7;
88      container >>>= bytes * 8L;
89    }
90  
91    public int close() {
92      addBitsFast(1, 1); // end mark
93      flush();
94  
95      if (currentAddress >= outputLimit) {
96        return 0;
97      }
98  
99      return (int) ((currentAddress - outputAddress) + (bitCount > 0? 1 : 0));
100   }
101 }