Class EpsReader

java.lang.Object
com.drew.metadata.eps.EpsReader

public class EpsReader extends 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 Details

    • _previousTag

      private int _previousTag
  • Constructor Details

    • EpsReader

      public EpsReader()
  • Method Details

    • extract

      public void extract(InputStream inputStream, Metadata metadata) throws 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:
      IOException
    • extract

      private void extract(EpsDirectory directory, Metadata metadata, SequentialReader reader) throws 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:
      IOException
    • addToDirectory

      private void addToDirectory(EpsDirectory directory, String name, String value) throws 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:
      IOException
    • extractImageData

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

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

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

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

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

      private static byte[] decodeHexCommentBlock(SequentialReader reader) throws 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:
      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.