Save This Page
Home » pdfbox-1.1.0-src » org.apache.fontbox.cff » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.fontbox.cff;
   18   
   19   import java.io.EOFException;
   20   import java.io.IOException;
   21   
   22   /**
   23    * This class contains some functionality to read a byte buffer.
   24    * 
   25    * @author Villu Ruusmann
   26    * @version $Revision$
   27    */
   28   public class DataInput
   29   {
   30   
   31       private byte[] inputBuffer = null;
   32       private int bufferPosition = 0;
   33   
   34       /**
   35        * Constructor.
   36        * @param buffer the buffer to be read
   37        */
   38       public DataInput(byte[] buffer)
   39       {
   40           inputBuffer = buffer;
   41       }
   42   
   43       /**
   44        * Determines if there are any bytes left to read or not. 
   45        * @return true if there are any bytes left to read
   46        */
   47       public boolean hasRemaining()
   48       {
   49           return bufferPosition < inputBuffer.length;
   50       }
   51   
   52       /**
   53        * Returns the current position.
   54        * @return current position
   55        */
   56       public int getPosition()
   57       {
   58           return bufferPosition;
   59       }
   60   
   61       /**
   62        * Sets the current position to the given value.
   63        * @param position the given position
   64        */
   65       public void setPosition(int position)
   66       {
   67           bufferPosition = position;
   68       }
   69   
   70       /** 
   71        * Returns the buffer as an ISO-8859-1 string.
   72        * @return the buffer as string
   73        * @throws IOException if an error occurs during reading
   74        */
   75       public String getString() throws IOException
   76       {
   77           return new String(inputBuffer, "ISO-8859-1");
   78       }
   79   
   80       /**
   81        * Read one single byte from the buffer.
   82        * @return the byte
   83        * @throws IOException if an error occurs during reading
   84        */
   85       public byte readByte() throws IOException
   86       {
   87           return (byte) readUnsignedByte();
   88       }
   89   
   90       /**
   91        * Read one single unsigned byte from the buffer.
   92        * @return the unsigned byte as int
   93        * @throws IOException if an error occurs during reading
   94        */
   95       public int readUnsignedByte() throws IOException
   96       {
   97           int b = read();
   98           if (b < 0)
   99           {
  100               throw new EOFException();
  101           }
  102           return b;
  103       }
  104   
  105       /**
  106        * Read one single short value from the buffer.
  107        * @return the short value
  108        * @throws IOException if an error occurs during reading
  109        */
  110       public short readShort() throws IOException
  111       {
  112           return (short) readUnsignedShort();
  113       }
  114   
  115       /**
  116        * Read one single unsigned short (2 bytes) value from the buffer.
  117        * @return the unsigned short value as int
  118        * @throws IOException if an error occurs during reading
  119        */
  120       public int readUnsignedShort() throws IOException
  121       {
  122           int b1 = read();
  123           int b2 = read();
  124           if ((b1 | b2) < 0)
  125           {
  126               throw new EOFException();
  127           }
  128           return b1 << 8 | b2;
  129       }
  130   
  131       /**
  132        * Read one single int (4 bytes) from the buffer.
  133        * @return the int value
  134        * @throws IOException if an error occurs during reading
  135        */
  136       public int readInt() throws IOException
  137       {
  138           int b1 = read();
  139           int b2 = read();
  140           int b3 = read();
  141           int b4 = read();
  142           if ((b1 | b2 | b3 | b4) < 0)
  143           {
  144               throw new EOFException();
  145           }
  146           return b1 << 24 | b2 << 16 | b3 << 8 | b4;
  147       }
  148   
  149       /**
  150        * Read a number of single byte values from the buffer.
  151        * @param length the number of bytes to be read
  152        * @return an array with containing the bytes from the buffer 
  153        * @throws IOException if an error occurs during reading
  154        */
  155       public byte[] readBytes(int length) throws IOException
  156       {
  157           byte[] bytes = new byte[length];
  158           for (int i = 0; i < length; i++)
  159           {
  160               bytes[i] = readByte();
  161           }
  162           return bytes;
  163       }
  164   
  165       private int read()
  166       {
  167           try
  168           {
  169               int value = inputBuffer[bufferPosition] & 0xff;
  170               bufferPosition++;
  171               return value;
  172           } 
  173           catch (RuntimeException re)
  174           {
  175               return -1;
  176           }
  177       }
  178   }

Save This Page
Home » pdfbox-1.1.0-src » org.apache.fontbox.cff » [javadoc | source]