📄 compressedgeometryfile.java
字号:
/* * $RCSfile: CompressedGeometryFile.java,v $ * * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or * maintenance of any nuclear facility. * * $Revision: 1.5 $ * $Date: 2007/02/09 17:20:16 $ * $State: Exp $ */package com.sun.j3d.utils.compression;import java.io.FileNotFoundException;import java.io.IOException;import java.io.RandomAccessFile;import javax.media.j3d.CompressedGeometry;import javax.media.j3d.CompressedGeometryHeader;//// The compressed geometry file format supported by this class has a 32// byte header followed by multiple compressed geometry objects.//// Each object consists of a block of compressed data and an 8-byte// individual block header describing its contents.//// The file ends with a directory data structure used for random access,// containing a 64-bit offset for each object in the order in which it// appears in the file. This is also used to find the size of the largest// object in the file and must be present.///** * This class provides methods to read and write compressed geometry resource * files. These files usually end with the .cg extension and support * sequential as well as random access to multiple compressed geometry * objects. * * @deprecated As of Java 3D 1.5, replaced by * com.sun.j3d.utils.geometry.compression.{@link com.sun.j3d.utils.geometry.compression.CompressedGeometryFile}. */public class CompressedGeometryFile { private static final boolean print = false ; private static final boolean benchmark = false ; /** * The magic number which identifies the compressed geometry file type. */ static final int MAGIC_NUMBER = 0xbaddfab4 ; /** * Byte offset of the magic number from start of file. */ static final int MAGIC_NUMBER_OFFSET = 0 ; /** * Byte offset of the major version number from start of file. */ static final int MAJOR_VERSION_OFFSET = 4 ; /** * Byte offset of the minor version number from start of file. */ static final int MINOR_VERSION_OFFSET = 8 ; /** * Byte offset of the minor minor version number from start of file. */ static final int MINOR_MINOR_VERSION_OFFSET = 12 ; /** * Byte offset of the number of objects from start of file. */ static final int OBJECT_COUNT_OFFSET = 16 ; /** * Byte offset of the directory offset from start of file. * This offset is long word aligned since the directory offset is a long. */ static final int DIRECTORY_OFFSET_OFFSET = 24 ; /** * File header total size in bytes. */ static final int HEADER_SIZE = 32 ; /** * Byte offset of the object size from start of individual compressed * geometry block. */ static final int OBJECT_SIZE_OFFSET = 0 ; /** * Byte offset of the compressed geometry data descriptor from start of * individual compressed geometry block. */ static final int GEOM_DATA_OFFSET = 4 ; /** * Bits in compressed geometry data descriptor which encode the buffer type. */ static final int TYPE_MASK = 0x03 ; /** * Bit in compressed geometry data descriptor encoding presence of normals. */ static final int NORMAL_PRESENT_MASK = 0x04 ; /** * Bit in compressed geometry data descriptor encoding presence of colors. */ static final int COLOR_PRESENT_MASK = 0x08 ; /** * Bit in compressed geometry data descriptor encoding presence of alphas. */ static final int ALPHA_PRESENT_MASK = 0x10 ; /** * Value in compressed geometry data descriptor for a point buffer type. */ static final int TYPE_POINT = 1 ; /** * Value in compressed geometry data descriptor for a line buffer type. */ static final int TYPE_LINE = 2 ; /** * Value in compressed geometry data descriptor for a triangle buffer type. */ static final int TYPE_TRIANGLE = 3 ; /** * Block header total size in bytes. */ static final int BLOCK_HEADER_SIZE = 8 ; // The name of the compressed geometry resource file. String fileName = null ; // The major, minor, and subminor version number of the most recent // compressor used to compress any of the objects in the compressed // geometry resource file. int majorVersionNumber ; int minorVersionNumber ; int minorMinorVersionNumber ; // The number of objects in the compressed geometry resource file. int objectCount ; // The index of the current object in the file. int objectIndex = 0 ; // The random access file associated with this instance. RandomAccessFile cgFile = null ; // The magic number identifying the file type. int magicNumber ; // These fields are set from each individual block of compressed geometry. byte cgBuffer[] ; int geomSize ; int geomStart ; int geomDataType ; // The directory of object offsets is read from the end of the file. long directory[] ; long directoryOffset ; // The object sizes are computed from the directory offsets. These are // used to allocate a buffer large enough to hold the largest object and // to determine how many consecutive objects can be read into that buffer. int objectSizes[] ; int bufferObjectStart ; int bufferObjectCount ; int bufferNextObjectCount ; int bufferNextObjectOffset ; // The shared compressed geometry header object. CompressedGeometryHeader cgh ; // Flag indicating file update. boolean fileUpdate = false ; /** * Construct a new CompressedGeometryFile instance associated with the * specified file. An attempt is made to open the file with read-only * access; if this fails then a FileNotFoundException is thrown. * * @param file path to the compressed geometry resource file * @exception FileNotFoundException if file doesn't exist or * cannot be read * @exception IllegalArgumentException if the file is not a compressed * geometry resource file * @exception IOException if there is a header or directory read error */ public CompressedGeometryFile(String file) throws IOException { this(file, false) ; } /** * Construct a new CompressedGeometryFile instance associated with the * specified file. * * @param file path to the compressed geometry resource file * @param rw if true, opens the file for read and write access or attempts * to create one if it doesn't exist; if false, opens the file with * read-only access * @exception FileNotFoundException if file doesn't exist or * access permissions disallow access * @exception IllegalArgumentException if the file is not a compressed * geometry resource file * @exception IOException if there is a header or directory read error */ public CompressedGeometryFile(String file, boolean rw) throws IOException { // Open the file and read the file header. open(file, rw) ; // Copy the file name. fileName = new String(file) ; // Set up the file fields. initialize() ; } /** * Construct a new CompressedGeometryFile instance associated with a * currently open RandomAccessFile. * * @param file currently open RandomAccessFile * @exception IllegalArgumentException if the file is not a compressed * geometry resource file * @exception IOException if there is a header or directory read error */ public CompressedGeometryFile(RandomAccessFile file) throws IOException { // Copy the file reference. cgFile = file ; // Set up the file fields. initialize() ; } /** * Delete all compressed objects from this instance. This method may only * be called after successfully creating a CompressedGeometryFile instance * with read-write access, so a corrupted or otherwise invalid resource * must be removed manually before it can be rewritten. The close() * method must be called sometime after invoking clear() in order to write * out the new directory structure. * * @exception IOException if clear fails */ public void clear() throws IOException { // Truncate the file. cgFile.setLength(0) ; // Set up the file fields. initialize() ; } /** * Return a string containing the file name associated with this instance * or null if there is none. * * @return file name associated with this instance or null if there is * none */ public String getFileName() { return fileName ; } /** * Return the major version number of the most recent compressor used to * compress any of the objects in this instance. * * @return major version number */ public int getMajorVersionNumber() { return majorVersionNumber ; } /** * Return the minor version number of the most recent compressor used to * compress any of the objects in this instance. * * @return minor version number */ public int getMinorVersionNumber() { return minorVersionNumber ; } /** * Return the subminor version number of the most recent compressor used to * compress any of the objects in this instance. * * @return subminor version number */ public int getMinorMinorVersionNumber() { return minorMinorVersionNumber ; } /** * Return the number of compressed objects in this instance. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -