下面列出了java.io.DataInputStream#readUnsignedByte ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Parse a domain name starting at the current offset and moving the input
* stream pointer past this domain name (even if cross references occure).
*
* @param dis The input stream.
* @param data The raw data (for cross references).
* @return The domain name string.
* @throws IOException Should never happen.
*/
private static String readName(DataInputStream dis, byte data[])
throws IOException {
int c = dis.readUnsignedByte();
if ((c & 0xc0) == 0xc0) {
c = ((c & 0x3f) << 8) + dis.readUnsignedByte();
HashSet<Integer> jumps = new HashSet<Integer>();
jumps.add(c);
return readName(data, c, jumps);
}
if (c == 0) {
return "";
}
byte b[] = new byte[c];
dis.readFully(b);
String s = IDN.toUnicode(new String(b));
String t = readName(dis, data);
if (t.length() > 0) {
s = s + "." + t;
}
return s;
}
@Override
public void read(DataInputStream f)
throws IOException {
name= DoomIO.readNullTerminatedString(f,SAVESTRINGSIZE);
vcheck=DoomIO.readNullTerminatedString(f,VERSIONSIZE);
gameskill=skill_t.values()[f.readUnsignedByte()];
gameepisode=f.readByte();
gamemap=f.readByte();
for (int i=0 ; i<MAXPLAYERS ; i++)
playeringame[i]=f.readBoolean();
// get the times
int a = f.readUnsignedByte();
int b = f.readUnsignedByte();
int c = f.readUnsignedByte();
// Quite anomalous, leveltime is stored as a BIG ENDIAN, 24-bit unsigned integer :-S
leveltime = (a<<16) | (b<<8) | c;
}
private static int readApduLength(DataInputStream is) throws IOException {
int length = is.readUnsignedByte();
if (length < MIN_APDU_LENGTH || length > MAX_APDU_LENGTH) {
String msg = MessageFormat
.format("APDU has an invalid length must be between 4 and 253.\nReceived length was: {0}.", length);
throw new IOException(msg);
}
return length;
}
public int read(DataInputStream dis) throws IOException{
// Pitch wheel change is a 14 bit value stored in two bytes,
// least significant first
this.value = (int) dis.readUnsignedByte();
this.value += ((int) dis.readUnsignedByte()) * 128;
return 1;
}
/** Read the contends of this object in from disk */
public int read(DataInputStream dis) throws IOException{
int b1 = (int)dis.readUnsignedByte();
int b2 = (int)dis.readUnsignedByte();
int b3 = (int)dis.readUnsignedByte();
int temp = (b1<<16) + (b2<<8) + b3;
this.tempo = (double) (1000000f / (float)temp * 60.0f);
return 3;
}
private boolean hasEntries(DataInputStream in, boolean failFast) throws IOException {
if (in.available() <= 0) {
return false;
} else if (in.readUnsignedByte() != ENTRY_MAGIC) {
if (failFast) {
throw new IOException("Corrupted entry separator");
} else {
return false;
}
}
return true;
}
private static int readInt(DataInputStream dataInputStream) throws IOException {
int byte1 = dataInputStream.readUnsignedByte();
int byte2 = dataInputStream.readUnsignedByte();
int byte3 = dataInputStream.readUnsignedByte();
int byte4 = dataInputStream.readUnsignedByte();
return (byte1 + (byte2 << 8) + (byte3 << 16) + (byte4 << 24));
}
static InformationElement decode(DataInputStream is) throws IOException {
int b1 = is.readUnsignedByte();
int value = b1 & 0x7f;
boolean negativeConfirm = ((b1 & 0x80) == 0x80);
return new IeFileReadyQualifier(value, negativeConfirm);
}
/**
* Read the contends of this objec in from disk
* Implemented by Sean T. Hayes
* @param dis a stream of MIDI data
* @return the number of bytes read
* @throws java.io.IOException
*/
public int read(DataInputStream dis) throws IOException
{
numerator = (short)dis.readUnsignedByte();
int power = (short)dis.readUnsignedByte();
denominator = 1 << power; // = Math.pow( 2, power )
metronomePulse = dis.readUnsignedByte();
thirtySecondNotesPerBeat = dis.readUnsignedByte();
return 4;
}
public RangeDecoderFromStream(InputStream in) throws IOException {
inData = new DataInputStream(in);
if (inData.readUnsignedByte() != 0x00)
throw new CorruptedInputException();
code = inData.readInt();
range = 0xFFFFFFFF;
}
private static TypeAnnotation parse(DataInputStream data, ConstantPool pool) throws IOException {
int targetType = data.readUnsignedByte();
int target = targetType << 24;
switch (targetType) {
case TypeAnnotation.CLASS_TYPE_PARAMETER:
case TypeAnnotation.METHOD_TYPE_PARAMETER:
case TypeAnnotation.METHOD_PARAMETER:
target |= data.readUnsignedByte();
break;
case TypeAnnotation.SUPER_TYPE_REFERENCE:
case TypeAnnotation.CLASS_TYPE_PARAMETER_BOUND:
case TypeAnnotation.METHOD_TYPE_PARAMETER_BOUND:
case TypeAnnotation.THROWS_REFERENCE:
case TypeAnnotation.CATCH_CLAUSE:
case TypeAnnotation.EXPR_INSTANCEOF:
case TypeAnnotation.EXPR_NEW:
case TypeAnnotation.EXPR_CONSTRUCTOR_REF:
case TypeAnnotation.EXPR_METHOD_REF:
target |= data.readUnsignedShort();
break;
case TypeAnnotation.TYPE_ARG_CAST:
case TypeAnnotation.TYPE_ARG_CONSTRUCTOR_CALL:
case TypeAnnotation.TYPE_ARG_METHOD_CALL:
case TypeAnnotation.TYPE_ARG_CONSTRUCTOR_REF:
case TypeAnnotation.TYPE_ARG_METHOD_REF:
data.skipBytes(3);
break;
case TypeAnnotation.LOCAL_VARIABLE:
case TypeAnnotation.RESOURCE_VARIABLE:
data.skipBytes(data.readUnsignedShort() * 6);
break;
case TypeAnnotation.FIELD:
case TypeAnnotation.METHOD_RETURN_TYPE:
case TypeAnnotation.METHOD_RECEIVER:
break;
default:
throw new RuntimeException("unknown target type: " + targetType);
}
int pathLength = data.readUnsignedByte();
byte[] path = null;
if (pathLength > 0) {
path = new byte[2 * pathLength];
data.readFully(path);
}
AnnotationExprent annotation = StructAnnotationAttribute.parseAnnotation(data, pool);
return new TypeAnnotation(target, path, annotation);
}
public JJJVMConstantPoolImpl(final JJJVMClass klazz, final DataInputStream inStream) throws IOException {
int index = 0;
this.klazz = klazz;
int itemsNumber = inStream.readUnsignedShort();
this.records = new JJJVMConstantPoolItem[itemsNumber];
this.records[index++] = null;
itemsNumber--;
final StringBuilder strBuffer = new StringBuilder(128);
while (itemsNumber > 0) {
boolean doubleRecordItem = false;
final int recordType = inStream.readUnsignedByte();
final Object recordValue;
switch (recordType) {
case JJJVMConstantPoolItem.CONSTANT_UTF8: {
recordValue = inStream.readUTF();
}
break;
case JJJVMConstantPoolItem.CONSTANT_UNICODE: {
final int len = inStream.readUnsignedShort();
for (int i = 0; i < len; i++) {
char ch_char = (char) inStream.readUnsignedShort();
strBuffer.append(ch_char);
}
recordValue = strBuffer.toString();
strBuffer.setLength(0);
}
break;
case JJJVMConstantPoolItem.CONSTANT_INTEGER: {
recordValue = inStream.readInt();
}
break;
case JJJVMConstantPoolItem.CONSTANT_FLOAT: {
recordValue = inStream.readFloat();
}
break;
case JJJVMConstantPoolItem.CONSTANT_LONG: {
recordValue = inStream.readLong();
doubleRecordItem = true;
}
break;
case JJJVMConstantPoolItem.CONSTANT_DOUBLE: {
recordValue = inStream.readDouble();
doubleRecordItem = true;
}
break;
case JJJVMConstantPoolItem.CONSTANT_CLASSREF:
case JJJVMConstantPoolItem.CONSTANT_STRING: {
recordValue = inStream.readUnsignedShort();
}
break;
case JJJVMConstantPoolItem.CONSTANT_FIELDREF:
case JJJVMConstantPoolItem.CONSTANT_METHODREF:
case JJJVMConstantPoolItem.CONSTANT_INTERFACEMETHOD:
case JJJVMConstantPoolItem.CONSTANT_NAMETYPEREF:
case JJJVMConstantPoolItem.CONSTANT_METHODHANDLE:
case JJJVMConstantPoolItem.CONSTANT_INVOKEDYNAMIC: {
final int high = inStream.readUnsignedShort();
final int low = inStream.readUnsignedShort();
recordValue = (high << 16) | low;
}
break;
case JJJVMConstantPoolItem.CONSTANT_METHODTYPE: {
final int descIndex = inStream.readUnsignedShort();
recordValue = descIndex;
}
break;
default: {
throw new IOException("Unsupported constant pool item [" + recordType + ']');
}
}
this.records[index++] = new JJJVMConstantPoolItem(this, recordType, recordValue);
if (doubleRecordItem) {
itemsNumber--;
index++;
}
itemsNumber--;
}
}
/**
* Reads the SIZ marker segment and realigns the codestream at the point
* where the next marker segment should be found.
*
* <p>SIZ is a fixed information marker segment containing informations
* about image and tile sizes. It is required in the main header
* immediately after SOC.</p>
*
* @param ehs The encoded header stream
*
* @exception IOException If an I/O error occurs while reading from the
* encoded header stream
* */
private void readSIZ (DataInputStream ehs) throws IOException {
HeaderInfo.SIZ ms = hi.getNewSIZ();
hi.siz = ms;
// Read the length of SIZ marker segment (Lsiz)
ms.lsiz = ehs.readUnsignedShort();
// Read the capability of the codestream (Rsiz)
ms.rsiz = ehs.readUnsignedShort();
if (ms.rsiz > 2) {
throw new Error("Codestream capabiities not JPEG 2000 - Part I"+
" compliant");
}
// Read image size
ms.xsiz = ehs.readInt();
ms.ysiz = ehs.readInt();
if ( ms.xsiz<=0 || ms.ysiz<=0 ) {
throw new IOException("JJ2000 does not support images whose "+
"width and/or height not in the "+
"range: 1 -- (2^31)-1");
}
// Read image offset
ms.x0siz = ehs.readInt();
ms.y0siz = ehs.readInt();
if ( ms.x0siz<0 || ms.y0siz<0 ) {
throw new IOException("JJ2000 does not support images offset "+
"not in the range: 0 -- (2^31)-1");
}
// Read size of tile
ms.xtsiz = ehs.readInt();
ms.ytsiz = ehs.readInt();
if ( ms.xtsiz<=0 || ms.ytsiz<=0 ) {
throw new IOException("JJ2000 does not support tiles whose "+
"width and/or height are not in "+
"the range: 1 -- (2^31)-1");
}
// Read upper-left tile offset
ms.xt0siz = ehs.readInt();
ms.yt0siz = ehs.readInt();
if ( ms.xt0siz<0 || ms.yt0siz<0 ){
throw new IOException("JJ2000 does not support tiles whose "+
"offset is not in "+
"the range: 0 -- (2^31)-1");
}
// Read number of components and initialize related arrays
nComp = ms.csiz = ehs.readUnsignedShort();
if (nComp<1 || nComp>16384) {
throw new IllegalArgumentException("Number of component out of "+
"range 1--16384: "+nComp);
}
ms.ssiz = new int[nComp];
ms.xrsiz = new int[nComp];
ms.yrsiz = new int[nComp];
// Read bit-depth and down-sampling factors of each component
for(int i = 0; i<nComp; i++) {
ms.ssiz[i] = ehs.readUnsignedByte();
ms.xrsiz[i] = ehs.readUnsignedByte();
ms.yrsiz[i] = ehs.readUnsignedByte();
}
// Check marker length
checkMarkerLength(ehs,"SIZ marker");
// Create needed ModuleSpec
nTiles = ms.getNumTiles();
// Finish initialization of decSpec
decSpec = new DecoderSpecs(nTiles,nComp);
}
static IeNameOfFile decode(DataInputStream is) throws IOException {
int value = is.readUnsignedByte() | (is.readUnsignedByte() << 8);
return new IeNameOfFile(value);
}
IeAbstractQuality(DataInputStream is) throws IOException {
value = is.readUnsignedByte();
}
private static TypeAnnotation parse(DataInputStream data, ConstantPool pool) throws IOException {
int targetType = data.readUnsignedByte();
int target = targetType << 24;
switch (targetType) {
case TypeAnnotation.CLASS_TYPE_PARAMETER:
case TypeAnnotation.METHOD_TYPE_PARAMETER:
case TypeAnnotation.METHOD_PARAMETER:
target |= data.readUnsignedByte();
break;
case TypeAnnotation.SUPER_TYPE_REFERENCE:
case TypeAnnotation.CLASS_TYPE_PARAMETER_BOUND:
case TypeAnnotation.METHOD_TYPE_PARAMETER_BOUND:
case TypeAnnotation.THROWS_REFERENCE:
case TypeAnnotation.CATCH_CLAUSE:
case TypeAnnotation.EXPR_INSTANCEOF:
case TypeAnnotation.EXPR_NEW:
case TypeAnnotation.EXPR_CONSTRUCTOR_REF:
case TypeAnnotation.EXPR_METHOD_REF:
target |= data.readUnsignedShort();
break;
case TypeAnnotation.TYPE_ARG_CAST:
case TypeAnnotation.TYPE_ARG_CONSTRUCTOR_CALL:
case TypeAnnotation.TYPE_ARG_METHOD_CALL:
case TypeAnnotation.TYPE_ARG_CONSTRUCTOR_REF:
case TypeAnnotation.TYPE_ARG_METHOD_REF:
data.skipBytes(3);
break;
case TypeAnnotation.LOCAL_VARIABLE:
case TypeAnnotation.RESOURCE_VARIABLE:
data.skipBytes(data.readUnsignedShort() * 6);
break;
case TypeAnnotation.FIELD:
case TypeAnnotation.METHOD_RETURN_TYPE:
case TypeAnnotation.METHOD_RECEIVER:
break;
default:
throw new RuntimeException("unknown target type: " + targetType);
}
int pathLength = data.readUnsignedByte();
byte[] path = null;
if (pathLength > 0) {
path = new byte[2 * pathLength];
data.readFully(path);
}
AnnotationExprent annotation = StructAnnotationAttribute.parseAnnotation(data, pool);
return new TypeAnnotation(target, path, annotation);
}
public int read(DataInputStream dis) throws IOException{
this.pressure = (short) dis.readUnsignedByte();
return 1;
}
@SuppressWarnings("fallthrough")
private byte[] optimize(ResourcePoolEntry resource, ResourcePoolBuilder resources,
StringTable strings,
Set<Integer> descriptorIndexes, byte[] content) throws Exception {
DataInputStream stream = new DataInputStream(new ByteArrayInputStream(content));
ByteArrayOutputStream outStream = new ByteArrayOutputStream(content.length);
DataOutputStream out = new DataOutputStream(outStream);
byte[] header = new byte[8]; //magic/4, minor/2, major/2
stream.readFully(header);
out.write(header);
int count = stream.readUnsignedShort();
out.writeShort(count);
for (int i = 1; i < count; i++) {
int tag = stream.readUnsignedByte();
byte[] arr;
switch (tag) {
case ConstantPool.CONSTANT_Utf8: {
String original = stream.readUTF();
// 2 cases, a Descriptor or a simple String
if (descriptorIndexes.contains(i)) {
SignatureParser.ParseResult parseResult
= SignatureParser.parseSignatureDescriptor(original);
List<Integer> indexes
= parseResult.types.stream().map((type) -> {
return strings.addString(type);
}).collect(Collectors.toList());
if (!indexes.isEmpty()) {
out.write(StringSharingDecompressor.EXTERNALIZED_STRING_DESCRIPTOR);
int sigIndex = strings.addString(parseResult.formatted);
byte[] compressed
= CompressIndexes.compress(sigIndex);
out.write(compressed, 0, compressed.length);
writeDescriptorReference(out, indexes);
continue;
}
}
// Put all strings in strings table.
writeUTF8Reference(out, strings.addString(original));
break;
}
case ConstantPool.CONSTANT_Long:
case ConstantPool.CONSTANT_Double: {
i++;
}
default: {
out.write(tag);
int size = SIZES[tag];
arr = new byte[size];
stream.readFully(arr);
out.write(arr);
}
}
}
out.write(content, content.length - stream.available(),
stream.available());
out.flush();
return outStream.toByteArray();
}
public void validate(InputStream in) throws IOException {
// Index Indicator (0x00) has already been read by BlockInputStream
// so add 0x00 to the CRC32 here.
java.util.zip.CRC32 crc32 = new java.util.zip.CRC32();
crc32.update('\0');
CheckedInputStream inChecked = new CheckedInputStream(in, crc32);
// Get and validate the Number of Records field.
long storedRecordCount = DecoderUtil.decodeVLI(inChecked);
if (storedRecordCount != recordCount)
throw new CorruptedInputException("XZ Index is corrupt");
// Decode and hash the Index field and compare it to
// the hash value calculated from the decoded Blocks.
IndexHash stored = new IndexHash();
for (long i = 0; i < recordCount; ++i) {
long unpaddedSize = DecoderUtil.decodeVLI(inChecked);
long uncompressedSize = DecoderUtil.decodeVLI(inChecked);
try {
stored.add(unpaddedSize, uncompressedSize);
} catch (XZIOException e) {
throw new CorruptedInputException("XZ Index is corrupt");
}
if (stored.blocksSum > blocksSum
|| stored.uncompressedSum > uncompressedSum
|| stored.indexListSize > indexListSize)
throw new CorruptedInputException("XZ Index is corrupt");
}
if (stored.blocksSum != blocksSum
|| stored.uncompressedSum != uncompressedSum
|| stored.indexListSize != indexListSize
|| !Arrays.equals(stored.hash.finish(), hash.finish()))
throw new CorruptedInputException("XZ Index is corrupt");
// Index Padding
DataInputStream inData = new DataInputStream(inChecked);
for (int i = getIndexPaddingSize(); i > 0; --i)
if (inData.readUnsignedByte() != 0x00)
throw new CorruptedInputException("XZ Index is corrupt");
// CRC32
long value = crc32.getValue();
for (int i = 0; i < 4; ++i)
if (((value >>> (i * 8)) & 0xFF) != inData.readUnsignedByte())
throw new CorruptedInputException("XZ Index is corrupt");
}
static IeNameOfSection decode(DataInputStream is) throws IOException {
return new IeNameOfSection(is.readUnsignedByte());
}