📄 structconverter.java
字号:
/*
* StructConverter.java -
*
* This file is part of the Jawin Project: http://jawinproject.sourceforge.net/
*
* Please consult the LICENSE file in the project root directory,
* or at the project site before using this software.
*/
/* $Id: StructConverter.java,v 1.3 2004/06/14 19:49:14 arosii_moa Exp $ */
package org.jawin.marshal;
import java.io.IOException;
import org.jawin.COMException;
import org.jawin.GUID;
import org.jawin.IdentityManager;
import org.jawin.io.LittleEndianInputStream;
public class StructConverter {
/**
* private constructor to avoid instantiation, as this class only contains
* static methods.
*/
private StructConverter() {
// never called
}
/*
public static INative structToMem(IStruct s)
throws COMException
{
byte[] bytes = s.instToBytes();
System.out.println(HexFormatter.convertBytesToString(bytes, 16, true));
if (bytes == null) {
throw new Error("reflective marshalling not implemented yet");
}
MemPtr mp = new MemPtr(bytes);
return mp;
}
*/
/**
* @param val max 16 characters long. Should not contain any '0x'-prefix.
* the empty string will return 0.
* @throws NumberFormatException if val is longer than 16 characters
* or if val contains non parseable characters ([0-9, a-f, A-F] is legal).
* @throws NullPointerException if val is null.
* @see #parseInt(String)
*/
public static long parseLong(String val) {
return parseHex(val, 16);
}
/**
* parse a hex string into an int. This method differs from
* {@link Integer#parseInt(java.lang.String, int) Integer.parseInt(val, 16)}
* in that it does not allow '-' and allows the full range of eight
* hex characters (eg. 'FFFFFFFF' is allowed). Since int is signed
* the string is parsed in the standard signed 2-complement
* representaion, eg:
* <ul>
* <li>7FFFFFFF -> {@link Integer#MAX_VALUE Integer.MAX_VALUE}</li>
* <li>80000000 -> {@link Integer#MIN_VALUE Integer.MIN_VALUE}</li>
* <li>FFFFFFFF -> -1</li>
* </ul>
*
* @param val max 8 characters long. Should not contain any '0x'-prefix.
* the empty string will return 0.
* @throws NumberFormatException if val is longer than 8 characters
* or if val contains non parseable characters ([0-9, a-f, A-F] is legal).
* @throws NullPointerException if val is null.
*/
public static int parseInt(String val) {
return (int)parseHex(val, 8);
}
/**
* @param val max 4 characters long. Should not contain any '0x'-prefix.
* the empty string will return 0.
* @throws NumberFormatException if val is longer than 4 characters
* or if val contains non parseable characters ([0-9, a-f, A-F] is legal).
* @throws NullPointerException if val is null.
* @see #parseInt(String)
*/
public static short parseShort(String val) {
return (short)parseHex(val, 4);
}
/**
* @param maxChars the maximum number of hex digits, should be in the range ]0; 16]
* @throws NumberFormatException if unable to parse val.
*/
private static long parseHex(String val, int maxChars) {
int length = val.length();
if (length > maxChars) {
throw new NumberFormatException("unable to parse: '" + val + "' (too many digits)");
}
int shift = 0;
long res = 0;
for (int i = length-1; i >= 0; i--) {
int digit = Character.digit(val.charAt(i), 16);
if (digit < 0) {
throw new NumberFormatException("unable to parse: '" + val + "' (invalid char at pos: " + i + ")");
}
res += ((long)digit) << shift;
shift += 4;
}
return res;
}
static public int longIntoBEBytes(long data, byte[] bytes, int start) {
bytes[start++] = (byte) (data >>> 56);
bytes[start++] = (byte) (data >>> 48);
bytes[start++] = (byte) (data >>> 40);
bytes[start++] = (byte) (data >>> 32);
bytes[start++] = (byte) (data >>> 24);
bytes[start++] = (byte) (data >>> 16);
bytes[start++] = (byte) (data >>> 8);
bytes[start++] = (byte) (data);
return start;
}
static public long bytesIntoLong(byte[] bytes, int offset) {
int nLo = bytesIntoInt(bytes, offset);
int nHi = bytesIntoInt(bytes, offset+4);
return ((long)(nHi) << 32) + (nLo & 0xFFFFFFFFL);
}
static public double bytesIntoDouble(byte[] bytes, int offset) {
double d = Double.longBitsToDouble(bytesIntoLong(bytes, offset));
return d;
}
static public float bytesIntoFloat(byte[] bytes, int offset) {
float f = Float.intBitsToFloat(bytesIntoInt(bytes, offset));
return f;
}
static public boolean bytesIntoBoolean(byte[] bytes, int offset) {
return bytes[offset] != 0;
}
static public int bytesIntoInt(byte[] bytes, int offset) {
int l = (bytes[offset] & 0xff) |
((bytes[offset+1] & 0xff) << 8) |
((bytes[offset+2] & 0xff) << 16) |
((bytes[offset+3] & 0xff) << 24);
return l;
}
static public int BEBytesIntoInt(byte[] bytes, int offset) {
int l = (bytes[offset+3] & 0xff) |
((bytes[offset+2] & 0xff) << 8) |
((bytes[offset+1] & 0xff) << 16) |
((bytes[offset+0] & 0xff) << 24);
return l;
}
static public short bytesIntoShort(byte[] bytes, int offset) {
int l = (bytes[offset] & 0xff) |
((bytes[offset+1] & 0xff) << 8);
return (short) l;
}
static public int longIntoBytes(long data, byte[] bytes, int start) {
bytes[start++] = (byte) (data);
bytes[start++] = (byte) (data >>> 8);
bytes[start++] = (byte) (data >>> 16);
bytes[start++] = (byte) (data >>> 24);
bytes[start++] = (byte) (data >>> 32);
bytes[start++] = (byte) (data >>> 40);
bytes[start++] = (byte) (data >>> 48);
bytes[start++] = (byte) (data >>> 56);
return start;
}
static public int intIntoBytes(int data, byte[] bytes, int start) {
bytes[start++] = (byte) (data);
bytes[start++] = (byte) (data >>> 8);
bytes[start++] = (byte) (data >>> 16);
bytes[start++] = (byte) (data >>> 24);
return start;
}
static public int shortIntoBytes(short data, byte[] bytes, int start) {
bytes[start++] = (byte) (data);
bytes[start++] = (byte) (data >>> 8);
return start;
}
static public int byteArrayIntoBytes(byte[] src, byte[] dest, int start) {
System.arraycopy(src, 0, dest, start, src.length);
return start + src.length;
}
static public String[] readStringArray(LittleEndianInputStream leis)
throws IOException
{
byte isNull = leis.readByte();
if (isNull == 0)
return null;
int size = leis.readInt();
String[] result = new String[size];
for (int n=0; n<size; n++) {
result[n] = leis.readStringUnicode();
}
return result;
}
static public int[] readIntArray(LittleEndianInputStream leis)
throws IOException
{
byte isNull = leis.readByte();
if (isNull == 0)
return null;
int size = leis.readInt();
int[] result = new int[size];
for (int n=0; n<size; n++) {
result[n] = leis.readInt();
}
return result;
}
static public Object[] readObjectArray(LittleEndianInputStream leis, GUID iid)
throws IOException, COMException
{
//COMEBACK: could infer array type from iid
byte isNull = leis.readByte();
if (isNull == 0)
return null;
int size = leis.readInt();
Object[] result = new Object[size];
for (int n=0; n<size; n++) {
result[n] = IdentityManager.getCOMPtr(leis, iid);
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -