-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuffer.js
More file actions
111 lines (98 loc) · 2.78 KB
/
Copy pathbuffer.js
File metadata and controls
111 lines (98 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"use strict";
var $size = "@@channel/size"
var $buffer = "@@buffer/buffer"
function FixedBuffer(size) {
this[$size] = size
this[$buffer] = []
}
FixedBuffer.prototype = {
constructor: FixedBuffer,
get size() {
return this[$size]
},
isEmpty: function() {
return this[$buffer].length === 0
},
isFull: function() {
return this[$buffer].length === this[$size]
},
put: function(item) {
if (this.isFull())
throw Error("Can't put to a full buffer")
this[$buffer].unshift(item)
},
take: function(item) {
if (this.isEmpty())
throw Error("Can't take from empty buffer")
return this[$buffer].pop()
}
}
exports.FixedBuffer = FixedBuffer
function UnblockingBuffer(size) {
FixedBuffer.call(this, size)
}
UnblockingBuffer.prototype = Object.create(FixedBuffer.prototype)
UnblockingBuffer.prototype.constructor = UnblockingBuffer
UnblockingBuffer.prototype.isFull = function() {
return false;
}
UnblockingBuffer.prototype.put = function() {
throw TypeError("Subclass must implement `put` method");
}
exports.UnblockingBuffer = UnblockingBuffer
function SlidingBuffer(size) {
UnblockingBuffer.call(this, size)
}
SlidingBuffer.prototype = Object.create(UnblockingBuffer.prototype)
SlidingBuffer.prototype.constructor = SlidingBuffer
SlidingBuffer.prototype.put = function(item) {
if (this[$buffer].length === this[$size])
this.take()
this[$buffer].unshift(item)
}
exports.SlidingBuffer = SlidingBuffer
function DroppingBuffer(size) {
UnblockingBuffer.call(this, size)
}
DroppingBuffer.prototype = Object.create(DroppingBuffer.prototype)
DroppingBuffer.prototype.constructor = DroppingBuffer
DroppingBuffer.prototype.put = function(item) {
if (this[$buffer].length !== this[$size])
this[$buffer].unshift(item)
}
exports.DroppingBuffer = DroppingBuffer
function ByteBuffer(byteLength) {
this[$size] = byteLength
this[$buffer] = []
this.byteLength = 0
}
ByteBuffer.prototype = Object.create(FixedBuffer.prototype)
ByteBuffer.prototype.constructor = ByteBuffer
ByteBuffer.prototype.isFull = function() {
return this[$size] <= this.byteLength
}
ByteBuffer.prototype.put = function(item) {
if (!(item instanceof ArrayBuffer))
throw TypeError("Can only put ArrayBuffer")
FixedBuffer.prototype.put.call(this, item)
this.byteLength = this.byteLength + item.byteLength
}
ByteBuffer.prototype.take = function() {
var result = new ArrayBuffer(this.byteLength)
var chunks = this[$buffer].splice(0)
this.byteLength = 0
var index = 0
var offset = 0
while (index < chunks.length) {
var chunk = chunks[index]
var element = 0
while (element < chunk.byteLength) {
result[offset] = chunk[element]
element = element + 1
offset = offset + 1
}
index = index + 1
}
return result
}
exports.ByteBuffer = ByteBuffer