Class BlenderFile
- java.lang.Object
-
- org.cakelab.blender.io.BlenderFile
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
- Direct Known Subclasses:
BlenderFactoryBase.BlenderFileImplBase
,BlenderFactoryBase.StructDNAImage
,StructDNAImageGenerator
public class BlenderFile extends java.lang.Object implements java.io.Closeable
Class BlenderFile implements various functionalities to support in reading or writing blender files.Reading
To simply read a blender file you can use the constructor
BlenderFile(File)
. If the data model was generated with the optional utilities (i.e. with the 'utils' package) then you will find a class MainLib in this package. The MainLib provides CFacades to all data available in the file. To instantiate the MainLib you need block table received viagetBlockTable()
. The block table is also a way to access blocks directly. The block table maps virtual addresses to memory in blocks. Internally, it maintains a sorted list of the blocks. If you prefer or need direct access to blocks in the order given in the blender file then you can use the list returned fromgetBlocks()
instead.Writing
Unfortunately, writing a blender file can take a bit more effort, especially if you are adding new blocks. First of all, it is important to know, that the list of blocks you may have received after reading, is not in sync with the block table. Adding and removing blocks in the block table is not reflected in the list.
The order of blocks usually doesn't matter except of the ENDB block and some special cases I saw in blender code. The methods
write()
andwrite(List)
take care of ENDB and DNA1, i.e. they add a StructDNA block and an ENDB block at the end. Since those remaining special cases in blender code, where the order of blocks matters, are version specific, Java Blend cannot provide a generic solution and instead allows the API developer to provide his own block list to methodwrite(List)
.To help maintain a certain order of blocks, Java Blend provides the class
BlockList
which is a linked list of blocks, and therefore optimised for fast adding and removing of blocks. But this list is not automatically updated if blocks are added to or removed from the block table.
-
-
Field Summary
Fields Modifier and Type Field Description protected long
firstBlockOffset
protected FileHeader
header
protected CDataReadWriteAccess
io
-
Constructor Summary
Constructors Modifier Constructor Description protected
BlenderFile()
BlenderFile(java.io.File file)
protected
BlenderFile(java.io.File file, StructDNA sdna, int blenderVersion, java.lang.String[] offheapAreas)
protected
BlenderFile(java.io.File file, StructDNA sdna, int blenderVersion, Encoding encoding, java.lang.String[] offheapAreas)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(Block block)
Adds a block to the end of the file.void
close()
DNAModel
getBlenderModel()
BlockList
getBlocks()
BlockTable
getBlockTable()
Encoding
getEncoding()
java.io.File
getFile()
FileHeader
getHeader()
CMetaModel
getMetaModel()
protected int[]
getSdnaIndices(java.lang.String[] structNames)
Retrieve indices for given names from Struct DNA.StructDNA
getStructDNA()
FileHeader.Version
getVersion()
FileVersionInfo
readFileGlobal()
protected void
readFileHeader(CDataReadWriteAccess in)
Just basic read initialisation.protected void
readStructDNA()
BlockHeader
seekFirstBlock(Identifier code)
void
write()
Write all blocks to the file.void
write(java.util.List<Block> blocks)
Write given blocks to the file.protected void
writeEndBlock()
protected void
writeSdnaBlock()
-
-
-
Field Detail
-
header
protected FileHeader header
-
io
protected CDataReadWriteAccess io
-
firstBlockOffset
protected long firstBlockOffset
-
-
Constructor Detail
-
BlenderFile
public BlenderFile(java.io.File file) throws java.io.IOException
- Throws:
java.io.IOException
-
BlenderFile
protected BlenderFile(java.io.File file, StructDNA sdna, int blenderVersion, java.lang.String[] offheapAreas) throws java.io.IOException
- Throws:
java.io.IOException
-
BlenderFile
protected BlenderFile(java.io.File file, StructDNA sdna, int blenderVersion, Encoding encoding, java.lang.String[] offheapAreas) throws java.io.IOException
- Throws:
java.io.IOException
-
BlenderFile
protected BlenderFile()
-
-
Method Detail
-
readFileHeader
protected void readFileHeader(CDataReadWriteAccess in) throws java.io.IOException
Just basic read initialisation. Reading file header. (byte order doesn't matter in this case).- Parameters:
in
-- Throws:
java.io.IOException
-
getSdnaIndices
protected int[] getSdnaIndices(java.lang.String[] structNames) throws java.io.IOException
Retrieve indices for given names from Struct DNA.- Throws:
java.io.IOException
-
write
public void write() throws java.io.IOException
Write all blocks to the file. This method callswrite(List)
whith all blocks stored in this instance of BlenderFile.- Throws:
java.io.IOException
-
write
public void write(java.util.List<Block> blocks) throws java.io.IOException
Write given blocks to the file. This reorders only the Struct DNA (DNA1) block and the End (ENDB) block. All other blocks have to be in the order expected by blender.- Throws:
java.io.IOException
-
writeEndBlock
protected void writeEndBlock() throws java.io.IOException
- Throws:
java.io.IOException
-
writeSdnaBlock
protected void writeSdnaBlock() throws java.io.IOException
- Throws:
java.io.IOException
-
getBlenderModel
public DNAModel getBlenderModel() throws java.io.IOException
- Throws:
java.io.IOException
-
readFileGlobal
public FileVersionInfo readFileGlobal() throws java.io.IOException
- Throws:
java.io.IOException
-
readStructDNA
protected void readStructDNA() throws java.io.IOException
- Throws:
java.io.IOException
-
seekFirstBlock
public BlockHeader seekFirstBlock(Identifier code) throws java.io.IOException
- Throws:
java.io.IOException
-
getBlockTable
public BlockTable getBlockTable() throws java.io.IOException
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
getHeader
public FileHeader getHeader()
-
getEncoding
public Encoding getEncoding()
- Returns:
- Encoding according to the files header
-
getMetaModel
public CMetaModel getMetaModel() throws java.io.IOException
- Throws:
java.io.IOException
-
getVersion
public FileHeader.Version getVersion()
-
getStructDNA
public StructDNA getStructDNA()
-
getBlocks
public BlockList getBlocks()
-
add
public void add(Block block)
Adds a block to the end of the file. Please note, that methodwrite()
will rearrange blocks eventually to move ENDB at the end.- Parameters:
block
-
-
getFile
public java.io.File getFile()
-
-