Class: Tar::FileReader

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tar/file_reader.rb,
lib/tar/file_reader/line.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, io, external_encoding: Encoding.default_external, internal_encoding: Encoding.default_internal, **encoding_options) ⇒ FileReader

Returns a new instance of FileReader.



17
18
19
20
21
22
23
24
# File 'lib/tar/file_reader.rb', line 17

def initialize(header, io, external_encoding: Encoding.default_external, internal_encoding: Encoding.default_internal, **encoding_options)
  @header = header
  @io = io
  @closed = false
  @lineno = 0
  @pos = 0
  set_encoding external_encoding, internal_encoding, **encoding_options
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



15
16
17
# File 'lib/tar/file_reader.rb', line 15

def header
  @header
end

Instance Method Details

#binmodeObject



123
124
125
# File 'lib/tar/file_reader.rb', line 123

def binmode
  set_encoding Encoding::BINARY
end

#binmode?Boolean

Returns:

  • (Boolean)


127
128
129
130
# File 'lib/tar/file_reader.rb', line 127

def binmode?
  check_not_closed!
  @external_encoding == Encoding::BINARY && @internal_encoding.nil?
end

#bytes(&block) ⇒ Object



176
177
178
179
# File 'lib/tar/file_reader.rb', line 176

def bytes(&block)
  warn "warning: #{self.class}#bytes is deprecated; use #each_byte instead"
  each_byte(&block)
end

#chars(&block) ⇒ Object



219
220
221
222
# File 'lib/tar/file_reader.rb', line 219

def chars(&block)
  warn "warning: #{self.class}#chars is deprecated; use #each_char instead"
  each_char(&block)
end

#closeObject



26
27
28
# File 'lib/tar/file_reader.rb', line 26

def close
  @closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/tar/file_reader.rb', line 30

def closed?
  @closed || @io.closed?
end

#codepoints(&block) ⇒ Object



234
235
236
237
# File 'lib/tar/file_reader.rb', line 234

def codepoints(&block)
  warn "warning: #{self.class}#codepoints is deprecated; use #each_codepoint instead"
  each_codepoint(&block)
end

#each_byteObject



170
171
172
173
174
# File 'lib/tar/file_reader.rb', line 170

def each_byte
  check_not_closed!
  return to_enum(__method__) unless block_given?
  yield getbyte until eof?
end

#each_charObject



213
214
215
216
217
# File 'lib/tar/file_reader.rb', line 213

def each_char
  check_not_closed!
  return to_enum(__method__) unless block_given?
  yield getc until eof?
end

#each_codepointObject



224
225
226
227
228
229
230
231
232
# File 'lib/tar/file_reader.rb', line 224

def each_codepoint
  check_not_closed!
  return to_enum(__method__) unless block_given?
  each_char do |char|
    char.each_codepoint do |codepoint|
      yield codepoint
    end
  end
end

#each_line(*args) ⇒ Object Also known as: each



253
254
255
256
257
258
# File 'lib/tar/file_reader.rb', line 253

def each_line(*args)
  line = Line.new(self, *args)
  check_not_closed!
  return to_enum(__method__, *args) unless block_given?
  yield line.read until eof?
end

#eof?Boolean Also known as: eof

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/tar/file_reader.rb', line 34

def eof?
  check_not_closed!
  @pos >= @header.size
end

#external_encodingObject



99
100
101
102
# File 'lib/tar/file_reader.rb', line 99

def external_encoding
  check_not_closed!
  @external_encoding
end

#getbyteObject



151
152
153
154
155
156
# File 'lib/tar/file_reader.rb', line 151

def getbyte
  check_not_closed!
  return nil if eof?
  @pos += 1
  @io.getbyte
end

#getcObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/tar/file_reader.rb', line 181

def getc
  check_not_closed!
  return nil if eof?

  char = String.new(encoding: Encoding::BINARY)
  min_char_size, max_char_size = CharSize.minmax(external_encoding)

  until char.size == max_char_size || eof?
    char << read(min_char_size)

    char.force_encoding external_encoding
    return encode(char) if char.valid_encoding?
    char.force_encoding Encoding::BINARY
  end

  undo_getc_attempt char, min_char_size

  encode(char)
end

#gets(*args) ⇒ Object



239
240
241
242
243
244
# File 'lib/tar/file_reader.rb', line 239

def gets(*args)
  line = Line.new(self, *args)
  check_not_closed!
  return nil if eof?
  line.read
end

#internal_encodingObject



104
105
106
107
# File 'lib/tar/file_reader.rb', line 104

def internal_encoding
  check_not_closed!
  @internal_encoding
end

#linenoObject



55
56
57
58
# File 'lib/tar/file_reader.rb', line 55

def lineno
  check_not_closed!
  @lineno
end

#lineno=(new_lineno) ⇒ Object



60
61
62
63
# File 'lib/tar/file_reader.rb', line 60

def lineno=(new_lineno)
  check_not_closed!
  @lineno = new_lineno
end

#lines(*args, &block) ⇒ Object



261
262
263
264
# File 'lib/tar/file_reader.rb', line 261

def lines(*args, &block)
  warn "warning: #{self.class}#lines is deprecated; use #each_line instead"
  each_line(*args, &block)
end

#pendingObject



50
51
52
53
# File 'lib/tar/file_reader.rb', line 50

def pending
  check_not_closed!
  [0, @header.size - @pos].max
end

#posObject Also known as: tell



40
41
42
43
# File 'lib/tar/file_reader.rb', line 40

def pos
  check_not_closed!
  @pos
end

#pos=(new_pos) ⇒ Object



46
47
48
# File 'lib/tar/file_reader.rb', line 46

def pos=(new_pos)
  seek new_pos
end

#read(length = nil, buffer = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tar/file_reader.rb', line 65

def read(length = nil, buffer = nil)
  check_not_closed!

  data = @io.read(truncate(length), buffer)
  @pos += data.bytesize

  if length.nil?
    encode(data)
  else
    data.force_encoding(Encoding::BINARY)
  end
end

#readbyteObject



164
165
166
167
168
# File 'lib/tar/file_reader.rb', line 164

def readbyte
  check_not_closed!
  check_not_eof!
  getbyte
end

#readcharObject



207
208
209
210
211
# File 'lib/tar/file_reader.rb', line 207

def readchar
  check_not_closed!
  check_not_eof!
  getc
end

#readline(*args) ⇒ Object



246
247
248
249
250
251
# File 'lib/tar/file_reader.rb', line 246

def readline(*args)
  line = Line.new(self, *args)
  check_not_closed!
  check_not_eof!
  line.read
end

#readlines(*args) ⇒ Object



266
267
268
# File 'lib/tar/file_reader.rb', line 266

def readlines(*args)
  each_line(*args).to_a
end

#readpartial(max_length, buffer = nil) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/tar/file_reader.rb', line 78

def readpartial(max_length, buffer = nil)
  check_not_closed!

  data = @io.readpartial(truncate(max_length), buffer)
  @pos += data.bytesize
  data.force_encoding(Encoding::BINARY)
end

#rewindObject



146
147
148
149
# File 'lib/tar/file_reader.rb', line 146

def rewind
  seek 0
  @lineno = 0
end

#seek(amount, mode = IO::SEEK_SET) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/tar/file_reader.rb', line 138

def seek(amount, mode = IO::SEEK_SET)
  check_seekable!
  check_not_closed!
  offset = relativize(amount, mode)
  @io.seek offset, IO::SEEK_CUR
  @pos += offset
end

#set_encoding(external_encoding, *internal_encoding, **encoding_options) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/tar/file_reader.rb', line 109

def set_encoding(external_encoding, *internal_encoding, **encoding_options)
  check_not_closed!

  external_encoding, internal_encoding = extract_encodings(external_encoding, *internal_encoding)

  if parse_bom?(external_encoding)
    external_encoding = parse_bom || external_encoding[4..-1]
  end

  @external_encoding = find_encoding(external_encoding, if_nil: Encoding.default_external, if_unsupported: Encoding.default_external)
  @internal_encoding = find_encoding(internal_encoding, if_nil: nil, if_unsupported: Encoding.default_internal)
  @encoding_options = encoding_options
end

#skip_to_next_recordObject



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/tar/file_reader.rb', line 86

def skip_to_next_record
  check_not_closed!

  target_pos = USTAR.records_size(@header.size)

  if seekable?
    seek target_pos
  else
    @io.read(target_pos - @pos)
    @pos = target_pos
  end
end

#tty?Boolean Also known as: isatty

Returns:

  • (Boolean)


132
133
134
135
# File 'lib/tar/file_reader.rb', line 132

def tty?
  check_not_closed!
  @io.respond_to?(:tty?) && @io.tty?
end

#ungetbyte(byte) ⇒ Object



158
159
160
161
162
# File 'lib/tar/file_reader.rb', line 158

def ungetbyte(byte)
  check_not_closed!
  @pos -= 1
  @io.ungetbyte byte
end

#ungetc(char) ⇒ Object



201
202
203
204
205
# File 'lib/tar/file_reader.rb', line 201

def ungetc(char)
  char.encode(external_encoding).bytes.reverse_each do |byte|
    ungetbyte byte
  end
end