-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuffer.js
More file actions
95 lines (83 loc) · 2.45 KB
/
buffer.js
File metadata and controls
95 lines (83 loc) · 2.45 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
/**
* @fileOverview A simple text Buffer class for composing strings.
*/
var ByteString = require('binary').ByteString;
var strings = require('./strings');
/**
* A Buffer class for composing strings. This is implemented
* as a simple wrapper around a JavaScript array.
* @param ... initial parts to write to the buffer
*/
var Buffer = exports.Buffer = function() {
var content = [],
length = 0;
/**
* Reset the buffer discarding all its content.
* @returns this buffer object
*/
this.reset = function() {
content = [];
length = 0;
return this;
};
/**
* Append all arguments to this buffer.
* @param ... variable arguments to append to the buffer
* @returns this buffer object
*/
this.write = function() {
for (var i = 0; i < arguments.length; i++) {
var str = String(arguments[i]);
content.push(str);
length += str.length;
}
return this;
};
/**
* Append all arguments to this buffer terminated by a carriage return/newline sequence.
* @param ... variable arguments to append to the buffer
* @returns this buffer object
*/
this.writeln = function() {
this.write.apply(this, arguments);
content.push("\r\n");
length += 2;
return this;
};
/**
* Return the content of this buffer as string.
*/
this.toString = function() {
return content.join('');
};
/**
* Call function <code>fn</code> with each content part in this buffer.
* @param fn a function to apply to each buffer part
*/
this.forEach = function(fn) {
content.forEach(fn);
};
/**
* A read-only property containing the number of characters currently
* contained by this buffer.
*/
Object.defineProperty(this, "length", {
get: function() { return length; }
});
/**
* Get a message digest on the content of this buffer.
* @param algorithm the algorithm to use, defaults to MD5
*/
this.digest = function(algorithm) {
var md = java.security.MessageDigest.getInstance(algorithm || "MD5");
content.forEach(function(part) {
md.update(String(part).toByteString());
});
var b = ByteString.wrap(md.digest());
return strings.b16encode(b);
};
if (arguments.length > 0) {
this.write.apply(this, arguments);
}
return this;
}