/* * Copyright 2002-2016 Drew Noakes * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * More information about this project is available at: * * https://drewnoakes.com/code/exif/ * https://github.com/drewnoakes/metadata-extractor */ package com.drew.imaging.jpeg; import com.drew.lang.SequentialReader; import com.drew.lang.StreamReader; import com.drew.lang.annotations.NotNull; import com.drew.lang.annotations.Nullable; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.HashSet; import java.util.Set; /** * Performs read functions of JPEG files, returning specific file segments. *
* JPEG files are composed of a sequence of consecutive JPEG 'segments'. Each is identified by one of a set of byte
* values, modelled in the {@link JpegSegmentType} enumeration. Use readSegments to read out the some
* or all segments into a {@link JpegSegmentData} object, from which the raw JPEG segment byte arrays may be accessed.
*
* @author Drew Noakes https://drewnoakes.com
*/
public class JpegSegmentReader
{
/**
* The 0xFF byte that signals the start of a segment.
*/
private static final byte SEGMENT_IDENTIFIER = (byte) 0xFF;
/**
* Private, because this segment crashes my algorithm, and searching for it doesn't work (yet).
*/
private static final byte SEGMENT_SOS = (byte) 0xDA;
/**
* Private, because one wouldn't search for it.
*/
private static final byte MARKER_EOI = (byte) 0xD9;
/**
* Processes the provided JPEG data, and extracts the specified JPEG segments into a {@link JpegSegmentData} object.
*
* Will not return SOS (start of scan) or EOI (end of image) segments.
*
* @param file a {@link File} from which the JPEG data will be read.
* @param segmentTypes the set of JPEG segments types that are to be returned. If this argument is
* Will not return SOS (start of scan) or EOI (end of image) segments.
*
* @param reader a {@link SequentialReader} from which the JPEG data will be read. It must be positioned at the
* beginning of the JPEG data stream.
* @param segmentTypes the set of JPEG segments types that are to be returned. If this argument is null
* then all found segment types are returned.
*/
@NotNull
public static JpegSegmentData readSegments(@NotNull File file, @Nullable Iterablenull
* then all found segment types are returned.
*/
@NotNull
public static JpegSegmentData readSegments(@NotNull final SequentialReader reader, @Nullable Iterable