StlFacetDefinitionReaders.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.geometry.io.euclidean.threed.stl;

  18. import java.io.BufferedReader;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.io.PushbackInputStream;
  22. import java.nio.charset.Charset;
  23. import java.text.MessageFormat;
  24. import java.util.Arrays;

  25. import org.apache.commons.geometry.io.core.internal.GeometryIOUtils;
  26. import org.apache.commons.geometry.io.euclidean.threed.FacetDefinitionReader;

  27. /** Utility class with factory methods for constructing {@link FacetDefinitionReader}
  28.  * instances for STL content.
  29.  */
  30. public final class StlFacetDefinitionReaders {

  31.     /** Utility class; no instantiation. */
  32.     private StlFacetDefinitionReaders() { }

  33.     /** Construct a {@link FacetDefinitionReader} for reading STL content from the
  34.      * given input. The format of the input is checked to determine if it is a binary
  35.      * or text file and an appropriate reader is returned.
  36.      * @param in input to read from
  37.      * @param charset charset to use when checking the input for text content;
  38.      *      if null, the input is assumed to use the UTF-8 charset
  39.      * @return facet definition reader
  40.      * @throws IllegalStateException if a parsing error occurs
  41.      * @throws java.io.UncheckedIOException if an I/O error occurs
  42.      */
  43.     public static FacetDefinitionReader create(final InputStream in, final Charset charset) {
  44.         final Charset inputCharset = charset != null ?
  45.                 charset :
  46.                 StlConstants.DEFAULT_CHARSET;

  47.         final byte[] testBytes = StlConstants.SOLID_START_KEYWORD.getBytes(inputCharset);
  48.         final byte[] actualBytes = new byte[testBytes.length];

  49.         final int read = GeometryIOUtils.applyAsIntUnchecked(in::read, actualBytes);
  50.         if (read < actualBytes.length) {
  51.             throw GeometryIOUtils.parseError(MessageFormat.format(
  52.                     "Cannot determine STL format: attempted to read {0} bytes but found only {1} available",
  53.                     actualBytes.length, read));
  54.         }

  55.         // "unread" the test bytes so that the created readers can start from the
  56.         // beginning of the content
  57.         final PushbackInputStream pushbackInput = new PushbackInputStream(in, actualBytes.length);
  58.         GeometryIOUtils.acceptUnchecked(pushbackInput::unread, actualBytes);

  59.         if (Arrays.equals(testBytes, actualBytes)) {
  60.             // this is a text file
  61.             return new TextStlFacetDefinitionReader(
  62.                     new BufferedReader(new InputStreamReader(pushbackInput, inputCharset)));
  63.         } else {
  64.             // this is a binary file
  65.             return new BinaryStlFacetDefinitionReader(pushbackInput);
  66.         }
  67.     }
  68. }