1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
//start of BitOutputStream.java
//TEXT_STYLE:CODE=Shift_JIS(Japanese):RET_CODE=CRLF
/**
* BitOutputStream.java
*
* Copyright (C) 2001-2002 Michel Ishizuka All rights reserved.
*
* ȉ̏ɓӂȂ\[XƃoCi`̍ĔzzƎgp
* ύX̗Lɂ炸B
*
* PD\[XR[h̍ĔzzɂĒ쌠\ ̏̃Xg
* щL̐ێȂĂ͂ȂȂB
*
* QDoCi`̍ĔzzɂĒ쌠\ ̏̃Xg
* щL̐gp ̑̔zz
* ܂ގɋLqȂȂȂB
*
* ̃\tgEFA͐Β˔ڂɂĖۏŒA̖
* IBłƂۏAilLƂۏɂƂǂ܂炸A
* Ȃ閾IшÎIȕۏȂB
* Β˔ڂ ̃\tgEFA̎gpɂ钼ړIAԐړIA
* IAȁAT^IȁA邢͕KRIȑQ(gpɂf[^
* AƖ̒f〈܂Ăv̈⎸Ai
* T[rX̓l邪AĂꂾɌ肳Ȃ
* Q)ɑāAȂ鎖Ԃ̌ƂȂƂĂA_̐
* C△ߎӔC܂ ȂӔC낤ƂAƂꂪs
* ŝׂ߂łƂĂA܂͂̂悤ȑQ̉\
* ĂƂĂ̐ӔCȂ̂ƂB
*/
package jp.gr.java_conf.dangan.io;
//import classes and interfaces
import java.io.OutputStream;
//import exceptions
import java.io.IOException;
import java.lang.NullPointerException;
import java.lang.IllegalArgumentException;
/**
* ڑꂽo̓Xg[Ƀrbgf[^o͂邽߂
* o̓Xg[NXB<br>
*
* <pre>
* -- revision history --
* $Log: BitOutputStream.java,v $
* Revision 1.1 2002/12/05 00:00:00 dangan
* [maintenance]
* \[X
*
* Revision 1.0 2002/09/11 00:00:00 dangan
* add to version control
* [change]
* close() writen\bh flush()
* O𓊂悤ɏC
* [maintenance]
* ^up~
* CZX̏C
*
* </pre>
*
* @author $Author: dangan $
* @version $Revision: 1.1 $
*/
public class BitOutputStream extends OutputStream{
//------------------------------------------------------------------
// class field
//------------------------------------------------------------------
// default
//------------------------------------------------------------------
// private static final int DefaultCacheSize
//------------------------------------------------------------------
/**
* ftHgLbVTCY
*/
private static final int DefaultCacheSize = 1024;
//------------------------------------------------------------------
// instance field
//------------------------------------------------------------------
// sink
//------------------------------------------------------------------
// private OutputStream out
//------------------------------------------------------------------
/**
* ڑꂽo̓Xg[
*/
private OutputStream out;
//------------------------------------------------------------------
// instance field
//------------------------------------------------------------------
// cache
//------------------------------------------------------------------
// private byte[] cache
// private int cachePosition
//------------------------------------------------------------------
/**
* xቺ}~poCgz
*/
private byte[] cache;
/**
* cacheBuffer ̌ݏʒu
*/
private int cachePosition;
//------------------------------------------------------------------
// instance field
//------------------------------------------------------------------
// bit buffer
//------------------------------------------------------------------
// private int bitBuffer
// private int bitCount
//------------------------------------------------------------------
/**
* rbgobt@
*/
private int bitBuffer;
/**
* bitBuffer Lrbg
*/
private int bitCount;
//------------------------------------------------------------------
// constructer
//------------------------------------------------------------------
// private BitOutputStream()
// public BitOutputStream( OutputStream out )
// public BitOutputStream( OutputStream out, int CacheSize )
//------------------------------------------------------------------
/**
* ftHgRXgN^B
* gpsB
*/
private BitOutputStream(){ }
/**
* o̓Xg[ out f[^rbgPʂ
* ߂悤ȃXg[\zB<br>
* LbVTCYɂ̓ftHglgpB
*
* @param out o̓Xg[
*/
public BitOutputStream( OutputStream out ){
this( out, BitOutputStream.DefaultCacheSize );
}
/**
* o̓Xg[ out f[^rbgPʂ
* ߂悤ȃXg[\zB<br>
*
* @param out o̓Xg[
* @param CacheSize LbVTCY
*
* @exception IllegalArgumentException
* CacheSize 4̏ꍇA܂
* CacheSize 4̔{ŖꍇB
*/
public BitOutputStream( OutputStream out, int CacheSize ){
if( out != null && 4 <= CacheSize && 0 == ( CacheSize & 0x03 ) ){
this.out = out;
this.cache = new byte[ CacheSize ];
this.cachePosition = 0;
this.bitBuffer = 0;
this.bitCount = 0;
}else if( out == null ){
throw new NullPointerException( "out" );
}else if( CacheSize < 4 ){
throw new IllegalArgumentException( "CacheSize must be 4 or more." );
}else{
throw new IllegalArgumentException( "CacheSize must be multiple of 4." );
}
}
//------------------------------------------------------------------
// method of java.io.OutputStream
//------------------------------------------------------------------
// write
//------------------------------------------------------------------
// public void write( int data )
// public void write( byte[] buffer )
// public void write( byte[] buffer, int index, int length )
//------------------------------------------------------------------
/**
* ڑꂽo̓Xg[ 8rbg̃f[^o͂B<br>
*
* @param data 8rbg̃f[^B<br>
* 24rbg͖B<br>
*
* @exception IOException o̓G[ꍇ
*/
public void write( int data ) throws IOException {
this.writeBits( 8, data );
}
/**
* ڑꂽo̓Xg[buffer̓eA
* 8rbg̃f[^Ƃďo͂B<br>
*
* @param buffer oׂ͂f[^i[oCgz<br>
*
* @exception IOException o̓G[ꍇ
*/
public void write( byte[] buffer ) throws IOException {
this.write( buffer, 0, buffer.length ); //throws IOException
}
/**
* ڑꂽo̓Xg[bufferindex
* lengthoCg̓eA 8rbg̃f[^
* Ƃďo͂B<br>
*
* @param buffer oׂ͂f[^i[oCgz
* @param index buffer̃f[^Jnʒu
* @param length o͂f[^(oCg)
*
* @exception IOException o̓G[ꍇ
*/
public void write( byte[] buffer, int index, int length )
throws IOException {
if( this.bitCount % 8 == 0 ){
this.flush(); //throws IOException
this.out.write( buffer, index, length ); //throws IOException
}else{
while( 0 < length-- )
this.writeBits( 8, buffer[index++] ); //throws IOException
}
}
//------------------------------------------------------------------
// method of java.io.OutputStream
//------------------------------------------------------------------
// other
//------------------------------------------------------------------
// public void flush()
// public void close()
//------------------------------------------------------------------
/**
* ̃rbgo̓Xg[Ƀobt@OĂ
* 8rbgPʂ̃f[^Sďo͐ɏo͂B
* 8rbgɖȂf[^͏o͂ȂƂɒӁB<br>
*
* @exception IOException o̓G[ꍇ
*/
public void flush() throws IOException {
while( 8 <= this.bitCount ){
this.cache[ this.cachePosition++ ] = (byte)( this.bitBuffer >> 24 );
this.bitBuffer <<= 8;
this.bitCount -= 8;
}
this.out.write( this.cache, 0, this.cachePosition ); //throws IOException
this.cachePosition = 0;
this.out.flush(); //throws IOException
}
/**
* ̏o̓Xg[ƁAڑꂽo̓Xg[A
* gpĂ\[XJB<br>
*
* @exception IOException o̓G[ꍇ
*/
public void close() throws IOException {
while( 0 < this.bitCount ){
this.cache[ this.cachePosition++ ] = (byte)( this.bitBuffer >> 24 );
this.bitBuffer <<= 8;
this.bitCount -= 8;
}
this.out.write( this.cache, 0, this.cachePosition ); //throws IOException
this.cachePosition = 0;
this.out.flush(); //throws IOException
this.out.close(); //throws IOException
this.out = null;
this.cache = null;
this.cachePosition = 0;
this.bitCount = 128;
this.bitBuffer = 0;
}
//------------------------------------------------------------------
// original method
//------------------------------------------------------------------
// bit write
//------------------------------------------------------------------
// public void writeBit( int data )
// public void writeBoolean( boolean bool )
// public void writeBits( int count, int data )
//------------------------------------------------------------------
/**
* ڑꂽo̓Xg[1rbg̃f[^o͂B<br>
*
* @param data 1rbg̃f[^B<br>
* 31rbg͖B<br>
*
* @exception IOException o̓G[ꍇ
*/
public void writeBit( int data ) throws IOException {
this.bitBuffer |= ( data & 0x00000001 ) << 31 - this.bitCount;
this.bitCount++;
if( 32 <= this.bitCount ) this.writeOutBitBuffer(); //throws IOException
}
/**
* ^Ulڑꂽo̓Xg[1rbg
* f[^Ƃďo͂B<br>
* true 1Afalse 0Ƃďo͂B<br>
* java.io.DataOutput writeBoolean() Ƃ
* ݊̂Œӂ邱ƁB<br>
*
* @param bool ^Ul
*
* @exception IOException o̓G[ꍇ
*/
public void writeBoolean( boolean bool ) throws IOException {
if( bool ) this.bitBuffer |= 1 << 31 - this.bitCount;
this.bitCount++;
if( 32 <= this.bitCount ) this.writeOutBitBuffer(); //throws IOException
}
/**
* ڑꂽo̓Xg[Ƀrbgf[^o͂B<br>
*
* @param count data ̗Lrbg
* @param data rbgf[^
*
* @exception IOException o̓G[ꍇ
*/
public void writeBits( int count, int data ) throws IOException {
while( 0 < count ){
int available = 32 - this.bitCount;
if( count < available ){
this.bitBuffer |= ( data & ( 0xFFFFFFFF >>> 32 - count ) )
<< available - count;
this.bitCount += count;
count = 0;
}else{
count -= available;
this.bitBuffer |= data >> count
& ( 0xFFFFFFFF >>> 32 - available );
this.writeOutBitBuffer();
}
}
}
//------------------------------------------------------------------
// local method
//------------------------------------------------------------------
// private void writeOutBitBuffer()
//------------------------------------------------------------------
/**
* rbgobt@ɒ~ꂽf[^SăLbV
* o͂ALbVꍇ̓LbṼf[^
* ڑꂽo̓Xg[ɏo͂B<br>
*
* @exception IOException o̓G[ꍇ
*/
private void writeOutBitBuffer() throws IOException {
this.cache[ this.cachePosition++ ] = (byte)( this.bitBuffer >> 24 );
this.cache[ this.cachePosition++ ] = (byte)( this.bitBuffer >> 16 );
this.cache[ this.cachePosition++ ] = (byte)( this.bitBuffer >> 8 );
this.cache[ this.cachePosition++ ] = (byte)this.bitBuffer;
this.bitBuffer = 0;
this.bitCount = 0;
if( this.cache.length <= this.cachePosition ){
this.out.write( this.cache );
this.cachePosition = 0;
}
}
}
//end of BitOutputStream.java
|