Class EpsReader


  • public class EpsReader
    extends java.lang.Object
    Reads file passed in through SequentialReader and parses encountered data:
    • Basic EPS Comments
    • EXIF
    • Photoshop
    • IPTC
    • ICC Profile
    • XMP
    EPS comments are retrieved from EPS directory. Photoshop, ICC Profile, and XMP processing is passed to their respective reader.

    EPS Constraints (Source: https://www-cdf.fnal.gov/offline/PostScript/5001.PDF pg.18):

    • Max line length is 255 characters
    • Lines end with a CR(0xD) or LF(0xA) character (or both, in practice)
    • ':' separates keywords (considered part of the keyword)
    • Whitespace is either a space(0x20) or tab(0x9)
    • If there is more than one header, the 1st is truth
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int _previousTag  
    • Constructor Summary

      Constructors 
      Constructor Description
      EpsReader()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void addToDirectory​(EpsDirectory directory, java.lang.String name, java.lang.String value)
      Default case that adds comment with keyword to directory
      private static byte[] decodeHexCommentBlock​(SequentialReader reader)
      EPS files can contain hexadecimal-encoded ASCII blocks, each prefixed with "% ".
      private void extract​(EpsDirectory directory, Metadata metadata, SequentialReader reader)
      Main method that parses all comments and then distributes data extraction among other methods that parse the rest of file and store encountered data in metadata (if there exists an entry in EpsDirectory for the found data).
      void extract​(java.io.InputStream inputStream, Metadata metadata)
      Filter method that determines if file will contain an EPS Header.
      private static void extractIccData​(Metadata metadata, SequentialReader reader)
      Decodes a commented hex section, and uses IccReader to decode the resulting data.
      private static void extractImageData​(EpsDirectory directory, java.lang.String imageData)
      Parses %ImageData comment which holds several values including width in px, height in px and color type.
      private static void extractPhotoshopData​(Metadata metadata, SequentialReader reader)
      Decodes a commented hex section, and uses PhotoshopReader to decode the resulting data.
      private static void extractXmpData​(Metadata metadata, SequentialReader reader)
      Extracts an XMP xpacket, and uses XmpReader to decode the resulting data.
      private static byte[] readUntil​(SequentialReader reader, byte[] sentinel)
      Reads all bytes until the given sentinel is observed.
      private static int tryHexToInt​(byte b)
      Treats a byte as an ASCII character, and returns it's numerical value in hexadecimal.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • _previousTag

        private int _previousTag
    • Constructor Detail

      • EpsReader

        public EpsReader()
    • Method Detail

      • extract

        public void extract​(java.io.InputStream inputStream,
                            Metadata metadata)
                     throws java.io.IOException
        Filter method that determines if file will contain an EPS Header. If it does, it will read the necessary data and then set the position to the beginning of the PostScript data. If it does not, the position will not be changed. After both scenarios, the main extract method is called.
        Parameters:
        inputStream - InputStream containing file
        metadata - Metadata to add directory to and extracted data
        Throws:
        java.io.IOException
      • extract

        private void extract​(EpsDirectory directory,
                             Metadata metadata,
                             SequentialReader reader)
                      throws java.io.IOException
        Main method that parses all comments and then distributes data extraction among other methods that parse the rest of file and store encountered data in metadata (if there exists an entry in EpsDirectory for the found data). Reads until a begin data/binary comment is found or _reader's estimated available data has run out (or AI09 End Private Data). Will extract data from normal EPS comments, Photoshop, ICC, and XMP.
        Parameters:
        metadata - Metadata to add directory to and extracted data
        Throws:
        java.io.IOException
      • addToDirectory

        private void addToDirectory​(EpsDirectory directory,
                                    java.lang.String name,
                                    java.lang.String value)
                             throws java.io.IOException
        Default case that adds comment with keyword to directory
        Parameters:
        directory - EpsDirectory to add extracted data to
        name - String that holds name of current comment
        value - String that holds value of current comment
        Throws:
        java.io.IOException
      • extractImageData

        private static void extractImageData​(EpsDirectory directory,
                                             java.lang.String imageData)
                                      throws java.io.IOException
        Parses %ImageData comment which holds several values including width in px, height in px and color type.
        Throws:
        java.io.IOException
      • extractPhotoshopData

        private static void extractPhotoshopData​(Metadata metadata,
                                                 SequentialReader reader)
                                          throws java.io.IOException
        Decodes a commented hex section, and uses PhotoshopReader to decode the resulting data.
        Throws:
        java.io.IOException
      • extractIccData

        private static void extractIccData​(Metadata metadata,
                                           SequentialReader reader)
                                    throws java.io.IOException
        Decodes a commented hex section, and uses IccReader to decode the resulting data.
        Throws:
        java.io.IOException
      • extractXmpData

        private static void extractXmpData​(Metadata metadata,
                                           SequentialReader reader)
                                    throws java.io.IOException
        Extracts an XMP xpacket, and uses XmpReader to decode the resulting data.
        Throws:
        java.io.IOException
      • readUntil

        private static byte[] readUntil​(SequentialReader reader,
                                        byte[] sentinel)
                                 throws java.io.IOException
        Reads all bytes until the given sentinel is observed. The sentinel will be included in the returned bytes.
        Throws:
        java.io.IOException
      • decodeHexCommentBlock

        private static byte[] decodeHexCommentBlock​(SequentialReader reader)
                                             throws java.io.IOException
        EPS files can contain hexadecimal-encoded ASCII blocks, each prefixed with "% ". This method reads such a block and returns a byte[] of the decoded contents. Reading stops at the first invalid line, which is discarded (it's a terminator anyway).

        For example:

        
         %BeginPhotoshop: 9564
         % 3842494D040400000000005D1C015A00031B25471C0200000200041C02780004
         % 6E756C6C1C027A00046E756C6C1C025000046E756C6C1C023700083230313630
         % 3331311C023C000B3131343335362B303030301C023E00083230313630333131
         % 48000000010000003842494D03FD0000000000080101000000000000
         %EndPhotoshop
         
        When calling this method, the reader must be positioned at the start of the first line containing hex data, not at the introductory line.
        Returns:
        The decoded bytes, or null if decoding failed.
        Throws:
        java.io.IOException
      • tryHexToInt

        private static int tryHexToInt​(byte b)
        Treats a byte as an ASCII character, and returns it's numerical value in hexadecimal. If conversion is not possible, returns -1.