Module: NBTFile::Private::ReadMethods

Includes:
CommonMethods, Tokens
Included in:
CompoundTokenizerState, ListTokenizerState, TopTokenizerState
Defined in:
lib/nbtfile.rb

Instance Method Summary collapse

Methods included from CommonMethods

#sign_bit

Instance Method Details

#read_byte(io) ⇒ Object



147
148
149
# File 'lib/nbtfile.rb', line 147

def read_byte(io)
  read_integer(io, 1)
end

#read_byte_array(io) ⇒ Object



178
179
180
181
182
183
# File 'lib/nbtfile.rb', line 178

def read_byte_array(io)
  length = read_int(io)
  value = read_raw(io, length)
  value._nbtfile_force_encoding("BINARY")
  value
end

#read_double(io) ⇒ Object



167
168
169
# File 'lib/nbtfile.rb', line 167

def read_double(io)
  read_raw(io, 8).unpack("G").first
end

#read_float(io) ⇒ Object



163
164
165
# File 'lib/nbtfile.rb', line 163

def read_float(io)
  read_raw(io, 4).unpack("g").first
end

#read_int(io) ⇒ Object



155
156
157
# File 'lib/nbtfile.rb', line 155

def read_int(io)
  read_integer(io, 4)
end

#read_integer(io, n_bytes) ⇒ Object



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

def read_integer(io, n_bytes)
  raw_value = read_raw(io, n_bytes)
  value = (0...n_bytes).reduce(0) do |accum, n|
    (accum << 8) | raw_value._nbtfile_getbyte(n)
  end
  value -= ((value & sign_bit(n_bytes)) << 1)
  value
end

#read_list_header(io) ⇒ Object



185
186
187
188
189
# File 'lib/nbtfile.rb', line 185

def read_list_header(io)
  list_type = read_type(io)
  list_length = read_int(io)
  [list_type, list_length]
end

#read_long(io) ⇒ Object



159
160
161
# File 'lib/nbtfile.rb', line 159

def read_long(io)
  read_integer(io, 8)
end

#read_raw(io, n_bytes) ⇒ Object

Raises:

  • (EOFError)


132
133
134
135
136
# File 'lib/nbtfile.rb', line 132

def read_raw(io, n_bytes)
  data = io.read(n_bytes)
  raise EOFError unless data and data.length == n_bytes
  data
end

#read_short(io) ⇒ Object



151
152
153
# File 'lib/nbtfile.rb', line 151

def read_short(io)
  read_integer(io, 2)
end

#read_string(io) ⇒ Object



171
172
173
174
175
176
# File 'lib/nbtfile.rb', line 171

def read_string(io)
  length = read_short(io)
  string = read_raw(io, length)
  string._nbtfile_force_encoding("UTF-8")
  string
end

#read_type(io) ⇒ Object



191
192
193
194
195
196
197
198
# File 'lib/nbtfile.rb', line 191

def read_type(io)
  byte = read_byte(io)
  begin
    TOKEN_CLASSES_BY_INDEX.fetch(byte)
  rescue IndexError
    raise RuntimeError, "Unexpected tag ordinal #{byte}"
  end
end

#read_value(io, type, name, state, cont) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/nbtfile.rb', line 200

def read_value(io, type, name, state, cont)
  next_state = state

  case
  when type == TAG_End
    next_state = cont
    value = nil
  when type == TAG_Byte
    value = read_byte(io)
  when type == TAG_Short
    value = read_short(io)
  when type == TAG_Int
    value = read_int(io)
  when type == TAG_Long
    value = read_long(io)
  when type == TAG_Float
    value = read_float(io)
  when type == TAG_Double
    value = read_double(io)
  when type == TAG_Byte_Array
    value = read_byte_array(io)
  when type == TAG_String
    value = read_string(io)
  when type == TAG_List
    list_type, list_length = read_list_header(io)
    next_state = ListTokenizerState.new(state, list_type, list_length)
    value = list_type
  when type == TAG_Compound
    next_state = CompoundTokenizerState.new(state)
  end

  [next_state, type[name, value]]
end