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.examples.io.threed;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  import java.io.UncheckedIOException;
24  import java.nio.file.Files;
25  
26  import org.apache.commons.geometry.core.precision.DoublePrecisionContext;
27  import org.apache.commons.geometry.euclidean.threed.BoundarySource3D;
28  
29  /** Abstract base class for {@link ModelIOHandler} implementations.
30   */
31  public abstract class AbstractModelIOHandler implements ModelIOHandler {
32  
33      /** {@inheritDoc} */
34      @Override
35      public BoundarySource3D read(final String type, final File in, final DoublePrecisionContext precision) {
36          ensureTypeSupported(type);
37          try {
38              try (InputStream is = Files.newInputStream(in.toPath())) {
39                  return readInternal(type, is, precision);
40              }
41          } catch (IOException exc) {
42              throw createUnchecked(exc);
43          }
44      }
45  
46      /** {@inheritDoc} */
47      @Override
48      public BoundarySource3D read(final String type, final InputStream in, final DoublePrecisionContext precision) {
49          ensureTypeSupported(type);
50          try {
51              return readInternal(type, in, precision);
52          } catch (IOException exc) {
53              throw createUnchecked(exc);
54          }
55      }
56  
57      /** {@inheritDoc} */
58      @Override
59      public void write(final BoundarySource3D model, final String type, final File out) {
60          ensureTypeSupported(type);
61          try {
62              try (OutputStream os = Files.newOutputStream(out.toPath())) {
63                  writeInternal(model, type, os);
64              }
65          } catch (IOException exc) {
66              throw createUnchecked(exc);
67          }
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public void write(final BoundarySource3D model, final String type, OutputStream out) {
73          ensureTypeSupported(type);
74          try {
75              writeInternal(model, type, out);
76          } catch (IOException exc) {
77              throw createUnchecked(exc);
78          }
79      }
80  
81      /** Throw an exception if the given type is not supported by this instance.
82       * @param type model type to check
83       */
84      private void ensureTypeSupported(final String type) {
85          if (!handlesType(type)) {
86              throw new IllegalArgumentException("File type is not supported by this handler: " + type);
87          }
88      }
89  
90      /** Create an unchecked exception from the given checked exception.
91       * @param exc exception to wrap in an unchecked exception
92       * @return the unchecked exception
93       */
94      private UncheckedIOException createUnchecked(final IOException exc) {
95          final String msg = exc.getClass().getSimpleName() + ": " + exc.getMessage();
96          return new UncheckedIOException(msg, exc);
97      }
98  
99      /** Internal class used to read a model. {@link IOException}s thrown from here are
100      * wrapped in {@link java.io.UncheckedIOException}s.
101      * @param type model type; guaranteed to be supported by this instance
102      * @param in input stream
103      * @param precision precision context used to construct the model
104      * @return 3D model
105      * @throws IOException if an IO operation fails
106      */
107     protected abstract BoundarySource3D readInternal(String type, InputStream in, DoublePrecisionContext precision)
108             throws IOException;
109 
110     /** Internal class used to write a model. {@link IOException}s thrown from here are
111      * wrapped in {@link java.io.UncheckedIOException}s.
112      * @param model model to write
113      * @param type model type; guaranteed to be supported by this instance
114      * @param out input stream
115      * @throws IOException if an IO operation fails
116      */
117     protected abstract void writeInternal(BoundarySource3D model, String type, OutputStream out) throws IOException;
118 }