/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
package org.apache.fontbox.ttf;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;

/**
 * An implementation of the TTFDataStream that goes against a RAF.
 * 
 * @author Ben Litchfield
 */
class RAFDataStream extends TTFDataStream 
{
    private RandomAccessFile raf = null;
    private File ttfFile = null;
    private static final int BUFFERSIZE = 16384;
    
    /**
     * Constructor.
     * 
     * @param name The raf file.
     * @param mode The mode to open the RAF.
     * 
     * @throws IOException If there is a problem creating the RAF.
     * 
     * @see RandomAccessFile#RandomAccessFile( String, String )
     */
    RAFDataStream(String name, String mode) throws IOException
    {
        this( new File( name ), mode );
    }
    
    /**
     * Constructor.
     * 
     * @param file The raf file.
     * @param mode The mode to open the RAF.
     * 
     * @throws IOException If there is a problem creating the RAF.
     * 
     * @see RandomAccessFile#RandomAccessFile( File, String )
     */
    RAFDataStream(File file, String mode) throws IOException
    {
        raf = new BufferedRandomAccessFile(file, mode, BUFFERSIZE);
        ttfFile = file;
    }
    
    /**
     * Read an signed short.
     * 
     * @return An signed short.
     * @throws IOException If there is an error reading the data.
     */
    @Override
    public short readSignedShort() throws IOException
    {
        return raf.readShort();
    }
    
    /**
     * Get the current position in the stream.
     * @return The current position in the stream.
     * @throws IOException If an error occurs while reading the stream.
     */
    @Override
    public long getCurrentPosition() throws IOException
    {
        return raf.getFilePointer();
    }
    
    /**
     * Close the underlying resources.
     * 
     * @throws IOException If there is an error closing the resources.
     */
    @Override
    public void close() throws IOException
    {
        raf.close();
        raf = null;
    }
    
    /**
     * Read an unsigned byte.
     * @return An unsigned byte.
     * @throws IOException If there is an error reading the data.
     */
    @Override
    public int read() throws IOException
    {
        return raf.read();
    }
    
    /**
     * Read an unsigned short.
     * 
     * @return An unsigned short.
     * @throws IOException If there is an error reading the data.
     */
    @Override
    public int readUnsignedShort() throws IOException
    {
        return raf.readUnsignedShort();
    }
    
    /**
     * Read an unsigned byte.
     * @return An unsigned byte.
     * @throws IOException If there is an error reading the data.
     */
    @Override
    public long readLong() throws IOException
    {
        return raf.readLong();
    }
    
    /**
     * Seek into the datasource.
     * 
     * @param pos The position to seek to.
     * @throws IOException If there is an error seeking to that position.
     */
    @Override
    public void seek(long pos) throws IOException
    {
        raf.seek( pos );
    }
    
    /**
     * @see java.io.InputStream#read( byte[], int, int )
     * 
     * @param b The buffer to write to.
     * @param off The offset into the buffer.
     * @param len The length into the buffer.
     * 
     * @return The number of bytes read.
     * 
     * @throws IOException If there is an error reading from the stream.
     */
    @Override
    public int read(byte[] b, int off, int len) throws IOException
    {
        return raf.read(b, off, len);
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public InputStream getOriginalData() throws IOException
    {
        return new FileInputStream( ttfFile );
    }
}

@@ -2164,17 +2164,16 @@
 * Read a
-n
  signed 
@@ -2172,32 +2172,32 @@
  signed short.%0D%0A
+
      * %0D%0A     * 
@@ -2281,32 +2281,74 @@
 ding the data.%0D%0A
+     * @see RandomAccessFile#readShort()%0D%0A
      */%0D%0A    @Ov
@@ -3178,32 +3178,69 @@
 ding the data.%0D%0A
+     * @see RandomAccessFile#read()%0D%0A
      */%0D%0A    @Ov
@@ -3481,32 +3481,82 @@
 ding the data.%0D%0A
+     * @see RandomAccessFile#readUnsignedShort()%0D%0A
      */%0D%0A    @Ov
@@ -3705,57 +3705,90 @@
 ad a
-n un
+ 
 signed 
-byte.%0D%0A     * @return An unsigned byte
+64-bit integer.%0D%0A     * %0D%0A     * @return eight bytes interpreted as a long
 .%0D%0A 
@@ -3849,24 +3849,69 @@
  the data.%0D%0A
+     * @see RandomAccessFile#readLong()    %0D%0A
      */%0D%0A   

clarify javadoc