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.imaging.bytesource;
018
019import java.io.File;
020import java.io.IOException;
021import java.io.InputStream;
022import java.nio.file.Path;
023import java.util.Objects;
024
025import org.apache.commons.imaging.common.BinaryFunctions;
026import org.apache.commons.io.IOUtils;
027import org.apache.commons.io.build.AbstractOrigin;
028import org.apache.commons.io.build.AbstractOrigin.ByteArrayOrigin;
029import org.apache.commons.io.build.AbstractOrigin.FileOrigin;
030import org.apache.commons.io.build.AbstractOrigin.PathOrigin;
031
032public class ByteSource {
033
034    public static ByteSource array(final byte[] array) {
035        return new ByteSource(new ByteArrayOrigin(array), null);
036    }
037
038    public static ByteSource array(final byte[] array, final String name) {
039        return new ByteSource(new ByteArrayOrigin(array), name);
040    }
041
042    public static ByteSource file(final File file) {
043        return new ByteSource(new FileOrigin(file), file.getName());
044    }
045
046    public static ByteSource inputStream(final InputStream is, final String name) {
047        return new InputStreamByteSource(is, name);
048    }
049
050    public static ByteSource path(final Path file) {
051        return new ByteSource(new PathOrigin(file), Objects.toString(file.getFileName(), null));
052    }
053
054    private final String fileName;
055    private final AbstractOrigin<?, ?> origin;
056
057    public ByteSource(final AbstractOrigin<?, ?> origin, final String fileName) {
058        this.fileName = fileName;
059        this.origin = origin;
060    }
061
062    public byte[] getByteArray(final long position, final int length) throws IOException {
063        return origin.getByteArray(position, length);
064    }
065
066    public final String getFileName() {
067        return fileName;
068    }
069
070    public InputStream getInputStream() throws IOException {
071        return origin.getInputStream();
072    }
073
074    public final InputStream getInputStream(final long start) throws IOException {
075        InputStream is = null;
076        boolean succeeded = false;
077        try {
078            is = getInputStream();
079            BinaryFunctions.skipBytes(is, start);
080            succeeded = true;
081        } finally {
082            if (!succeeded) {
083                IOUtils.close(is);
084            }
085        }
086        return is;
087    }
088
089    /**
090     * This operation can be VERY expensive; for InputStream byte sources, the entire stream must be drained to determine its length.
091     *
092     * @return the byte source length
093     * @throws IOException if it fails to read the byte source data
094     */
095    public long size() throws IOException {
096        return origin.size();
097    }
098
099    @Override
100    public String toString() {
101        return getClass().getSimpleName() + "[" + getFileName() + "]";
102    }
103
104}