001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.geometry.io.euclidean.threed.stl;
018
019import java.nio.charset.Charset;
020
021import org.apache.commons.geometry.io.core.GeometryFormat;
022import org.apache.commons.geometry.io.core.input.GeometryInput;
023import org.apache.commons.geometry.io.core.internal.GeometryIOUtils;
024import org.apache.commons.geometry.io.euclidean.threed.AbstractBoundaryReadHandler3D;
025import org.apache.commons.geometry.io.euclidean.threed.FacetDefinitionReader;
026import org.apache.commons.geometry.io.euclidean.threed.GeometryFormat3D;
027
028/** {@link org.apache.commons.geometry.io.euclidean.threed.BoundaryReadHandler3D BoundaryReadHandler3D}
029 * implementation for reading STL data. Text input is read using the UTF-8 charset by default.
030 */
031public class StlBoundaryReadHandler3D extends AbstractBoundaryReadHandler3D {
032
033    /** Default charset for reading text input. */
034    private Charset defaultCharset = StlConstants.DEFAULT_CHARSET;
035
036    /** {@inheritDoc} */
037    @Override
038    public GeometryFormat getFormat() {
039        return GeometryFormat3D.STL;
040    }
041
042    /** Get the input default charset, used if text input does not
043     * specify a charset.
044     * @return text input default charset
045     */
046    public Charset getDefaultCharset() {
047        return defaultCharset;
048    }
049
050    /** Set the input default charset, used if text input does not
051     * specify a charset.
052     * @param charset text input default charset
053     */
054    public void setDefaultCharset(final Charset charset) {
055        this.defaultCharset = charset;
056    }
057
058    /** {@inheritDoc} */
059    @Override
060    public FacetDefinitionReader facetDefinitionReader(final GeometryInput in) {
061        final Charset inputCharset = in.getCharset() != null ?
062                in.getCharset() :
063                defaultCharset;
064
065        return GeometryIOUtils.tryApplyCloseable(
066                inputStream -> StlFacetDefinitionReaders.create(inputStream, inputCharset),
067                in::getInputStream);
068    }
069}