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 final InputStream getInputStream(final ByteSource byteSource, final long skip) throws IOException {
047        InputStream is = null;
048        boolean succeeded = false;
049        try {
050            is = byteSource.getInputStream();
051            BinaryFunctions.skipBytes(is, skip);
052            succeeded = true;
053        } finally {
054            if (!succeeded) {
055                IOUtils.close(is);
056            }
057        }
058        return is;
059    }
060
061    public static ByteSource inputStream(final InputStream is, final String name) throws IOException {
062        return new InputStreamByteSource(is, name);
063    }
064
065    public static ByteSource path(final Path file) {
066        return new ByteSource(new PathOrigin(file), Objects.toString(file.getFileName(), null));
067    }
068
069    private final String fileName;
070    private final AbstractOrigin<?, ?> origin;
071
072    ByteSource(final AbstractOrigin<?, ?> origin, final String fileName) {
073        this.origin = Objects.requireNonNull(origin, "origin");
074        this.fileName = fileName; // may be null
075    }
076
077    public byte[] getByteArray(final long position, final int length) throws IOException {
078        return origin.getByteArray(position, length);
079    }
080
081    public final String getFileName() {
082        return fileName;
083    }
084
085    public InputStream getInputStream() throws IOException {
086        return origin.getInputStream();
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}