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.core.input;
18
19 import java.io.BufferedInputStream;
20 import java.io.InputStream;
21 import java.net.URL;
22 import java.nio.charset.Charset;
23
24 import org.apache.commons.geometry.io.core.AbstractGeometryIOMetadata;
25 import org.apache.commons.geometry.io.core.internal.GeometryIOUtils;
26
27 /** {@link GeometryInput} implementation for reading content from a URL.
28 */
29 public class UrlGeometryInput extends AbstractGeometryIOMetadata
30 implements GeometryInput {
31
32 /** Input URL. */
33 private final URL url;
34
35 /** Construct a new instance for reading from the given URL.
36 * @param url input url
37 */
38 public UrlGeometryInput(final URL url) {
39 this(url, null);
40 }
41
42 /** Construct a new instance for reading from the given URL with the
43 * specified charset.
44 * @param url input URL
45 * @param charset charset to use when reading content
46 */
47 public UrlGeometryInput(final URL url, final Charset charset) {
48 super(GeometryIOUtils.getFileName(url), charset);
49
50 this.url = url;
51 }
52
53 /** Get the input URL.
54 * @return input URL
55 */
56 public URL getUrl() {
57 return url;
58 }
59
60 /** {@inheritDoc}
61 *
62 * <p>The returned input stream is buffered.</p>
63 */
64 @Override
65 public InputStream getInputStream() {
66 return GeometryIOUtils.getUnchecked(() -> new BufferedInputStream(url.openStream()));
67 }
68
69 /** {@inheritDoc} */
70 @Override
71 public String toString() {
72 final StringBuilder sb = new StringBuilder();
73 sb.append(getClass().getSimpleName())
74 .append("[url= ")
75 .append(getUrl())
76 .append(']');
77
78 return sb.toString();
79 }
80 }