Class: AMFStringBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/flv/amf_string_buffer.rb

Overview

Copyright © 2005 Norman Timmler (inlet media e.K., Hamburg, Germany) All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Instance Method Summary collapse

Constructor Details

#initialize(str = nil) ⇒ AMFStringBuffer

Returns a new instance of AMFStringBuffer.



27
28
29
30
# File 'lib/flv/amf_string_buffer.rb', line 27

def initialize(str = nil)
  @buffer = str || ''
  @pos = 0
end

Instance Method Details

#closeObject



55
56
# File 'lib/flv/amf_string_buffer.rb', line 55

def close
end

#eof?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/flv/amf_string_buffer.rb', line 66

def eof?
  @pos == length
end

#lengthObject



62
63
64
# File 'lib/flv/amf_string_buffer.rb', line 62

def length
  @buffer.length
end

#posObject



70
71
72
# File 'lib/flv/amf_string_buffer.rb', line 70

def pos
  @pos
end

#read(length) ⇒ Object

Raises:

  • (EOFError)


36
37
38
39
40
41
# File 'lib/flv/amf_string_buffer.rb', line 36

def read(length)
  raise EOFError if @pos + length > @buffer.length
  rt = @buffer[@pos, length]
  @pos += length
  rt
end

#read__AMF_arrayObject



107
108
109
110
111
112
113
114
115
# File 'lib/flv/amf_string_buffer.rb', line 107

def read__AMF_array
  size = read__UI32
  array = []
  (1..size).step do |pos|  
    break if eof?
    array << read__AMF_data
  end
  array
end

#read__AMF_booleanObject



82
83
84
# File 'lib/flv/amf_string_buffer.rb', line 82

def read__AMF_boolean
  read__UI8 == 1
end

#read__AMF_data(type = nil) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/flv/amf_string_buffer.rb', line 122

def read__AMF_data(type = nil)
  type ||= read__UI8
  value = case type.to_i
  when  0
    read__AMF_double
  when  1
    read__AMF_boolean
  when  2
    read__AMF_string
  when  3
    read__AMF_object
  when  8
    read__AMF_mixed_array
  when 10
    read__AMF_array
  when 11
    read__AMF_date
  else
  end
  return value
end

#read__AMF_dateObject



117
118
119
120
# File 'lib/flv/amf_string_buffer.rb', line 117

def read__AMF_date
  utc_time = Time.at((read__AMF_double / 1000).to_i)
  utc_time + (read__SI16 * 60) - Time.now.gmtoff
end

#read__AMF_doubleObject



78
79
80
# File 'lib/flv/amf_string_buffer.rb', line 78

def read__AMF_double
  num = read(8).unpack('G').first.to_f
end

#read__AMF_mixed_arrayObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/flv/amf_string_buffer.rb', line 86

def read__AMF_mixed_array
  size = read__UI32 # is not used
  hash = {}
  while !eof?
    key = read__AMF_string
    break if key.empty? && (type = read__UI8) == 9
    hash[key] = read__AMF_data(type)
  end
  hash
end

#read__AMF_objectObject



97
98
99
100
101
102
103
104
105
# File 'lib/flv/amf_string_buffer.rb', line 97

def read__AMF_object
  object = Object.new
  while !eof?
    key = read__AMF_string
    break if key.empty? && (type = read__UI8) == 9
    object.instance_variable_set( eval(":@#{key}"), read__AMF_data(type) )
  end
  object
end

#read__AMF_stringObject



74
75
76
# File 'lib/flv/amf_string_buffer.rb', line 74

def read__AMF_string
  read(read__UI16)
end

#read__SI16Object



246
247
248
# File 'lib/flv/amf_string_buffer.rb', line 246

def read__SI16
  read(2).reverse.unpack('s').first.to_i
end

#read__STRING(length) ⇒ Object



242
243
244
# File 'lib/flv/amf_string_buffer.rb', line 242

def read__STRING(length)
  read length
end

#read__UI16Object



230
231
232
# File 'lib/flv/amf_string_buffer.rb', line 230

def read__UI16
  (readchar << 8) + readchar
end

#read__UI24Object



234
235
236
# File 'lib/flv/amf_string_buffer.rb', line 234

def read__UI24
  (readchar << 16) + (readchar << 8) + readchar
end

#read__UI32Object



238
239
240
# File 'lib/flv/amf_string_buffer.rb', line 238

def read__UI32
  (readchar << 24) + (readchar << 16) + (readchar << 8) + readchar
end

#read__UI8Object

FIXME: This methods are copied from flv_stream.rb. Should get in here per include? or something like this.



226
227
228
# File 'lib/flv/amf_string_buffer.rb', line 226

def read__UI8
  readchar
end

#readcharObject



43
44
45
# File 'lib/flv/amf_string_buffer.rb', line 43

def readchar
  read(1).unpack('C').first
end

#seek(pos) ⇒ Object



32
33
34
# File 'lib/flv/amf_string_buffer.rb', line 32

def seek(pos)
  @pos = pos
end

#to_sObject



58
59
60
# File 'lib/flv/amf_string_buffer.rb', line 58

def to_s
  @buffer
end

#write(str) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/flv/amf_string_buffer.rb', line 47

def write(str)
  if @pos + str.length > @buffer.length
    @buffer << ' ' * (@pos + str.length - @buffer.length)
  end
  @buffer[@pos, str.length] = str
  @pos += str.length
end

#write__AMF_array(array) ⇒ Object



203
204
205
206
207
208
209
210
# File 'lib/flv/amf_string_buffer.rb', line 203

def write__AMF_array(array)
  write__UI8 10
  write__UI32 array.length
  
  array.each do |value|
    write__AMF_data value
  end
end

#write__AMF_boolean(value) ⇒ Object



155
156
157
158
159
# File 'lib/flv/amf_string_buffer.rb', line 155

def write__AMF_boolean(value)
  write__UI8 1
  value = value ? 1 : 0
  write [value].pack('C')
end

#write__AMF_data(object) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/flv/amf_string_buffer.rb', line 167

def write__AMF_data(object)
  if object === true || object === false
    write__AMF_boolean object
  elsif object.kind_of? Numeric
    write__AMF_double object
  elsif object.kind_of? Time
    write__AMF_date object
  elsif object.kind_of? Hash
    write__AMF_mixed_array object
  elsif object.kind_of? String
    write__AMF_string object
  elsif object.kind_of? Array
    write__AMF_array object
  else
    write__AMF_object object
  end
end

#write__AMF_date(time) ⇒ Object



161
162
163
164
165
# File 'lib/flv/amf_string_buffer.rb', line 161

def write__AMF_date(time)
  write__UI8 11
  write [(time.to_f * 1000.0)].pack('G')
  write__SI16( (Time.now.gmtoff / 60).to_i ) 
end

#write__AMF_double(value) ⇒ Object



150
151
152
153
# File 'lib/flv/amf_string_buffer.rb', line 150

def write__AMF_double(value)
  write__UI8 0
  write [value].pack('G')
end

#write__AMF_key(key) ⇒ Object



185
186
187
188
# File 'lib/flv/amf_string_buffer.rb', line 185

def write__AMF_key(key)
  write__UI16 key.length
  write key
end

#write__AMF_mixed_array(hash) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/flv/amf_string_buffer.rb', line 190

def write__AMF_mixed_array(hash)
  write__UI8 8
  write__UI32 hash.length # length will never be read
  
  hash.each_pair do |key, value|
    write__AMF_key key
    write__AMF_data value
  end
  
  write__UI16 0
  write__UI8 9
end

#write__AMF_object(object) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
# File 'lib/flv/amf_string_buffer.rb', line 212

def write__AMF_object(object)
  write__UI8 3
  
  object.instance_variables.each do |variable|
    write__AMF_key variable.gsub('@', '')
    write__AMF_data object.instance_variable_get( variable.intern )
  end
  
  write__UI16 0
  write__UI8 9
end

#write__AMF_string(str) ⇒ Object



144
145
146
147
148
# File 'lib/flv/amf_string_buffer.rb', line 144

def write__AMF_string(str)
  write__UI8 2
  write__UI16 str.length
  write str
end

#write__SI16(value) ⇒ Object



269
270
271
272
# File 'lib/flv/amf_string_buffer.rb', line 269

def write__SI16(value)
  write [(value >> 8) & 0xff].pack('c')
  write [value & 0xff].pack('c')
end

#write__STRING(string) ⇒ Object Also known as: <<



274
275
276
# File 'lib/flv/amf_string_buffer.rb', line 274

def write__STRING(string)
  write string
end

#write__UI16(value) ⇒ Object



254
255
256
257
# File 'lib/flv/amf_string_buffer.rb', line 254

def write__UI16(value)
  write [(value >> 8) & 0xff].pack('c')
  write [value & 0xff].pack('c')
end

#write__UI24(value) ⇒ Object



259
260
261
262
263
# File 'lib/flv/amf_string_buffer.rb', line 259

def write__UI24(value)
  write [value >> 16].pack('c')
  write [(value >> 8) & 0xff].pack('c')
  write [value & 0xff].pack('c')
end

#write__UI32(value) ⇒ Object



265
266
267
# File 'lib/flv/amf_string_buffer.rb', line 265

def write__UI32(value)
  write [value].pack('N')
end

#write__UI8(value) ⇒ Object



250
251
252
# File 'lib/flv/amf_string_buffer.rb', line 250

def write__UI8(value)
  write [value].pack('C')
end