hummus-recipe
Version:
A powerful PDF tool for NodeJS based on HummusJS
102 lines (83 loc) • 2.46 kB
text/coffeescript
stream = require 'stream'
DecodeStream = require './DecodeStream'
try iconv = require 'iconv-lite'
class EncodeStream extends stream.Readable
constructor: (bufferSize = 65536) ->
super
= new Buffer(bufferSize)
= 0
= 0
for key of Buffer.prototype when key.slice(0, 5) is 'write'
do (key) =>
bytes = +DecodeStream.TYPES[key.replace(/write|[BL]E/g, '')]
EncodeStream::[key] = (value) ->
bytes
[key](value, )
+= bytes
+= bytes
_read: ->
# do nothing, required by node
ensure: (bytes) ->
if + bytes > .length
flush: ->
if > 0
new Buffer .slice(0, )
= 0
writeBuffer: (buffer) ->
buffer
+= buffer.length
writeString: (string, encoding = 'ascii') ->
switch encoding
when 'utf16le', 'ucs2', 'utf8', 'ascii'
new Buffer(string, encoding)
when 'utf16be'
buf = new Buffer(string, 'utf16le')
# swap the bytes
for i in [0...buf.length - 1] by 2
byte = buf[i]
buf[i] = buf[i + 1]
buf[i + 1] = byte
buf
else
if iconv
iconv.encode(string, encoding)
else
throw new Error 'Install iconv-lite to enable additional string encodings.'
writeUInt24BE: (val) ->
3
[++] = val >>> 16 & 0xff
[++] = val >>> 8 & 0xff
[++] = val & 0xff
+= 3
writeUInt24LE: (val) ->
3
[++] = val & 0xff
[++] = val >>> 8 & 0xff
[++] = val >>> 16 & 0xff
+= 3
writeInt24BE: (val) ->
if val >= 0
val
else
val + 0xffffff + 1
writeInt24LE: (val) ->
if val >= 0
val
else
val + 0xffffff + 1
fill: (val, length) ->
if length < .length
length
.fill(val, , + length)
+= length
+= length
else
buf = new Buffer(length)
buf.fill(val)
buf
end: ->
null
module.exports = EncodeStream