Class BinaryStlFacetDefinitionReader
java.lang.Object
org.apache.commons.geometry.io.euclidean.threed.stl.BinaryStlFacetDefinitionReader
- All Implemented Interfaces:
AutoCloseable
,FacetDefinitionReader
Class used to read the binary form of the STL file format.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
True when the header content has been read.private ByteBuffer
Header content.private final InputStream
Input stream to read from.private final ByteBuffer
Buffer used to read triangle definitions.private long
Number of triangles read so far.private long
Total number of triangles declared to be present in the input. -
Constructor Summary
ConstructorsConstructorDescriptionConstruct a new instance that reads from the given input stream. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
Read the file header content and triangle count.void
close()
Close this instance and release all associated resources.private static IllegalStateException
dataNotAvailable
(String name) Return an exception stating that data is not available for the file component with the given name.private int
fill
(ByteBuffer buf) Fill the buffer with data from the input stream.Get a read-only buffer containing the 80 bytes of the STL header.Return the header content as a string decoded using the UTF-8 charset.getHeaderAsString
(Charset charset) Return the header content as a string decoded using the given charset.long
Get the total number of triangles (i.e.Return the next facet definition from the input source or null if no more facets are available.private BinaryStlFacetDefinition
Internal method to read a single facet from the input.private Vector3D
readVector
(ByteBuffer buf) Read a vector from the given byte buffer.
-
Field Details
-
in
Input stream to read from. -
triangleBuffer
Buffer used to read triangle definitions. -
header
Header content. -
triangleTotal
private long triangleTotalTotal number of triangles declared to be present in the input. -
trianglesRead
private long trianglesReadNumber of triangles read so far. -
hasReadHeader
private boolean hasReadHeaderTrue when the header content has been read.
-
-
Constructor Details
-
BinaryStlFacetDefinitionReader
Construct a new instance that reads from the given input stream.- Parameters:
in
- input stream to read from.
-
-
Method Details
-
getHeader
Get a read-only buffer containing the 80 bytes of the STL header. The header does not include the 4-byte value indicating the total number of triangles in the STL file.- Returns:
- the STL header content
- Throws:
UncheckedIOException
- if an I/O error occurs
-
getHeaderAsString
Return the header content as a string decoded using the UTF-8 charset. Control characters (such as '\0') are not included in the result.- Returns:
- the header content decoded as a UTF-8 string
- Throws:
UncheckedIOException
- if an I/O error occurs
-
getHeaderAsString
Return the header content as a string decoded using the given charset. Control characters (such as '\0') are not included in the result.- Parameters:
charset
- charset to decode the header with- Returns:
- the header content decoded as a string
- Throws:
UncheckedIOException
- if an I/O error occurs
-
getNumTriangles
public long getNumTriangles()Get the total number of triangles (i.e. facets) declared to be present in the input.- Returns:
- total number of triangle in the input
- Throws:
UncheckedIOException
- if an I/O error occurs
-
readFacet
Return the next facet definition from the input source or null if no more facets are available.- Specified by:
readFacet
in interfaceFacetDefinitionReader
- Returns:
- the next facet definition or null if no more facets are available
-
close
public void close()Close this instance and release all associated resources.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceFacetDefinitionReader
-
beginRead
private void beginRead()Read the file header content and triangle count.- Throws:
IllegalStateException
- is a parse error occursUncheckedIOException
- if an I/O error occurs
-
readFacetInternal
Internal method to read a single facet from the input.- Returns:
- facet read from the input
-
fill
Fill the buffer with data from the input stream. The buffer is then flipped and made ready for reading.- Parameters:
buf
- buffer to fill- Returns:
- number of bytes read
- Throws:
UncheckedIOException
- if an I/O error occurs
-
readVector
Read a vector from the given byte buffer.- Parameters:
buf
- buffer to read from- Returns:
- vector containing the next 3 double values from the given buffer
-
dataNotAvailable
Return an exception stating that data is not available for the file component with the given name.- Parameters:
name
- name of the file component missing data- Returns:
- exception instance
-