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
|
//start of LittleEndian.java
//TEXT_STYLE:CODE=Shift_JIS(Japanese):RET_CODE=CRLF
/**
* LittleEndian.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.InputStream;
import java.io.OutputStream;
//import exceptions
import java.io.IOException;
import java.io.EOFException;
import java.lang.ArrayIndexOutOfBoundsException;
/**
* gGfBA
* oCgz InputStream, OutputStream
* ɃANZX郁\bh郆[eBeBNXB
*
* <pre>
* -- revision history --
* $Log: LittleEndian.java,v $
* Revision 1.0 2002/07/24 00:00:00 dangan
* add to version control
* [change]
* writeByte(), readByte() PB
* [maintenance]
* \[X
*
* </pre>
*
* @author $Author: dangan $
* @version $Revision: 1.0 $
*/
public class LittleEndian{
//------------------------------------------------------------------
// constructor
//------------------------------------------------------------------
// private LittleEndian()
//------------------------------------------------------------------
/**
* ftHgRXgN^BgpsB
*/
private LittleEndian(){ }
//------------------------------------------------------------------
// shared method
//------------------------------------------------------------------
// read from byte array
//------------------------------------------------------------------
// public static final int readShort( byte[] ByteArray, int index )
// public static final int readInt( byte[] ByteArray, int index )
// public static final long readLong( byte[] ByteArray, int index )
//------------------------------------------------------------------
/**
* ByteArray index ̈ʒu gGfBA
* 2oCglǂݏoBǂݏoꂽ 2oCgl
* 0x0000`0xFFFFɃ}bvB
*
* @param ByteArray oCgz
* @param index ByteArraỹf[^̊Jnʒu
*
* @return ǂݏoꂽ2oCgl
*
* @exception ArrayIndexOutOfBoundsException
* indexn܂f[^
* ByteArray͈͓̔ɖꍇB
*/
public static final int readShort( byte[] ByteArray, int index ){
return ( ByteArray[index] & 0xFF )
| ( ( ByteArray[index + 1] & 0xFF ) << 8 );
}
/**
* ByteArray index ̈ʒu烊gGfBA
* 4oCglǂݏoB
*
* @param ByteArray oCgz
* @param index ByteArraỹf[^̊Jnʒu
*
* @return ǂݏoꂽ4oCgl
*
* @exception ArrayIndexOutOfBoundsException
* indexn܂f[^
* ByteArray͈͓̔ɖꍇB
*/
public static final int readInt( byte[] ByteArray, int index ){
return ( ByteArray[index] & 0xFF )
| ( ( ByteArray[index + 1] & 0xFF ) << 8 )
| ( ( ByteArray[index + 2] & 0xFF ) << 16 )
| ( ByteArray[index + 3] << 24 );
}
/**
* ByteArray index ̈ʒu烊gGfBA
* 8oCglǂݏoB
*
* @param ByteArray oCgz
* @param index ByteArraỹf[^̊Jnʒu
*
* @return ǂݏoꂽ8oCgl
*
* @exception ArrayIndexOutOfBoundsException
* indexn܂f[^
* ByteArray͈͓̔ɖꍇB
*/
public static final long readLong( byte[] ByteArray, int index ){
return ( (long)LittleEndian.readInt( ByteArray, index ) & 0xFFFFFFFFL )
| ( (long)LittleEndian.readInt( ByteArray, index + 4 ) << 32L );
}
//------------------------------------------------------------------
// shared method
//------------------------------------------------------------------
// read from InputStream
//------------------------------------------------------------------
// public static final int readShort( InputStream in )
// public static final int readInt( InputStream in )
// public static final long readLong( InputStream in )
//------------------------------------------------------------------
/**
* ̓Xg[ in gGfBA
* 2bytelǂݏoB
*
* @param in ̓Xg[
*
* @return ǂݏoꂽ2bytel
*
* @exception EOFException
* End Of StreamɒBĂA
* ǂݍ݂̓r End Of StreamɒBB
* ǂݍݓr̃f[^͏B
* @exception IOException
* o̓G[ꍇ
*/
public static final int readShort( InputStream in )
throws IOException {
int byte1 = in.read();
int byte2 = in.read();
if( 0 <= byte1 && 0 <= byte2 ){
return ( byte1 & 0xFF )
| ( ( byte2 & 0xFF ) << 8 );
}else{
throw new EOFException();
}
}
/**
* ̓Xg[ in gGfBA
* 4bytelǂݏoB
*
* @param in ̓Xg[
*
* @return ǂݏoꂽ4bytel
*
* @exception EOFException
* End Of StreamɒBĂA
* ǂݍ݂̓r End Of StreamɒBB
* ǂݍݓr̃f[^͏B
* @exception IOException
* o̓G[ꍇ
*/
public static final int readInt( InputStream in )
throws IOException {
int byte1 = in.read();
int byte2 = in.read();
int byte3 = in.read();
int byte4 = in.read();
if( 0 <= byte1 && 0 <= byte2 && 0 <= byte3 && 0 <= byte4 ){
return ( byte1 & 0xFF )
| ( ( byte2 & 0xFF ) << 8 )
| ( ( byte3 & 0xFF ) << 16 )
| ( byte4 << 24 );
}else{
throw new EOFException();
}
}
/**
* ̓Xg[ in gGfBA
* 8bytelǂݏoB
*
* @param in ̓Xg[
*
* @return ǂݏoꂽ8bytel
*
* @exception EOFException
* End Of StreamɒBĂA
* ǂݍ݂̓r End Of StreamɒBB
* ǂݍݓr̃f[^͏B
* @exception IOException
* o̓G[ꍇ
*/
public static final long readLong( InputStream in )
throws IOException {
return ( (long)LittleEndian.readInt( in ) & 0xFFFFFFFFL )
| ( (long)LittleEndian.readInt( in ) << 32 );
}
//------------------------------------------------------------------
// shared method
//------------------------------------------------------------------
// write to byte array
//------------------------------------------------------------------
// public static final void writeShort( byte[] ByteArray, int index, int value )
// public static final void writeInt( byte[] ByteArray, int index, int value )
// public static final void writeLong( byte[] ByteArray, int index, long value )
//------------------------------------------------------------------
/**
* ByteArray index ̈ʒuɃgGfBA
* 2byteloB
*
* @param ByteArray oCgz
* @param index ByteArraỹf[^ވʒu
* @param value 2bytel
*
* @exception ArrayIndexOutOfBoundsException
* indexn܂f[^
* ByteArray͈͓̔ɖꍇB
*/
public static final void writeShort( byte[] ByteArray,
int index,
int value ){
if( 0 <= index && index + 1 < ByteArray.length ){
ByteArray[index] = (byte)value;
ByteArray[index + 1] = (byte)( value >> 8 );
}else{
throw new ArrayIndexOutOfBoundsException();
}
}
/**
* ByteArray index ̈ʒuɃgGfBA
* 4byteloB
*
* @param ByteArray oCgz
* @param index ByteArraỹf[^ވʒu
* @param value 4bytel
*
* @exception ArrayIndexOutOfBoundsException
* indexn܂f[^
* ByteArray͈͓̔ɖꍇB
*/
public static final void writeInt( byte[] ByteArray,
int index,
int value ){
if( 0 <= index && index + 3 < ByteArray.length ){
ByteArray[index] = (byte)value;
ByteArray[index + 1] = (byte)( value >> 8 );
ByteArray[index + 2] = (byte)( value >> 16 );
ByteArray[index + 3] = (byte)( value >> 24 );
}else{
throw new ArrayIndexOutOfBoundsException();
}
}
/**
* ByteArray index ̈ʒuɃgGfBA
* 8byteloB
*
* @param ByteArray oCgz
* @param index ByteArraỹf[^ވʒu
* @param value 8bytel
*
* @exception ArrayIndexOutOfBoundsException
* indexn܂f[^
* ByteArray͈͓̔ɖꍇB
*/
public static final void writeLong( byte[] ByteArray,
int index,
long value ){
if( 0 <= index && index + 7 < ByteArray.length ){
LittleEndian.writeInt( ByteArray, index, (int)value );
LittleEndian.writeInt( ByteArray, index + 4, (int)(value >> 32) );
}else{
throw new ArrayIndexOutOfBoundsException();
}
}
//------------------------------------------------------------------
// shared method
//------------------------------------------------------------------
// write to OutputStream
//------------------------------------------------------------------
// public static final void writeShort( OutputStream out, int value )
// public static final void writeInt( OutputStream out, int value )
// public static final void writeLong( OutputStream out, long value )
//------------------------------------------------------------------
/**
* o̓Xg[ out gGfBA
* 2oCgoB
*
* @param out o̓Xg[
* @param value o2oCgl
*
* @exception IOException
* o̓G[ꍇ
*/
public static final void writeShort( OutputStream out, int value )
throws IOException {
out.write( value & 0xFF );
out.write( ( value >> 8 ) & 0xFF );
}
/**
* o̓Xg[ out gGfBA
* 4oCgloB
*
* @param out o̓Xg[
* @param value o1oCgl
*
* @exception IOException
* o̓G[ꍇ
*/
public static final void writeInt( OutputStream out, int value )
throws IOException {
out.write( value & 0xFF );
out.write( ( value >> 8 ) & 0xFF );
out.write( ( value >> 16 ) & 0xFF );
out.write( value >>> 24 );
}
/**
* o̓Xg[ out gGfBA
* 8oCgloB
*
* @param out o̓Xg[
* @param value o1oCgl
*
* @exception IOException
* o̓G[ꍇ
*/
public static final void writeLong( OutputStream out, long value )
throws IOException {
int low = (int)value;
int hi = (int)( value >> 32 );
out.write( low & 0xFF );
out.write( ( low >> 8 ) & 0xFF );
out.write( ( low >> 16 ) & 0xFF );
out.write( low >>> 24 );
out.write( hi & 0xFF );
out.write( ( hi >> 8 ) & 0xFF );
out.write( ( hi >> 16 ) & 0xFF );
out.write( hi >>> 24 );
}
}
//end of LittleEndian.java
|