1 /* 2 * RangeEncoderToBuffer 3 * 4 * Authors: Lasse Collin <lasse.collin@tukaani.org> 5 * Igor Pavlov <http://7-zip.org/> 6 * 7 * This file has been put into the public domain. 8 * You can do whatever you want with this file. 9 */ 10 11 package org.tukaani.xz.rangecoder; 12 13 import java.io.OutputStream; 14 import java.io.IOException; 15 import org.tukaani.xz.ArrayCache; 16 17 public final class RangeEncoderToBuffer extends RangeEncoder { 18 private final byte[] buf; 19 private int bufPos; 20 RangeEncoderToBuffer(int bufSize, ArrayCache arrayCache)21 public RangeEncoderToBuffer(int bufSize, ArrayCache arrayCache) { 22 buf = arrayCache.getByteArray(bufSize, false); 23 reset(); 24 } 25 putArraysToCache(ArrayCache arrayCache)26 public void putArraysToCache(ArrayCache arrayCache) { 27 arrayCache.putArray(buf); 28 } 29 reset()30 public void reset() { 31 super.reset(); 32 bufPos = 0; 33 } 34 getPendingSize()35 public int getPendingSize() { 36 // With LZMA2 it is known that cacheSize fits into an int. 37 return bufPos + (int)cacheSize + 5 - 1; 38 } 39 finish()40 public int finish() { 41 // super.finish() cannot throw an IOException because writeByte() 42 // provided in this file cannot throw an IOException. 43 try { 44 super.finish(); 45 } catch (IOException e) { 46 throw new Error(); 47 } 48 49 return bufPos; 50 } 51 write(OutputStream out)52 public void write(OutputStream out) throws IOException { 53 out.write(buf, 0, bufPos); 54 } 55 writeByte(int b)56 void writeByte(int b) { 57 buf[bufPos++] = (byte)b; 58 } 59 } 60