private void writeConsoleNotes(PrintWriter writer, int precision) throws IOException { DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(build.getLogInputStream())); boolean threw = true; try { while (true) { dataInputStream.mark(1); int currentByte = dataInputStream.read(); if (currentByte == -1) { threw = false; return; } if (currentByte == ConsoleNote.PREAMBLE[0]) { dataInputStream.reset(); ConsoleNote<?> consoleNote; try { consoleNote = ConsoleNote.readFrom(dataInputStream); } catch (ClassNotFoundException ex) { // Unknown console note. Ignore. continue; } if (consoleNote instanceof TimestampNote) { TimestampNote timestampNote = (TimestampNote) consoleNote; Timestamp timestamp = timestampNote.getTimestamp(build); writer.write(formatTimestamp(timestamp, precision)); } } } } finally { Closeables.close(dataInputStream, threw); } }
/** * Parses an HRegionInfo instance from the passed in stream. Presumes the HRegionInfo was * serialized to the stream with {@link #toDelimitedByteArray()} * * @param in * @return An instance of HRegionInfo. * @throws IOException */ public static HRegionInfo parseFrom(final DataInputStream in) throws IOException { // I need to be able to move back in the stream if this is not a pb serialization so I can // do the Writable decoding instead. int pblen = ProtobufUtil.lengthOfPBMagic(); byte[] pbuf = new byte[pblen]; if (in.markSupported()) { // read it with mark() in.mark(pblen); } int read = in.read(pbuf); // assumption: if Writable serialization, it should be longer than pblen. if (read != pblen) throw new IOException("read=" + read + ", wanted=" + pblen); if (ProtobufUtil.isPBMagicPrefix(pbuf)) { return convert(HBaseProtos.RegionInfo.parseDelimitedFrom(in)); } else { // Presume Writables. Need to reset the stream since it didn't start w/ pb. if (in.markSupported()) { in.reset(); HRegionInfo hri = new HRegionInfo(); hri.readFields(in); return hri; } else { // we cannot use BufferedInputStream, it consumes more than we read from the underlying IS ByteArrayInputStream bais = new ByteArrayInputStream(pbuf); SequenceInputStream sis = new SequenceInputStream(bais, in); // concatenate input streams HRegionInfo hri = new HRegionInfo(); hri.readFields(new DataInputStream(sis)); return hri; } } }
/** * Populate this instance with what we find on the passed in <code>in</code> stream. Can * deserialize protobuf of old Writables format. * * @param in * @throws IOException * @see #write(DataOutputStream) */ void read(final DataInputStream in) throws IOException { // This code is tested over in TestHFileReaderV1 where we read an old hfile w/ this new code. int pblen = ProtobufUtil.lengthOfPBMagic(); byte[] pbuf = new byte[pblen]; if (in.markSupported()) in.mark(pblen); int read = in.read(pbuf); if (read != pblen) throw new IOException("read=" + read + ", wanted=" + pblen); if (ProtobufUtil.isPBMagicPrefix(pbuf)) { parsePB(HFileProtos.FileInfoProto.parseDelimitedFrom(in)); } else { if (in.markSupported()) { in.reset(); parseWritable(in); } else { // We cannot use BufferedInputStream, it consumes more than we read from the underlying IS ByteArrayInputStream bais = new ByteArrayInputStream(pbuf); SequenceInputStream sis = new SequenceInputStream(bais, in); // Concatenate input streams // TODO: Am I leaking anything here wrapping the passed in stream? We are not calling // close on the wrapped // streams but they should be let go after we leave this context? I see that we keep a // reference to the // passed in inputstream but since we no longer have a reference to this after we leave, // we should be ok. parseWritable(new DataInputStream(sis)); } } }
/** * Check an fsimage datainputstream's version number. * * <p>The datainput stream is returned at the same point as it was passed in; this method has no * effect on the datainputstream's read pointer. * * @param in Datainputstream of fsimage * @return Filesystem layout version of fsimage represented by stream * @throws IOException If problem reading from in */ private int findImageVersion(DataInputStream in) throws IOException { in.mark(42); // arbitrary amount, resetting immediately int version = in.readInt(); in.reset(); return version; }
/** * Determines from the magic number whether the given InputStream points to an SGI RGB image. The * given InputStream must return true from markSupported() and support a minimum of two bytes of * read-ahead. */ public static boolean isSGIImage(InputStream in) throws IOException { if (!(in instanceof BufferedInputStream)) { in = new BufferedInputStream(in); } if (!in.markSupported()) { throw new IOException( "Can not test non-destructively whether given InputStream is an SGI RGB image"); } final DataInputStream dIn = new DataInputStream(in); dIn.mark(4); final short magic = dIn.readShort(); dIn.reset(); return (magic == MAGIC); }
@Override public SearchTerm<byte[]> nextTerm() throws IOException { inStream.mark(1); final int nextByte = inStream.read(); if (nextByte == -1) { return null; } inStream.reset(); final int termLength = inStream.readInt(); final byte[] term = new byte[termLength]; inStream.readFully(term); return new SearchTerm<>(term); }
/** Creates new ScriptTable */ protected Script(DataInputStream dis, int offset) throws IOException { // Ensure we're in the right place dis.reset(); dis.skipBytes(offset); // Start reading _defaultLangSysOffset = dis.readUnsignedShort(); _langSysCount = dis.readUnsignedShort(); if (_langSysCount > 0) { _langSysRecords = new LangSysRecord[_langSysCount]; for (int i = 0; i < _langSysCount; i++) { _langSysRecords[i] = new LangSysRecord(dis); } } // Read the LangSys tables if (_langSysCount > 0) { _langSys = new LangSys[_langSysCount]; for (int i = 0; i < _langSysCount; i++) { dis.reset(); dis.skipBytes(offset + _langSysRecords[i].getOffset()); _langSys[i] = new LangSys(dis); } } if (_defaultLangSysOffset > 0) { dis.reset(); dis.skipBytes(offset + _defaultLangSysOffset); _defaultLangSys = new LangSys(dis); } }
public static LigatureSubst read(DataInputStream dis, int offset) throws IOException { dis.reset(); dis.skipBytes(offset); int format = dis.readUnsignedShort(); if (format == 1) { return new LigatureSubstFormat1(dis, offset); } return null; }
int loadFSEdits(DataInputStream in, boolean closeOnExit) throws IOException { int numEdits = 0; int logVersion = 0; try { // Read log file version. Could be missing. in.mark(4); // If edits log is greater than 2G, available method will return negative // numbers, so we avoid having to call available boolean available = true; try { logVersion = in.readByte(); } catch (EOFException e) { available = false; } if (available) { in.reset(); logVersion = in.readInt(); if (logVersion < FSConstants.LAYOUT_VERSION) // future version throw new IOException( "Unexpected version of the file system log file: " + logVersion + ". Current version = " + FSConstants.LAYOUT_VERSION + "."); } assert logVersion <= Storage.LAST_UPGRADABLE_LAYOUT_VERSION : "Unsupported version " + logVersion; numEdits = loadEditRecords(logVersion, in, false); } finally { if (closeOnExit) in.close(); } if (logVersion != FSConstants.LAYOUT_VERSION) // other version numEdits++; // save this image asap return numEdits; }
/** modified by tony */ @SuppressWarnings("deprecation") int loadEditRecords(int logVersion, DataInputStream in, boolean closeOnExit) throws IOException { FSNamesystem.LOG.info("logversion: " + logVersion); FSDirectory fsDir = fsNamesys.dir; int numEdits = 0; String clientName = null; String clientMachine = null; String path = null; int numOpAdd = 0, numOpClose = 0, numOpDelete = 0, numOpRenameOld = 0, numOpSetRepl = 0, numOpMkDir = 0, numOpSetPerm = 0, numOpSetOwner = 0, numOpSetGenStamp = 0, numOpTimes = 0, numOpRename = 0, numOpConcatDelete = 0, numOpSymlink = 0, numOpGetDelegationToken = 0, numOpRenewDelegationToken = 0, numOpCancelDelegationToken = 0, numOpUpdateMasterKey = 0, numOpOther = 0; try { while (true) { long timestamp = 0; long mtime = 0; long atime = 0; long blockSize = 0; byte opcode = -1; try { in.mark(1); opcode = in.readByte(); if (opcode == Ops.OP_INVALID) { in.reset(); // reset back to end of file if somebody reads it again break; // no more transactions } } catch (EOFException e) { break; // no more transactions } numEdits++; switch (opcode) { case Ops.OP_ADD: case Ops.OP_CLOSE: { // versions > 0 support per file replication // get name and replication int length = in.readInt(); // modified by tony if (-7 == logVersion && length != 3 || -17 < logVersion && logVersion < -7 && length != 4 || logVersion <= -17 && length != 7) { throw new IOException( "Incorrect data format." + " logVersion is " + logVersion + " but writables.length is " + length + ". "); } path = FSImageSerialization.readString(in); short replication = fsNamesys.adjustReplication(readShort(in)); mtime = readLong(in); if (LayoutVersion.supports(Feature.FILE_ACCESS_TIME, logVersion)) { atime = readLong(in); } if (logVersion < -7) { blockSize = readLong(in); } long fileSize = readLong(in); byte type = (byte) readLong(in); // get blocks boolean isFileUnderConstruction = (opcode == Ops.OP_ADD); BlockInfo blocks[] = readBlocks(in, logVersion, isFileUnderConstruction, replication); // Older versions of HDFS does not store the block size in inode. // If the file has more than one block, use the size of the // first block as the blocksize. Otherwise use the default // block size. if (-8 <= logVersion && blockSize == 0) { if (blocks.length > 1) { blockSize = blocks[0].getNumBytes(); } else { long first = ((blocks.length == 1) ? blocks[0].getNumBytes() : 0); blockSize = Math.max(fsNamesys.getDefaultBlockSize(), first); } } PermissionStatus permissions = fsNamesys.getUpgradePermission(); if (logVersion <= -11) { permissions = PermissionStatus.read(in); } CodingMatrix codingMatrix = CodingMatrix.getMatrixofCertainType(type); codingMatrix.readFields(in); /** added by tony* */ LongWritable offset = new LongWritable(); offset.readFields(in); long headeroffset = offset.get(); // clientname, clientMachine and block locations of last block. if (opcode == Ops.OP_ADD && logVersion <= -12) { clientName = FSImageSerialization.readString(in); clientMachine = FSImageSerialization.readString(in); if (-13 <= logVersion) { readDatanodeDescriptorArray(in); } } else { clientName = ""; clientMachine = ""; } // The open lease transaction re-creates a file if necessary. // Delete the file if it already exists. if (FSNamesystem.LOG.isDebugEnabled()) { FSNamesystem.LOG.debug( opcode + ": " + path + " numblocks : " + blocks.length + " clientHolder " + clientName + " clientMachine " + clientMachine); } fsDir.unprotectedDelete(path, mtime); /** modified by tony add to the file tree */ INodeFile node = (INodeFile) fsDir.unprotectedAddFile( path, permissions, codingMatrix, headeroffset, fileSize, blocks, replication, mtime, atime, blockSize); if (isFileUnderConstruction) { numOpAdd++; // // Replace current node with a INodeUnderConstruction. // Recreate in-memory lease record. // // INodeFileUnderConstruction cons = new INodeFileUnderConstruction( // node.getLocalNameBytes(), // node.getReplication(), // node.getModificationTime(), // node.getPreferredBlockSize(), // node.getBlocks(), // node.getPermissionStatus(), // clientName, // clientMachine, // null); // TODO: INodeFileUnderConstruction cons = null; fsDir.replaceNode(path, node, cons); fsNamesys.leaseManager.addLease(cons.getClientName(), path); } break; } case Ops.OP_SET_REPLICATION: { numOpSetRepl++; path = FSImageSerialization.readString(in); short replication = fsNamesys.adjustReplication(readShort(in)); fsDir.unprotectedSetReplication(path, replication, null); break; } case Ops.OP_CONCAT_DELETE: { numOpConcatDelete++; int length = in.readInt(); if (length < 3) { // trg, srcs.., timestam throw new IOException("Incorrect data format. " + "Mkdir operation."); } String trg = FSImageSerialization.readString(in); int srcSize = length - 1 - 1; // trg and timestamp String[] srcs = new String[srcSize]; for (int i = 0; i < srcSize; i++) { srcs[i] = FSImageSerialization.readString(in); } timestamp = readLong(in); fsDir.unprotectedConcat(trg, srcs); break; } case Ops.OP_RENAME_OLD: { numOpRenameOld++; int length = in.readInt(); if (length != 3) { throw new IOException("Incorrect data format. " + "Mkdir operation."); } String s = FSImageSerialization.readString(in); String d = FSImageSerialization.readString(in); timestamp = readLong(in); HdfsFileStatus dinfo = fsDir.getFileInfo(d, false); fsDir.unprotectedRenameTo(s, d, timestamp); fsNamesys.changeLease(s, d, dinfo); break; } case Ops.OP_DELETE: { numOpDelete++; int length = in.readInt(); if (length != 2) { throw new IOException("Incorrect data format. " + "delete operation."); } path = FSImageSerialization.readString(in); timestamp = readLong(in); fsDir.unprotectedDelete(path, timestamp); break; } case Ops.OP_MKDIR: { numOpMkDir++; PermissionStatus permissions = fsNamesys.getUpgradePermission(); int length = in.readInt(); if (-17 < logVersion && length != 2 || logVersion <= -17 && length != 3) { throw new IOException("Incorrect data format. " + "Mkdir operation."); } path = FSImageSerialization.readString(in); timestamp = readLong(in); // The disk format stores atimes for directories as well. // However, currently this is not being updated/used because of // performance reasons. if (LayoutVersion.supports(Feature.FILE_ACCESS_TIME, logVersion)) { atime = readLong(in); } if (logVersion <= -11) { permissions = PermissionStatus.read(in); } fsDir.unprotectedMkdir(path, permissions, timestamp); break; } case Ops.OP_SET_GENSTAMP: { numOpSetGenStamp++; long lw = in.readLong(); fsNamesys.setGenerationStamp(lw); break; } case Ops.OP_DATANODE_ADD: { numOpOther++; // Datanodes are not persistent any more. FSImageSerialization.DatanodeImage.skipOne(in); break; } case Ops.OP_DATANODE_REMOVE: { numOpOther++; DatanodeID nodeID = new DatanodeID(); nodeID.readFields(in); // Datanodes are not persistent any more. break; } case Ops.OP_SET_PERMISSIONS: { numOpSetPerm++; fsDir.unprotectedSetPermission( FSImageSerialization.readString(in), FsPermission.read(in)); break; } case Ops.OP_SET_OWNER: { numOpSetOwner++; fsDir.unprotectedSetOwner( FSImageSerialization.readString(in), FSImageSerialization.readString_EmptyAsNull(in), FSImageSerialization.readString_EmptyAsNull(in)); break; } case Ops.OP_SET_NS_QUOTA: { fsDir.unprotectedSetQuota( FSImageSerialization.readString(in), readLongWritable(in), FSConstants.QUOTA_DONT_SET); break; } case Ops.OP_CLEAR_NS_QUOTA: { fsDir.unprotectedSetQuota( FSImageSerialization.readString(in), FSConstants.QUOTA_RESET, FSConstants.QUOTA_DONT_SET); break; } case Ops.OP_SET_QUOTA: fsDir.unprotectedSetQuota( FSImageSerialization.readString(in), readLongWritable(in), readLongWritable(in)); break; case Ops.OP_TIMES: { numOpTimes++; int length = in.readInt(); if (length != 3) { throw new IOException("Incorrect data format. " + "times operation."); } path = FSImageSerialization.readString(in); mtime = readLong(in); atime = readLong(in); fsDir.unprotectedSetTimes(path, mtime, atime, true); break; } case Ops.OP_SYMLINK: { numOpSymlink++; int length = in.readInt(); if (length != 4) { throw new IOException("Incorrect data format. " + "symlink operation."); } path = FSImageSerialization.readString(in); String value = FSImageSerialization.readString(in); mtime = readLong(in); atime = readLong(in); PermissionStatus perm = PermissionStatus.read(in); fsDir.unprotectedSymlink(path, value, mtime, atime, perm); break; } case Ops.OP_RENAME: { numOpRename++; int length = in.readInt(); if (length != 3) { throw new IOException("Incorrect data format. " + "Mkdir operation."); } String s = FSImageSerialization.readString(in); String d = FSImageSerialization.readString(in); timestamp = readLong(in); Rename[] options = readRenameOptions(in); HdfsFileStatus dinfo = fsDir.getFileInfo(d, false); fsDir.unprotectedRenameTo(s, d, timestamp, options); fsNamesys.changeLease(s, d, dinfo); break; } case Ops.OP_GET_DELEGATION_TOKEN: { numOpGetDelegationToken++; DelegationTokenIdentifier delegationTokenId = new DelegationTokenIdentifier(); delegationTokenId.readFields(in); long expiryTime = readLong(in); fsNamesys .getDelegationTokenSecretManager() .addPersistedDelegationToken(delegationTokenId, expiryTime); break; } case Ops.OP_RENEW_DELEGATION_TOKEN: { numOpRenewDelegationToken++; DelegationTokenIdentifier delegationTokenId = new DelegationTokenIdentifier(); delegationTokenId.readFields(in); long expiryTime = readLong(in); fsNamesys .getDelegationTokenSecretManager() .updatePersistedTokenRenewal(delegationTokenId, expiryTime); break; } case Ops.OP_CANCEL_DELEGATION_TOKEN: { numOpCancelDelegationToken++; DelegationTokenIdentifier delegationTokenId = new DelegationTokenIdentifier(); delegationTokenId.readFields(in); fsNamesys .getDelegationTokenSecretManager() .updatePersistedTokenCancellation(delegationTokenId); break; } case Ops.OP_UPDATE_MASTER_KEY: { numOpUpdateMasterKey++; DelegationKey delegationKey = new DelegationKey(); delegationKey.readFields(in); fsNamesys.getDelegationTokenSecretManager().updatePersistedMasterKey(delegationKey); break; } default: { throw new IOException("Never seen opcode " + opcode); } } } } catch (IOException ex) { check203UpgradeFailure(logVersion, ex); } finally { if (closeOnExit) in.close(); } if (FSImage.LOG.isDebugEnabled()) { FSImage.LOG.debug( "numOpAdd = " + numOpAdd + " numOpClose = " + numOpClose + " numOpDelete = " + numOpDelete + " numOpRenameOld = " + numOpRenameOld + " numOpSetRepl = " + numOpSetRepl + " numOpMkDir = " + numOpMkDir + " numOpSetPerm = " + numOpSetPerm + " numOpSetOwner = " + numOpSetOwner + " numOpSetGenStamp = " + numOpSetGenStamp + " numOpTimes = " + numOpTimes + " numOpConcatDelete = " + numOpConcatDelete + " numOpRename = " + numOpRename + " numOpGetDelegationToken = " + numOpGetDelegationToken + " numOpRenewDelegationToken = " + numOpRenewDelegationToken + " numOpCancelDelegationToken = " + numOpCancelDelegationToken + " numOpUpdateMasterKey = " + numOpUpdateMasterKey + " numOpOther = " + numOpOther); } return numEdits; }
public static Object3D[] load(byte[] data, int offset) { DataInputStream old = dis; dis = new DataInputStream(new ByteArrayInputStream(data)); try { while (dis.available() > 0) { int objectType = readByte(); int length = readInt(); System.out.println("objectType: " + objectType); System.out.println("length: " + length); dis.mark(Integer.MAX_VALUE); if (objectType == 0) { int versionHigh = readByte(); int versionLow = readByte(); boolean hasExternalReferences = readBoolean(); int totolFileSize = readInt(); int approximateContentSize = readInt(); String authoringField = readString(); objs.addElement(new Group()); // dummy } else if (objectType == 255) { // TODO: load external resource System.out.println("Loader: Loading external resources not implemented."); String uri = readString(); } else if (objectType == 1) { System.out.println("Loader: AnimationController not implemented."); objs.addElement(new Group()); // dummy } else if (objectType == 2) { System.out.println("Loader: AnimationTrack not implemented."); objs.addElement(new Group()); // dummy } else if (objectType == 3) { // System.out.println("Appearance"); Appearance appearance = new Appearance(); loadObject3D(appearance); appearance.setLayer(readByte()); appearance.setCompositingMode((CompositingMode) getObject(readInt())); appearance.setFog((Fog) getObject(readInt())); appearance.setPolygonMode((PolygonMode) getObject(readInt())); appearance.setMaterial((Material) getObject(readInt())); int numTextures = readInt(); for (int i = 0; i < numTextures; ++i) appearance.setTexture(i, (Texture2D) getObject(readInt())); objs.addElement(appearance); } else if (objectType == 4) { // System.out.println("Background"); Background background = new Background(); loadObject3D(background); background.setColor(readRGBA()); background.setImage((Image2D) getObject(readInt())); int modeX = readByte(); int modeY = readByte(); background.setImageMode(modeX, modeY); int cropX = readInt(); int cropY = readInt(); int cropWidth = readInt(); int cropHeight = readInt(); background.setCrop(cropX, cropY, cropWidth, cropHeight); background.setDepthClearEnable(readBoolean()); background.setColorClearEnable(readBoolean()); objs.addElement(background); // dummy } else if (objectType == 5) { // System.out.println("Camera"); Camera camera = new Camera(); loadNode(camera); int projectionType = readByte(); if (projectionType == Camera.GENERIC) { Transform t = new Transform(); t.set(readMatrix()); camera.setGeneric(t); } else { float fovy = readFloat(); float aspect = readFloat(); float near = readFloat(); float far = readFloat(); if (projectionType == Camera.PARALLEL) camera.setParallel(fovy, aspect, near, far); else camera.setPerspective(fovy, aspect, near, far); } objs.addElement(camera); } else if (objectType == 6) { // System.out.println("CompositingMode"); CompositingMode compositingMode = new CompositingMode(); loadObject3D(compositingMode); compositingMode.setDepthTestEnabled(readBoolean()); compositingMode.setDepthWriteEnabled(readBoolean()); compositingMode.setColorWriteEnabled(readBoolean()); compositingMode.setAlphaWriteEnabled(readBoolean()); compositingMode.setBlending(readByte()); compositingMode.setAlphaThreshold((float) readByte() / 255.0f); compositingMode.setDepthOffsetFactor(readFloat()); compositingMode.setDepthOffsetUnits(readFloat()); objs.addElement(compositingMode); } else if (objectType == 7) { // System.out.println("Fog"); Fog fog = new Fog(); loadObject3D(fog); fog.setColor(readRGB()); fog.setMode(readByte()); if (fog.getMode() == Fog.EXPONENTIAL) fog.setDensity(readFloat()); else { fog.setNearDistance(readFloat()); fog.setFarDistance(readFloat()); } objs.addElement(fog); } else if (objectType == 9) { // System.out.println("Group"); Group group = new Group(); loadGroup(group); objs.addElement(group); } else if (objectType == 10) { // System.out.println("Image2D"); Image2D image = null; loadObject3D(new Group()); // dummy int format = readByte(); boolean isMutable = readBoolean(); int width = readInt(); int height = readInt(); if (!isMutable) { // Read palette int paletteSize = readInt(); byte[] palette = null; if (paletteSize > 0) { palette = new byte[paletteSize]; dis.readFully(palette); } // Read pixels int pixelSize = readInt(); byte[] pixel = new byte[pixelSize]; dis.readFully(pixel); // Create image if (palette != null) image = new Image2D(format, width, height, pixel, palette); else image = new Image2D(format, width, height, pixel); } else image = new Image2D(format, width, height); dis.reset(); loadObject3D(image); objs.addElement(image); } else if (objectType == 19) { System.out.println("Loader: KeyframeSequence not implemented."); /* Byte interpolation; Byte repeatMode; Byte encoding; UInt32 duration; UInt32 validRangeFirst; UInt32 validRangeLast; UInt32 componentCount; UInt32 keyframeCount; IF encoding == 0 FOR each key frame... UInt32 time; Float32[componentCount] vectorValue; END ELSE IF encoding == 1 Float32[componentCount] vectorBias; Float32[componentCount] vectorScale; FOR each key frame... UInt32 time; Byte[componentCount] vectorValue; END ELSE IF encoding == 2 Float32[componentCount] vectorBias; Float32[componentCount] vectorScale; FOR each key frame... UInt32 time; UInt16[componentCount] vectorValue; END END */ objs.addElement(new Group()); // dummy } else if (objectType == 12) { // System.out.println("Light"); Light light = new Light(); loadNode(light); float constant = readFloat(); float linear = readFloat(); float quadratic = readFloat(); light.setAttenuation(constant, linear, quadratic); light.setColor(readRGB()); light.setMode(readByte()); light.setIntensity(readFloat()); light.setSpotAngle(readFloat()); light.setSpotExponent(readFloat()); objs.addElement(light); } else if (objectType == 13) { // System.out.println("Material"); Material material = new Material(); loadObject3D(material); material.setColor(Material.AMBIENT, readRGB()); material.setColor(Material.DIFFUSE, readRGBA()); material.setColor(Material.EMISSIVE, readRGB()); material.setColor(Material.SPECULAR, readRGB()); material.setShininess(readFloat()); material.setVertexColorTrackingEnable(readBoolean()); objs.addElement(material); } else if (objectType == 14) { // System.out.println("Mesh"); loadNode(new Group()); // dummy VertexBuffer vertices = (VertexBuffer) getObject(readInt()); int submeshCount = readInt(); IndexBuffer[] submeshes = new IndexBuffer[submeshCount]; Appearance[] appearances = new Appearance[submeshCount]; for (int i = 0; i < submeshCount; ++i) { submeshes[i] = (IndexBuffer) getObject(readInt()); appearances[i] = (Appearance) getObject(readInt()); } Mesh mesh = new Mesh(vertices, submeshes, appearances); dis.reset(); loadNode(mesh); objs.addElement(mesh); } else if (objectType == 15) { System.out.println("Loader: MorphingMesh not implemented."); /* UInt32 morphTargetCount; FOR each target buffer... ObjectIndex morphTarget; Float32 initialWeight; END */ objs.addElement(new Group()); // dummy } else if (objectType == 8) { // System.out.println("PolygonMode"); PolygonMode polygonMode = new PolygonMode(); loadObject3D(polygonMode); polygonMode.setCulling(readByte()); polygonMode.setShading(readByte()); polygonMode.setWinding(readByte()); polygonMode.setTwoSidedLightingEnable(readBoolean()); polygonMode.setLocalCameraLightingEnable(readBoolean()); polygonMode.setPerspectiveCorrectionEnable(readBoolean()); objs.addElement(polygonMode); } else if (objectType == 16) { System.out.println("Loader: SkinnedMesh not implemented."); /* ObjectIndex skeleton; UInt32 transformReferenceCount; FOR each bone reference... ObjectIndex transformNode; UInt32 firstVertex; UInt32 vertexCount; Int32 weight; END */ objs.addElement(new Group()); // dummy } else if (objectType == 18) { System.out.println("Loader: Sprite not implemented."); /* ObjectIndex image; ObjectIndex appearance; Boolean isScaled; Int32 cropX; Int32 cropY; Int32 cropWidth; Int32 cropHeight; */ objs.addElement(new Group()); // dummy } else if (objectType == 17) { // System.out.println("Texture2D"); loadTransformable(new Group()); // dummy Texture2D texture = new Texture2D((Image2D) getObject(readInt())); texture.setBlendColor(readRGB()); texture.setBlending(readByte()); int wrapS = readByte(); int wrapT = readByte(); texture.setWrapping(wrapS, wrapT); int levelFilter = readByte(); int imageFilter = readByte(); texture.setFiltering(levelFilter, imageFilter); dis.reset(); loadTransformable(texture); objs.addElement(texture); } else if (objectType == 11) { // System.out.println("TriangleStripArray"); loadObject3D(new Group()); // dummy int encoding = readByte(); int firstIndex = 0; int[] indices = null; if (encoding == 0) firstIndex = readInt(); else if (encoding == 1) firstIndex = readByte(); else if (encoding == 2) firstIndex = readShort(); else if (encoding == 128) { int numIndices = readInt(); indices = new int[numIndices]; for (int i = 0; i < numIndices; ++i) indices[i] = readInt(); } else if (encoding == 129) { int numIndices = readInt(); indices = new int[numIndices]; for (int i = 0; i < numIndices; ++i) indices[i] = readByte(); } else if (encoding == 130) { int numIndices = readInt(); indices = new int[numIndices]; for (int i = 0; i < numIndices; ++i) indices[i] = readShort(); } int numStripLengths = readInt(); int[] stripLengths = new int[numStripLengths]; for (int i = 0; i < numStripLengths; ++i) stripLengths[i] = readInt(); dis.reset(); TriangleStripArray triStrip = null; if (indices == null) triStrip = new TriangleStripArray(firstIndex, stripLengths); else triStrip = new TriangleStripArray(indices, stripLengths); loadObject3D(triStrip); objs.addElement(triStrip); } else if (objectType == 20) { // System.out.println("VertexArray"); loadObject3D(new Group()); // dummy int componentSize = readByte(); int componentCount = readByte(); int encoding = readByte(); int vertexCount = readShort(); VertexArray vertices = new VertexArray(vertexCount, componentCount, componentSize); if (componentSize == 1) { byte[] values = new byte[componentCount * vertexCount]; if (encoding == 0) dis.readFully(values); else { byte last = 0; for (int i = 0; i < vertexCount * componentCount; ++i) { last += readByte(); values[i] = last; } } vertices.set(0, vertexCount, values); } else { short last = 0; short[] values = new short[componentCount * vertexCount]; for (int i = 0; i < componentCount * vertexCount; ++i) { if (encoding == 0) values[i] = (short) readShort(); else { last += (short) readShort(); values[i] = last; } } vertices.set(0, vertexCount, values); } dis.reset(); loadObject3D(vertices); objs.addElement(vertices); } else if (objectType == 21) { // System.out.println("VertexBuffer"); VertexBuffer vertices = new VertexBuffer(); loadObject3D(vertices); vertices.setDefaultColor(readRGBA()); VertexArray positions = (VertexArray) getObject(readInt()); float[] bias = new float[3]; bias[0] = readFloat(); bias[1] = readFloat(); bias[2] = readFloat(); float scale = readFloat(); vertices.setPositions(positions, scale, bias); vertices.setNormals((VertexArray) getObject(readInt())); vertices.setColors((VertexArray) getObject(readInt())); int texCoordArrayCount = readInt(); for (int i = 0; i < texCoordArrayCount; ++i) { VertexArray texcoords = (VertexArray) getObject(readInt()); bias[0] = readFloat(); bias[1] = readFloat(); bias[2] = readFloat(); scale = readFloat(); vertices.setTexCoords(i, texcoords, scale, bias); } objs.addElement(vertices); } else if (objectType == 22) { // System.out.println("World"); World world = new World(); loadGroup(world); world.setActiveCamera((Camera) getObject(readInt())); world.setBackground((Background) getObject(readInt())); objs.addElement(world); } else { System.out.println("Loader: unsupported objectType " + objectType + "."); } dis.reset(); dis.skipBytes(length); } } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); e.printStackTrace(); } dis = old; return null; }
/** * Reads a MIDI track chunk * * @param DataInputStream dis - the input stream to read from * @exception IOException */ private void readTrackChunk(DataInputStream dis) throws IOException { // local variables for Track class Track track = new Track(); // Insert new Track into a list of tracks this.trackList.addElement(track); int deltaTime = 0; if (VERBOSE) System.out.println("Reading Track .........."); // Read track header if (dis.readInt() != 0x4D54726B) { // If MTrk read is wrong throw new IOException("Track started in wrong place!!!! ABORTING"); } else { // If MTrk read ok get bytesRemaining dis.readInt(); } // loop variables int status, oldStatus = 0, eventLength = 0; // Start gathering event data Event event = null; while (true) { try { // get variable length timestamp deltaTime = MidiUtil.readVarLength(dis); // mark stream so we can return if we need running status dis.mark(2); status = dis.readUnsignedByte(); // decide on running status if (status < 0x80) { // set running status status = oldStatus; // return stream to before status read dis.reset(); } // create default event of correct type if (status >= 0xFF) { // Meta Event int type = dis.readUnsignedByte(); eventLength = MidiUtil.readVarLength(dis); event = MidiUtil.createMetaEvent(type); } else if (status >= 0xF0) { // System Exclusive --- NOT SUPPORTED System.out.println("SysEX---"); eventLength = MidiUtil.readVarLength(dis); } else if (status >= 0x80) { // MIDI voice event short selection = (short) (status / 0x10); short midiChannel = (short) (status - (selection * 0x10)); VoiceEvt evt = (VoiceEvt) MidiUtil.createVoiceEvent(selection); evt.setMidiChannel(midiChannel); event = evt; if (event == null) { throw new IOException("MIDI file read error: invalid voice event type!"); } } oldStatus = status; } catch (Exception e) { e.printStackTrace(); System.exit(1); } if (event != null) { // read data into the new event and // add the new event to the Track object event.setTime(deltaTime); event.read(dis); // if (VERBOSE) event.print(); track.addEvent(event); // event.print(); if (event instanceof EndTrack) break; } else { // skip the stream ahead to next valid event dis.skipBytes(eventLength); } } }
@Override public void reset() throws IOException { dataInput.reset(); }
@Override public void reset() throws IOException { in.reset(); }