下面列出了java.io.DataInputStream#readLong ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Google price decrypter
* @param websafeB64EncodedCiphertext String. The encoded crypto text
* @param utc long. The current UTC.
* @return double. The decrypted price as a double.
* @throws Exception on crypto errors.
*/
public static double decrypt(String websafeB64EncodedCiphertext, long utc) throws Exception {
String b64EncodedCiphertext = Decrypter.unWebSafeAndPad(websafeB64EncodedCiphertext);
byte[] codeString = Base64.decodeBase64(b64EncodedCiphertext.getBytes("US-ASCII"));
byte[] plaintext;
double value;
SecretKey encryptionKey = new SecretKeySpec(encryptionKeyBytes, "HmacSHA1");
SecretKey integrityKey = new SecretKeySpec(integrityKeyBytes, "HmacSHA1");
try {
plaintext = Decrypter.decrypt(codeString, encryptionKey, integrityKey);
DataInputStream dis = new DataInputStream( new ByteArrayInputStream(plaintext));
value = dis.readLong();
return value;
} catch (Exception e) {
// might be a test, we will try to just use it as is.
try {
value = Double.parseDouble(websafeB64EncodedCiphertext);
return value;
} catch (Exception ee) {
logger.warn("Failed to decode ciphertext; {}, error: {}", websafeB64EncodedCiphertext, e.getMessage());
throw ee;
}
}
}
/**
* Parse the operator and its arguments from bytecode.
*/
public Vertex parseOperatorByteCode(DataInputStream dataStream, Vertex pop, Network network) throws IOException {
Vertex expression = network.createTemporyVertex();
expression.addRelationship(Primitive.INSTANTIATION, Primitive.EXPRESSION);
long id = dataStream.readLong();
Vertex operator = network.findById(id);
if (operator == null) {
return expression;
}
expression.setName(operator.getDataValue());
expression.addRelationship(Primitive.OPERATOR, operator);
id = dataStream.readLong();
if (id == 0) {
return expression;
}
while (id > 0) {
Vertex next = network.findById(id);
if (next == null) {
return expression;
}
parseArgumentsByteCode(expression, dataStream, next, pop, network);
id = dataStream.readLong();
}
return expression;
}
/**
* Reads a byte stream, which was written by {@linkplain #writeTo(OutputStream)}, into a
* {@code BloomFilter<T>}.
*
* The {@code Funnel} to be used is not encoded in the stream, so it must be provided here.
* <b>Warning:</b> the funnel provided <b>must</b> behave identically to the one used to populate
* the original Bloom filter!
*
* @throws IOException if the InputStream throws an {@code IOException}, or if its data does not
* appear to be a BloomFilter serialized using the {@linkplain #writeTo(OutputStream)} method.
*/
public static <T> BloomFilter<T> readFrom(InputStream in, Funnel<T> funnel)
throws IOException {
checkNotNull(in, "InputStream");
checkNotNull(funnel, "Funnel");
int strategyOrdinal = -1;
int numHashFunctions = -1;
int dataLength = -1;
try {
DataInputStream din = new DataInputStream(in);
// currently this assumes there is no negative ordinal; will have to be updated if we
// add non-stateless strategies (for which we've reserved negative ordinals; see
// Strategy.ordinal()).
strategyOrdinal = din.readByte();
numHashFunctions = UnsignedBytes.toInt(din.readByte());
dataLength = din.readInt();
Strategy strategy = BloomFilterStrategies.values() [strategyOrdinal];
long[] data = new long[dataLength];
for (int i = 0; i < data.length; i++) {
data[i] = din.readLong();
}
return new BloomFilter<T>(new BitArray(data), numHashFunctions, funnel, strategy);
} catch (RuntimeException e) {
IOException ioException = new IOException("Unable to deserialize BloomFilter from InputStream." + " strategyOrdinal: "
+ strategyOrdinal
+ " numHashFunctions: "
+ numHashFunctions + " dataLength: " + dataLength);
ioException.initCause(e);
throw ioException;
}
}
@Override
public void initialize() throws Exception {
byte[] payload = getContext().getUserPayload();
ByteArrayInputStream bis = new ByteArrayInputStream(payload);
DataInputStream dis = new DataInputStream(bis);
streamOutputFileSize = dis.readLong();
hashOutputFileSize = dis.readLong();
LOG.info("Initialized with largeFileTargetSize=" + streamOutputFileSize
+ ", smallFileTragetSize=" + hashOutputFileSize);
dis.close();
bis.close();
}
@VisibleForTesting
public static OperatorStateHandle deserializeOperatorStateHandle(
DataInputStream dis) throws IOException {
final int type = dis.readByte();
if (NULL_HANDLE == type) {
return null;
} else if (PARTITIONABLE_OPERATOR_STATE_HANDLE == type) {
int mapSize = dis.readInt();
Map<String, OperatorStateHandle.StateMetaInfo> offsetsMap = new HashMap<>(mapSize);
for (int i = 0; i < mapSize; ++i) {
String key = dis.readUTF();
int modeOrdinal = dis.readByte();
OperatorStateHandle.Mode mode = OperatorStateHandle.Mode.values()[modeOrdinal];
long[] offsets = new long[dis.readInt()];
for (int j = 0; j < offsets.length; ++j) {
offsets[j] = dis.readLong();
}
OperatorStateHandle.StateMetaInfo metaInfo =
new OperatorStateHandle.StateMetaInfo(offsets, mode);
offsetsMap.put(key, metaInfo);
}
StreamStateHandle stateHandle = deserializeStreamStateHandle(dis);
return new OperatorStreamStateHandle(offsetsMap, stateHandle);
} else {
throw new IllegalStateException("Reading invalid OperatorStateHandle, type: " + type);
}
}
public void deserialize(DataInputStream dis) throws IOException {
randomSeed = dis.readLong();
playerId = dis.readByte();
mapName = dis.readUTF();
mapId = dis.readUTF();
playerSettings = new PlayerSetting[dis.readInt()];
for (int i = 0; i < playerSettings.length; i++) {
playerSettings[i] = PlayerSetting.readFromStream(dis);
}
}
private static OperatorSubtaskState deserializeSubtaskState(DataInputStream dis) throws IOException {
// Duration field has been removed from SubtaskState, do not remove
long ignoredDuration = dis.readLong();
// for compatibility, do not remove
int len = dis.readInt();
if (SavepointSerializers.FAIL_WHEN_LEGACY_STATE_DETECTED) {
Preconditions.checkState(len == 0,
"Legacy state (from Flink <= 1.1, created through the 'Checkpointed' interface) is " +
"no longer supported starting from Flink 1.4. Please rewrite your job to use " +
"'CheckpointedFunction' instead!");
} else {
for (int i = 0; i < len; ++i) {
// absorb bytes from stream and ignore result
deserializeStreamStateHandle(dis);
}
}
len = dis.readInt();
OperatorStateHandle operatorStateBackend = len == 0 ? null : deserializeOperatorStateHandle(dis);
len = dis.readInt();
OperatorStateHandle operatorStateStream = len == 0 ? null : deserializeOperatorStateHandle(dis);
KeyedStateHandle keyedStateBackend = deserializeKeyedStateHandle(dis);
KeyedStateHandle keyedStateStream = deserializeKeyedStateHandle(dis);
return new OperatorSubtaskState(
operatorStateBackend,
operatorStateStream,
keyedStateBackend,
keyedStateStream);
}
private DelegationTokenInformation getTokenInfoFromZK(TokenIdent ident,
boolean quiet) throws IOException {
String nodePath =
getNodePath(ZK_DTSM_TOKENS_ROOT,
DELEGATION_TOKEN_PREFIX + ident.getSequenceNumber());
try {
byte[] data = zkClient.getData().forPath(nodePath);
if ((data == null) || (data.length == 0)) {
return null;
}
ByteArrayInputStream bin = new ByteArrayInputStream(data);
DataInputStream din = new DataInputStream(bin);
createIdentifier().readFields(din);
long renewDate = din.readLong();
int pwdLen = din.readInt();
byte[] password = new byte[pwdLen];
int numRead = din.read(password, 0, pwdLen);
if (numRead > -1) {
DelegationTokenInformation tokenInfo =
new DelegationTokenInformation(renewDate, password);
return tokenInfo;
}
} catch (KeeperException.NoNodeException e) {
if (!quiet) {
LOG.error("No node in path [" + nodePath + "]");
}
} catch (Exception ex) {
throw new IOException(ex);
}
return null;
}
public static TimeResponse fromByteArray(byte[] bytes) throws IOException
{
ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
DataInputStream dataIn = new DataInputStream(bi);
byte packetId = dataIn.readByte();
long timeStamp = dataIn.readLong();
return new TimeResponse(packetId, timeStamp);
}
public MockFindToken(DataInputStream stream) throws IOException {
this.version = stream.readShort();
this.type = FindTokenType.values()[stream.readShort()];
this.index = stream.readInt();
this.bytesRead = stream.readLong();
}
protected Date readDate( DataInputStream inputStream ) throws IOException {
long time = inputStream.readLong();
return new Date( time );
}
/**
* Process an INode
*
* @param in image stream
* @param v visitor
* @param skipBlocks skip blocks or not
* @param parentName the name of its parent node
* @param isSnapshotCopy whether or not the inode is a snapshot copy
* @throws IOException
*/
private void processINode(DataInputStream in, ImageVisitor v,
boolean skipBlocks, String parentName, boolean isSnapshotCopy)
throws IOException {
boolean supportSnapshot =
NameNodeLayoutVersion.supports(Feature.SNAPSHOT, imageVersion);
boolean supportInodeId =
NameNodeLayoutVersion.supports(Feature.ADD_INODE_ID, imageVersion);
v.visitEnclosingElement(ImageElement.INODE);
final String pathName = readINodePath(in, parentName);
v.visit(ImageElement.INODE_PATH, pathName);
long inodeId = INodeId.GRANDFATHER_INODE_ID;
if (supportInodeId) {
inodeId = in.readLong();
v.visit(ImageElement.INODE_ID, inodeId);
}
v.visit(ImageElement.REPLICATION, in.readShort());
v.visit(ImageElement.MODIFICATION_TIME, formatDate(in.readLong()));
if(NameNodeLayoutVersion.supports(Feature.FILE_ACCESS_TIME, imageVersion))
v.visit(ImageElement.ACCESS_TIME, formatDate(in.readLong()));
v.visit(ImageElement.BLOCK_SIZE, in.readLong());
int numBlocks = in.readInt();
processBlocks(in, v, numBlocks, skipBlocks);
if (numBlocks >= 0) { // File
if (supportSnapshot) {
// make sure subtreeMap only contains entry for directory
subtreeMap.remove(inodeId);
// process file diffs
processFileDiffList(in, v, parentName);
if (isSnapshotCopy) {
boolean underConstruction = in.readBoolean();
if (underConstruction) {
v.visit(ImageElement.CLIENT_NAME,
FSImageSerialization.readString(in));
v.visit(ImageElement.CLIENT_MACHINE,
FSImageSerialization.readString(in));
}
}
}
processPermission(in, v);
} else if (numBlocks == -1) { // Directory
if (supportSnapshot && supportInodeId) {
dirNodeMap.put(inodeId, pathName);
}
v.visit(ImageElement.NS_QUOTA, numBlocks == -1 ? in.readLong() : -1);
if (NameNodeLayoutVersion.supports(Feature.DISKSPACE_QUOTA, imageVersion))
v.visit(ImageElement.DS_QUOTA, numBlocks == -1 ? in.readLong() : -1);
if (supportSnapshot) {
boolean snapshottable = in.readBoolean();
if (!snapshottable) {
boolean withSnapshot = in.readBoolean();
v.visit(ImageElement.IS_WITHSNAPSHOT_DIR, Boolean.toString(withSnapshot));
} else {
v.visit(ImageElement.IS_SNAPSHOTTABLE_DIR, Boolean.toString(snapshottable));
}
}
processPermission(in, v);
} else if (numBlocks == -2) {
v.visit(ImageElement.SYMLINK, Text.readString(in));
processPermission(in, v);
} else if (numBlocks == -3) { // reference node
final boolean isWithName = in.readBoolean();
int snapshotId = in.readInt();
if (isWithName) {
v.visit(ImageElement.SNAPSHOT_LAST_SNAPSHOT_ID, snapshotId);
} else {
v.visit(ImageElement.SNAPSHOT_DST_SNAPSHOT_ID, snapshotId);
}
final boolean firstReferred = in.readBoolean();
if (firstReferred) {
// if a subtree is linked by multiple "parents", the corresponding dir
// must be referred by a reference node. we put the reference node into
// the subtreeMap here and let its value be false. when we later visit
// the subtree for the first time, we change the value to true.
subtreeMap.put(inodeId, false);
v.visitEnclosingElement(ImageElement.SNAPSHOT_REF_INODE);
processINode(in, v, skipBlocks, parentName, isSnapshotCopy);
v.leaveEnclosingElement(); // referred inode
} else {
v.visit(ImageElement.SNAPSHOT_REF_INODE_ID, in.readLong());
}
}
v.leaveEnclosingElement(); // INode
}
/**
* Customizes a class file by replacing constant pools.
*
* @param image
* The image of the template class.
* @param replacements
* A list of pair of strings that specify the substitution
* {@code String[]{search_0, replace_0, search_1, replace_1, ..., search_n, replace_n }}
*
* The search strings found in the constant pool will be replaced by the corresponding
* replacement string.
*/
public static byte[] tailor( InputStream image, String templateClassName, String newClassName, String... replacements ) {
DataInputStream in = new DataInputStream(image);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
DataOutputStream out = new DataOutputStream(baos);
// skip until the constant pool count
long l = in.readLong();
out.writeLong(l);
// read the constant pool size
short count = in.readShort();
out.writeShort(count);
// replace constant pools
for( int i=0; i<count; i++ ) {
byte tag = in.readByte();
out.writeByte(tag);
switch(tag) {
case 0:
// this isn't described in the spec,
// but class files often seem to have this '0' tag.
// we can apparently just ignore it, but not sure
// what this really means.
break;
case 1: // CONSTANT_UTF8
{
String value = in.readUTF();
if(value.equals(templateClassName))
value = newClassName;
else {
for( int j=0; j<replacements.length; j+=2 )
if(value.equals(replacements[j])) {
value = replacements[j+1];
break;
}
}
out.writeUTF(value);
}
break;
case 3: // CONSTANT_Integer
case 4: // CONSTANT_Float
out.writeInt(in.readInt());
break;
case 5: // CONSTANT_Long
case 6: // CONSTANT_Double
i++; // doubles and longs take two entries
out.writeLong(in.readLong());
break;
case 7: // CONSTANT_Class
case 8: // CONSTANT_String
out.writeShort(in.readShort());
break;
case 9: // CONSTANT_Fieldref
case 10: // CONSTANT_Methodref
case 11: // CONSTANT_InterfaceMethodref
case 12: // CONSTANT_NameAndType
out.writeInt(in.readInt());
break;
default:
throw new IllegalArgumentException("Unknown constant type "+tag);
}
}
// then copy the rest
byte[] buf = new byte[512];
int len;
while((len=in.read(buf))>0)
out.write(buf,0,len);
in.close();
out.close();
// by now we got the properly tailored class file image
return baos.toByteArray();
} catch( IOException e ) {
// never happen
logger.log(Level.WARNING,"failed to tailor",e);
return null;
}
}
/**
* Constructor
*/
BinaryConstantPool(DataInputStream in) throws IOException {
// JVM 4.1 ClassFile.constant_pool_count
types = new byte[in.readUnsignedShort()];
cpool = new Object[types.length];
for (int i = 1 ; i < cpool.length ; i++) {
int j = i;
// JVM 4.4 cp_info.tag
switch(types[i] = in.readByte()) {
case CONSTANT_UTF8:
cpool[i] = in.readUTF();
break;
case CONSTANT_INTEGER:
cpool[i] = new Integer(in.readInt());
break;
case CONSTANT_FLOAT:
cpool[i] = new Float(in.readFloat());
break;
case CONSTANT_LONG:
cpool[i++] = new Long(in.readLong());
break;
case CONSTANT_DOUBLE:
cpool[i++] = new Double(in.readDouble());
break;
case CONSTANT_CLASS:
case CONSTANT_STRING:
// JVM 4.4.3 CONSTANT_String_info.string_index
// or JVM 4.4.1 CONSTANT_Class_info.name_index
cpool[i] = new Integer(in.readUnsignedShort());
break;
case CONSTANT_FIELD:
case CONSTANT_METHOD:
case CONSTANT_INTERFACEMETHOD:
case CONSTANT_NAMEANDTYPE:
// JVM 4.4.2 CONSTANT_*ref_info.class_index & name_and_type_index
cpool[i] = new Integer((in.readUnsignedShort() << 16) | in.readUnsignedShort());
break;
case CONSTANT_METHODHANDLE:
cpool[i] = readBytes(in, 3);
break;
case CONSTANT_METHODTYPE:
cpool[i] = readBytes(in, 2);
break;
case CONSTANT_INVOKEDYNAMIC:
cpool[i] = readBytes(in, 4);
break;
case 0:
default:
throw new ClassFormatError("invalid constant type: " + (int)types[i]);
}
}
}
private StandardProvenanceEventRecord readPreVersion6Record(final DataInputStream dis, final int serializationVersion) throws IOException {
final long startOffset = getBytesConsumed();
final StandardProvenanceEventRecord.Builder builder = new StandardProvenanceEventRecord.Builder();
final long eventId = dis.readLong();
if (serializationVersion == 4) {
// notion of a UUID for the event was added in Version 4 so that Events can be referred to uniquely
// across a cluster. This was then removed in version 5 because it was decided that a unique id
// could better be generated based on the event id and the cluster node identifier.
// Therefore, we read in the Event Identifier and throw it away.
dis.readUTF();
}
final String eventTypeName = dis.readUTF();
final ProvenanceEventType eventType = ProvenanceEventType.valueOf(eventTypeName);
builder.setEventType(eventType);
builder.setEventTime(dis.readLong());
if (serializationVersion > 3) {
// event duration introduced in version 4.
builder.setEventDuration(dis.readLong());
}
dis.readLong(); // Used to persist FlowFileId
final long fileSize = dis.readLong();
builder.setComponentId(readNullableString(dis));
builder.setComponentType(readNullableString(dis));
builder.setFlowFileUUID(readNullableString(dis));
final int numParents = dis.readInt();
for (int i = 0; i < numParents; i++) {
builder.addParentUuid(dis.readUTF());
}
if (serializationVersion > 2) {
// notion of child UUID's was introduced in version 3.
final int numChildren = dis.readInt();
for (int i = 0; i < numChildren; i++) {
builder.addChildUuid(dis.readUTF());
}
}
final String sourceSystemUri = readNullableString(dis);
if (serializationVersion > 3) {
// notion of a source system flowfile identifier was introduced in version 4.
builder.setSourceSystemFlowFileIdentifier(readNullableString(dis));
}
final String destinationSystemUri = readNullableString(dis);
if (sourceSystemUri != null) {
builder.setTransitUri(sourceSystemUri);
} else if (destinationSystemUri != null) {
builder.setTransitUri(destinationSystemUri);
}
readNullableString(dis); // Content-Type No longer used
builder.setAlternateIdentifierUri(readNullableString(dis));
final Map<String, String> attrs = readAttributes(dis, false);
builder.setFlowFileEntryDate(System.currentTimeMillis());
builder.setLineageStartDate(-1L);
builder.setAttributes(Collections.<String, String>emptyMap(), attrs);
builder.setCurrentContentClaim(null, null, null, null, fileSize);
builder.setStorageLocation(getFilename(), startOffset);
final StandardProvenanceEventRecord record = builder.build();
record.setEventId(eventId);
return record;
}
/**
* Parse the operation argument.
*/
public void parseArgumentByteCode(Object[] result, DataInputStream dataStream, Vertex pop, Network network) throws IOException {
Vertex last = null;
Vertex next = (Vertex)result[1];
Long id = (Long)result[0];
if (id == 0l) {
result[0] = id;
result[1] = null;
result[2] = null;
return;
}
Vertex element = next;
if (element == null) {
element = network.findById(id);
}
if (element == null) {
result[0] = dataStream.readLong();
result[1] = null;
result[2] = null;
return;
}
if (element.is(Primitive.EXPRESSION)) {
element = parseOperatorByteCode(dataStream, network);
}
if (element.is(Primitive.POP)) {
element = pop;
}
id = dataStream.readLong();
if (id == 0l) {
result[0] = id;
result[1] = null;
result[2] = element;
return;
}
last = element;
next = network.findById(id);
while ((next != null) && (next.is(Primitive.PUSH))) {
element = parseOperatorByteCode(dataStream, last, network);
id = dataStream.readLong();
if (id == 0l) {
next = null;
break;
}
last = element;
next = network.findById(id);
}
result[0] = id;
result[1] = next;
result[2] = element;
}
/**
* Process the INodes under construction section of the fsimage.
*
* @param in DataInputStream to process
* @param v Visitor to walk over inodes
* @param skipBlocks Walk over each block?
*/
private void processINodesUC(DataInputStream in, ImageVisitor v,
boolean skipBlocks) throws IOException {
int numINUC = in.readInt();
v.visitEnclosingElement(ImageElement.INODES_UNDER_CONSTRUCTION,
ImageElement.NUM_INODES_UNDER_CONSTRUCTION, numINUC);
for(int i = 0; i < numINUC; i++) {
v.visitEnclosingElement(ImageElement.INODE_UNDER_CONSTRUCTION);
byte [] name = FSImageSerialization.readBytes(in);
String n = new String(name, "UTF8");
v.visit(ImageElement.INODE_PATH, n);
if (NameNodeLayoutVersion.supports(Feature.ADD_INODE_ID, imageVersion)) {
long inodeId = in.readLong();
v.visit(ImageElement.INODE_ID, inodeId);
}
v.visit(ImageElement.REPLICATION, in.readShort());
v.visit(ImageElement.MODIFICATION_TIME, formatDate(in.readLong()));
v.visit(ImageElement.PREFERRED_BLOCK_SIZE, in.readLong());
int numBlocks = in.readInt();
processBlocks(in, v, numBlocks, skipBlocks);
processPermission(in, v);
v.visit(ImageElement.CLIENT_NAME, FSImageSerialization.readString(in));
v.visit(ImageElement.CLIENT_MACHINE, FSImageSerialization.readString(in));
// Skip over the datanode descriptors, which are still stored in the
// file but are not used by the datanode or loaded into memory
int numLocs = in.readInt();
for(int j = 0; j < numLocs; j++) {
in.readShort();
in.readLong();
in.readLong();
in.readLong();
in.readInt();
FSImageSerialization.readString(in);
FSImageSerialization.readString(in);
WritableUtils.readEnum(in, AdminStates.class);
}
v.leaveEnclosingElement(); // INodeUnderConstruction
}
v.leaveEnclosingElement(); // INodesUnderConstruction
}
/**
* Parse the GOTO bytecode.
*/
@Override
public Vertex parseGotoByteCode(DataInputStream dataStream, Network network) throws IOException {
Vertex expression = network.createTemporyVertex();
expression.addRelationship(Primitive.INSTANTIATION, Primitive.GOTO);
long id = dataStream.readLong();
if (id == 0) {
return expression;
}
Vertex element = network.findById(id);
if (element == null) {
return expression;
}
if (element.is(Primitive.FINALLY)) {
expression.addRelationship(Primitive.FINALLY, Primitive.FINALLY);
id = dataStream.readLong();
if (id == 0) {
return expression;
}
element = network.findById(id);
if (element == null) {
return expression;
}
}
expression.addRelationship(Primitive.GOTO, element);
id = dataStream.readLong();
if (id == 0) {
return expression;
}
element = network.findById(id);
if (element == null) {
return expression;
}
if (element.is(Primitive.ARGUMENT)) {
id = dataStream.readLong();
while (id > 0) {
element = network.findById(id);
if (element != null) {
expression.addRelationship(Primitive.ARGUMENT, element, Integer.MAX_VALUE);
}
id = dataStream.readLong();
}
id = dataStream.readLong();
}
return expression;
}
HprofHeap(DataInputStream dis, CacheDirectory cacheDir) throws IOException {
if (cacheDir.isDirty()) {
throw new IOException("Dirty cache "+cacheDir);
}
String id = dis.readUTF();
if (!SNAPSHOT_ID.equals(id)) {
throw new IOException("Invalid HPROF dump id "+id);
}
int version = dis.readInt();
if (version != SNAPSHOT_VERSION) {
throw new IOException("Invalid HPROF version "+SNAPSHOT_VERSION+" loaded "+version);
}
heapDumpFile = cacheDir.getHeapFile(dis.readUTF());
cacheDirectory = cacheDir;
dumpBuffer = HprofByteBuffer.createHprofByteBuffer(heapDumpFile);
long time = dis.readLong();
if (time != dumpBuffer.getTime()) {
throw new IOException("HPROF time mismatch. Cached "+time+" from heap dump "+dumpBuffer.getTime());
}
String os = dis.readUTF();
if (!os.equals(System.getProperty(OS_PROP))) {
throw new IOException("HPROF OS mismatch. Cached "+os+" current OS "+System.getProperty(OS_PROP));
}
nearestGCRoot = new NearestGCRoot(this, dis);
allInstanceDumpBounds = new TagBounds(dis);
heapDumpSegment = new TagBounds(dis);
heapTagBounds = new TagBounds[0x100];
TagBounds.readFromStream(dis, this, heapTagBounds);
TagBounds.readFromStream(dis, this, tagBounds);
instancesCountComputed = dis.readBoolean();
referencesComputed = dis.readBoolean();
retainedSizeComputed = dis.readBoolean();
retainedSizeByClassComputed = dis.readBoolean();
idMapSize = dis.readInt();
segment = dis.readInt();
idToOffsetMap = new LongMap(dis, cacheDirectory);
if (dis.readBoolean()) {
domTree = new DominatorTree(this, dis);
}
gcRoots = new HprofGCRoots(this);
getClassDumpSegment().extractSpecialClasses();
}
/**
* Customizes a class file by replacing constant pools.
*
* @param image
* The image of the template class.
* @param replacements
* A list of pair of strings that specify the substitution
* {@code String[]{search_0, replace_0, search_1, replace_1, ..., search_n, replace_n }}
*
* The search strings found in the constant pool will be replaced by the corresponding
* replacement string.
*/
public static byte[] tailor( InputStream image, String templateClassName, String newClassName, String... replacements ) {
DataInputStream in = new DataInputStream(image);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
DataOutputStream out = new DataOutputStream(baos);
// skip until the constant pool count
long l = in.readLong();
out.writeLong(l);
// read the constant pool size
short count = in.readShort();
out.writeShort(count);
// replace constant pools
for( int i=0; i<count; i++ ) {
byte tag = in.readByte();
out.writeByte(tag);
switch(tag) {
case 0:
// this isn't described in the spec,
// but class files often seem to have this '0' tag.
// we can apparently just ignore it, but not sure
// what this really means.
break;
case 1: // CONSTANT_UTF8
{
String value = in.readUTF();
if(value.equals(templateClassName))
value = newClassName;
else {
for( int j=0; j<replacements.length; j+=2 )
if(value.equals(replacements[j])) {
value = replacements[j+1];
break;
}
}
out.writeUTF(value);
}
break;
case 3: // CONSTANT_Integer
case 4: // CONSTANT_Float
out.writeInt(in.readInt());
break;
case 5: // CONSTANT_Long
case 6: // CONSTANT_Double
i++; // doubles and longs take two entries
out.writeLong(in.readLong());
break;
case 7: // CONSTANT_Class
case 8: // CONSTANT_String
out.writeShort(in.readShort());
break;
case 9: // CONSTANT_Fieldref
case 10: // CONSTANT_Methodref
case 11: // CONSTANT_InterfaceMethodref
case 12: // CONSTANT_NameAndType
out.writeInt(in.readInt());
break;
default:
throw new IllegalArgumentException("Unknown constant type "+tag);
}
}
// then copy the rest
byte[] buf = new byte[512];
int len;
while((len=in.read(buf))>0)
out.write(buf,0,len);
in.close();
out.close();
// by now we got the properly tailored class file image
return baos.toByteArray();
} catch( IOException e ) {
// never happen
logger.log(Level.WARNING,"failed to tailor",e);
return null;
}
}