/*
 * 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.cff;

import java.io.IOException;

/**
 * This is specialized DataInput. It's used to parse a CFFFont.
 * 
 * @author Villu Ruusmann
 */
public class CFFDataInput extends DataInput
{

    /**
     * Constructor.
     * @param buffer the buffer to be read 
     */
    public CFFDataInput(byte[] buffer)
    {
        super(buffer);
    }

    /**
     * Read one single Card8 value from the buffer. 
     * @return the card8 value
     * @throws IOException if an error occurs during reading
     */
    public int readCard8() throws IOException
    {
        return readUnsignedByte();
    }

    /**
     * Read one single Card16 value from the buffer.
     * @return the card16 value
     * @throws IOException if an error occurs during reading
     */
    public int readCard16() throws IOException
    {
        return readUnsignedShort();
    }

    /**
     * Read the offset from the buffer.
     * @param offSize the given offsize
     * @return the offset
     * @throws IOException if an error occurs during reading
     */
    public int readOffset(int offSize) throws IOException
    {
        int value = 0;
        for (int i = 0; i < offSize; i++)
        {
            value = value << 8 | readUnsignedByte();
        }
        return value;
    }

    /**
     * Read the offsize from the buffer.
     * @return the offsize
     * @throws IOException if an error occurs during reading
     */
    public int readOffSize() throws IOException
    {
        return readUnsignedByte();
    }

    /**
     * Read a SID from the buffer.
     * @return the SID
     * @throws IOException if an error occurs during reading
     */
    public int readSID() throws IOException
    {
        return readUnsignedShort();
    }
}
@@ -2109,24 +2109,20 @@
  * Read 
-the 
 off
-s
+S
 ize from
@@ -2125,32 +2125,79 @@
 from the buffer.
+ This is a 1 byte value between 1 and 4.%0A     *
 %0A     * @return 
@@ -2199,28 +2199,29 @@
 turn the off
-s
+S
 ize
+.
 %0A     * @thr
@@ -2261,32 +2261,60 @@
 s during reading
+ or if the value is illegal.
 %0A     */%0A    pub
@@ -2360,38 +2360,45 @@
 n%0A    %7B%0A        
-return
+int offSize =
  readUnsignedByt
@@ -2394,32 +2394,260 @@
 UnsignedByte();%0A
+        if (offSize %3C 1 %7C%7C offSize %3E 4)%0A        %7B%0A            throw new IOException(%22Illegal (%3C 1 or %3E 4) offSize value %22 + offSize + %22 in CFF font at position %22 + (getPosition() - 1));%0A        %7D%0A        return offSize;        %0A
     %7D%0A%0A    /**%0A 

throw exception on illegal offSize value