View Javadoc

    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
    19  import java.io.BufferedReader;
    20  import java.io.InputStream;
    21  import java.io.InputStreamReader;
    22  import java.io.PushbackInputStream;
    23  import java.nio.charset.Charset;
    24  import java.text.MessageFormat;
    25  import java.util.Arrays;
    26
    27  import org.apache.commons.geometry.io.core.internal.GeometryIOUtils;
    28  import org.apache.commons.geometry.io.euclidean.threed.FacetDefinitionReader;
    29
    30  /** Utility class with factory methods for constructing {@link FacetDefinitionReader}
    31   * instances for STL content.
    32   */
    33  public final class StlFacetDefinitionReaders {
    34
    35      /** Utility class; no instantiation. */
    36      private StlFacetDefinitionReaders() { }
    37
    38      /** Construct a {@link FacetDefinitionReader} for reading STL content from the
    39       * given input. The format of the input is checked to determine if it is a binary
    40       * or text file and an appropriate reader is returned.
    41       * @param in input to read from
    42       * @param charset charset to use when checking the input for text content;
    43       *      if null, the input is assumed to use the UTF-8 charset
    44       * @return facet definition reader
    45       * @throws IllegalStateException if a parsing error occurs
    46       * @throws java.io.UncheckedIOException if an I/O error occurs
    47       */
    48      public static FacetDefinitionReader create(final InputStream in, final Charset charset) {
    49          final Charset inputCharset = charset != null ?
    50                  charset :
    51                  StlConstants.DEFAULT_CHARSET;
    52
    53          final byte[] testBytes = StlConstants.SOLID_START_KEYWORD.getBytes(inputCharset);
    54          final byte[] actualBytes = new byte[testBytes.length];
    55
    56          final int read = GeometryIOUtils.applyAsIntUnchecked(in::read, actualBytes);
    57          if (read < actualBytes.length) {
    58              throw GeometryIOUtils.parseError(MessageFormat.format(
    59                      "Cannot determine STL format: attempted to read {0} bytes but found only {1} available",
    60                      actualBytes.length, read));
    61          }
    62
    63          // "unread" the test bytes so that the created readers can start from the
    64          // beginning of the content
    65          final PushbackInputStream pushbackInput = new PushbackInputStream(in, actualBytes.length);
    66          GeometryIOUtils.acceptUnchecked(pushbackInput::unread, actualBytes);
    67
    68          if (Arrays.equals(testBytes, actualBytes)) {
    69              // this is a text file
    70              return new TextStlFacetDefinitionReader(
    71                      new BufferedReader(new InputStreamReader(pushbackInput, inputCharset)));
    72          } else {
    73              // this is a binary file
    74              return new BinaryStlFacetDefinitionReader(pushbackInput);
    75          }
    76      }
    77  }