Class: PDF::Reader::Buffer

### Home Classes Methods

### In Files

- lib/pdf/reader/buffer.rb

### Parent

Object

### Methods

- ::new
- #empty?
- #find_first_xref_offset
- #read
- #token

### Files

- MIT-LICENSE
- README.rdoc
- TODO
- mac_expert.txt
- mac_roman.txt
- pdf_doc.txt
- standard.txt
- symbol.txt
- win_ansi.txt
- zapf_dingbats.txt
- glyphlist.txt

### Class/Module Index

Quicksearch

- PDF
- PDF::Reader
- PDF::Reader::Buffer
- PDF::Reader::CidWidths
- PDF::Reader::EncryptedPDFError
- PDF::Reader::Filter
- PDF::Reader::Filter::Ascii85
- PDF::Reader::Filter::AsciiHex
- PDF::Reader::Filter::Depredict
- PDF::Reader::Filter::Flate
- PDF::Reader::Filter::Lzw
- PDF::Reader::Filter::Null
- PDF::Reader::Filter::RunLength
- PDF::Reader::Font
- PDF::Reader::FontDescriptor
- PDF::Reader::FormXObject
- PDF::Reader::InvalidObjectError
- PDF::Reader::MalformedPDFError
- PDF::Reader::ObjectCache
- PDF::Reader::ObjectHash
- PDF::Reader::PageLayout
- PDF::Reader::PageState
- PDF::Reader::PageTextReceiver
- PDF::Reader::Parser
- PDF::Reader::PrintReceiver
- PDF::Reader::Reference
- PDF::Reader::RegisterReceiver
- PDF::Reader::ResourceMethods
- PDF::Reader::StandardSecurityHandler
- PDF::Reader::Stream
- PDF::Reader::SynchronizedCache
- PDF::Reader::TextReceiver
- PDF::Reader::TextRun
- PDF::Reader::TransformationMatrix
- PDF::Reader::UnsupportedFeatureError
- PDF::Reader::WidthCalculator
- PDF::Reader::WidthCalculator::BuiltIn
- PDF::Reader::WidthCalculator::Composite
- PDF::Reader::WidthCalculator::TrueType
- PDF::Reader::WidthCalculator::TypeOneOrThree
- PDF::Reader::WidthCalculator::TypeZero
- PDF::Reader::XRef
- AFM
- AFM::Font

No matching classes.

# PDF::Reader::Buffer

A string tokeniser that recognises PDF grammar. When passed an IO stream or a string, repeated calls to token() will return the next token from the source.

This is very low level, and getting the raw tokens is not very useful in itself.

This will usually be used in conjunction with PDF:Reader::Parser, which converts the raw tokens into objects we can work with (strings, ints, arrays, etc)

### Constants

FWD_SLASH
ID
LEFT_PAREN

  some strings for comparissons. Declaring them here avoids creating new strings that need GC over and over

LESS_THAN
STREAM
TOKEN_WHITESPACE

### Attributes

pos[R]

### Public Class Methods

new(io, opts = {}) click to toggle source

Creates a new buffer.

Params:

    io - an IO stream or string with the raw data to tokenise

options:

    :seek - a byte offset to seek to before starting to tokenise
    :content_stream - set to true if buffer will be tokenising a
                      content stream. Defaults to false

    # File lib/pdf/reader/buffer.rb, line 62
    def initialize (io, opts = {})
      @io = io
      @tokens = []
      @in_content_stream = opts[:content_stream]

      @io.seek(opts[:seek]) if opts[:seek]
      @pos = @io.pos
    end

### Public Instance Methods

empty?() click to toggle source

return true if there are no more tokens left

    # File lib/pdf/reader/buffer.rb, line 73
    def empty?
      prepare_tokens if @tokens.size < 3

      @tokens.empty?
    end

find_first_xref_offset() click to toggle source

return the byte offset where the first XRef table in th source can be found.

    # File lib/pdf/reader/buffer.rb, line 124
    def find_first_xref_offset
      check_size_is_non_zero
      @io.seek(-1024, IO::SEEK_END) rescue @io.seek(0)
      data = @io.read(1024)

      # the PDF 1.7 spec (section #3.4) says that EOL markers can be either \r, \n, or both.
      lines = data.split(/[\n\r]+/).reverse
      eof_index = lines.index { |l| l.strip[/^%%EOF/] }

      raise MalformedPDFError, "PDF does not contain EOF marker" if eof_index.nil?
      raise MalformedPDFError, "PDF EOF marker does not follow offset" if eof_index >= lines.size-1
      lines[eof_index+1].to_i
    end

read(bytes, opts = {}) click to toggle source

return raw bytes from the underlying IO stream.

    bytes - the number of bytes to read

options:

    :skip_eol - if true, the IO stream is advanced past a CRLF or LF that
                is sitting under the io cursor.

    # File lib/pdf/reader/buffer.rb, line 88
    def read(bytes, opts = {})
      reset_pos

      if opts[:skip_eol]
        @io.seek(-1, IO::SEEK_CUR)
        str = @io.read(2)
        if str.nil?
          return nil
        elsif str == "\r\n"
          # do nothing
        elsif str[0,1] == "\n"
          @io.seek(-1, IO::SEEK_CUR)
        else
          @io.seek(-2, IO::SEEK_CUR)
        end
      end

      bytes = @io.read(bytes)
      save_pos
      bytes
    end

token() click to toggle source

return the next token from the source. Returns a string if a token is found, nil if there are no tokens left.

    # File lib/pdf/reader/buffer.rb, line 113
    def token
      reset_pos
      prepare_tokens if @tokens.size < 3
      merge_indirect_reference
      prepare_tokens if @tokens.size < 3

      @tokens.shift
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.