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.imaging.bytesource;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.nio.file.Path;
23  import java.util.Objects;
24  
25  import org.apache.commons.imaging.common.BinaryFunctions;
26  import org.apache.commons.io.IOUtils;
27  import org.apache.commons.io.build.AbstractOrigin;
28  import org.apache.commons.io.build.AbstractOrigin.ByteArrayOrigin;
29  import org.apache.commons.io.build.AbstractOrigin.FileOrigin;
30  import org.apache.commons.io.build.AbstractOrigin.PathOrigin;
31  
32  public class ByteSource {
33  
34      public static ByteSource array(final byte[] array) {
35          return new ByteSource(new ByteArrayOrigin(array), null);
36      }
37  
38      public static ByteSource array(final byte[] array, final String name) {
39          return new ByteSource(new ByteArrayOrigin(array), name);
40      }
41  
42      public static ByteSource file(final File file) {
43          return new ByteSource(new FileOrigin(file), file.getName());
44      }
45  
46      public static ByteSource inputStream(final InputStream is, final String name) {
47          return new InputStreamByteSource(is, name);
48      }
49  
50      public static ByteSource path(final Path file) {
51          return new ByteSource(new PathOrigin(file), Objects.toString(file.getFileName(), null));
52      }
53  
54      private final String fileName;
55      private final AbstractOrigin<?, ?> origin;
56  
57      public ByteSource(final AbstractOrigin<?, ?> origin, final String fileName) {
58          this.fileName = fileName;
59          this.origin = origin;
60      }
61  
62      public byte[] getByteArray(final long position, final int length) throws IOException {
63          return origin.getByteArray(position, length);
64      }
65  
66      public final String getFileName() {
67          return fileName;
68      }
69  
70      public InputStream getInputStream() throws IOException {
71          return origin.getInputStream();
72      }
73  
74      public final InputStream getInputStream(final long start) throws IOException {
75          InputStream is = null;
76          boolean succeeded = false;
77          try {
78              is = getInputStream();
79              BinaryFunctions.skipBytes(is, start);
80              succeeded = true;
81          } finally {
82              if (!succeeded) {
83                  IOUtils.close(is);
84              }
85          }
86          return is;
87      }
88  
89      /**
90       * This operation can be VERY expensive; for InputStream byte sources, the entire stream must be drained to determine its length.
91       *
92       * @return the byte source length
93       * @throws IOException if it fails to read the byte source data
94       */
95      public long size() throws IOException {
96          return origin.size();
97      }
98  
99      @Override
100     public String toString() {
101         return getClass().getSimpleName() + "[" + getFileName() + "]";
102     }
103 
104 }