/*
 * Copyright 2002-2015 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.raf;

import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.lang.annotations.NotNull;
import com.drew.metadata.Metadata;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author TSGames https://github.com/TSGames
 */
public class RafMetadataReader
{
    @NotNull
    public static Metadata readMetadata(@NotNull InputStream inputStream) throws JpegProcessingException, IOException
    {
        inputStream.mark(512);

        byte[] data = new byte[512];
        inputStream.read(data);
        inputStream.reset();
        for (int i = 0; i < data.length - 2; i++) {
            if (data[i] == (byte) 0xff && data[i + 1] == (byte) 0xd8 && data[i + 2] == (byte) 0xff) {
                inputStream.skip(i);
                break;
            }
        }

        return JpegMetadataReader.readMetadata(inputStream);
    }

    private RafMetadataReader() throws Exception
    {
        throw new Exception("Not intended for instantiation");
    }
}

@@ -1057,50 +1057,163 @@
 %0A * 
-@author TSGames https://github.com/TSGames
+Obtains metadata from RAF (Fujifilm camera raw) image files.%0A *%0A * @author TSGames https://github.com/TSGames%0A * @author Drew Noakes https://drewnoakes.com
 %0A */
@@ -1375,32 +1375,144 @@
 Exception%0A    %7B%0A
+        if (!inputStream.markSupported())%0A            throw new IOException(%22Stream must support mark/reset%22);%0A%0A
         inputStr
@@ -1563,32 +1563,48 @@
 te%5B512%5D;%0A       
+ int bytesRead =
  inputStream.rea
@@ -1612,16 +1612,101 @@
 (data);%0A
+%0A        if (bytesRead == -1)%0A            throw new IOException(%22Stream is empty%22);%0A%0A
         
@@ -1726,16 +1726,17 @@
 eset();%0A
+%0A
         
@@ -1759,19 +1759,17 @@
 i %3C 
-data.length
+bytesRead
  - 2
@@ -1773,24 +1773,93 @@
 - 2; i++) %7B%0A
+            // Look for the first three bytes of a JPEG encoded file%0A
             
@@ -1968,26 +1968,160 @@
     
-inputStream.skip(i
+long bytesSkipped = inputStream.skip(i);%0A                if (bytesSkipped != i)%0A                    throw new IOException(%22Skipping stream bytes failed%22
 );%0A 

Validate use of stream in RafMetadataReader.