hummus-recipe
Version:
A powerful PDF tool for NodeJS based on HummusJS
68 lines (52 loc) • 1.45 kB
text/coffeescript
try iconv = require 'iconv-lite'
class DecodeStream
constructor: () ->
= 0
= .length
=
UInt8: 1
UInt16: 2
UInt24: 3
UInt32: 4
Int8: 1
Int16: 2
Int24: 3
Int32: 4
Float: 4
Double: 8
for key of Buffer.prototype when key.slice(0, 4) is 'read'
do (key) =>
bytes = [key.replace(/read|[BL]E/g, '')]
this::[key] = ->
ret = [key]()
+= bytes
return ret
readString: (length, encoding = 'ascii') ->
switch encoding
when 'utf16le', 'ucs2', 'utf8', 'ascii'
return .toString(encoding, , += length)
when 'utf16be'
buf = new Buffer()
# swap the bytes
for i in [0...buf.length - 1] by 2
byte = buf[i]
buf[i] = buf[i + 1]
buf[i + 1] = byte
return buf.toString('utf16le')
else
buf = length
if iconv
try
return iconv.decode(buf, encoding)
return buf
readBuffer: (length) ->
return .slice(, += length)
readUInt24BE: ->
return ( << 8) +
readUInt24LE: ->
return + ( << 16)
readInt24BE: ->
return ( << 8) +
readInt24LE: ->
return + ( << 16)
module.exports = DecodeStream