Skip to content

Commit f648aca

Browse files
author
Daniel Wirtz
committed
#mark
1 parent b6c2f88 commit f648aca

File tree

10 files changed

+423
-64
lines changed

10 files changed

+423
-64
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
language: node_js
22
node_js:
33
- 0.8
4-
- 0.9
4+
- 0.10

ByteBuffer.js

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@
6161
*/
6262
this.offset = 0;
6363

64+
/**
65+
* Marked offset set through {@link ByteBuffer#mark}. Evaluates to -1 if there is no marked offset.
66+
* @type {number}
67+
* @expose
68+
*/
69+
this.markedOffset = -1;
70+
6471
/**
6572
* Length of the contained data. Offset- and capacity-independent index. Contents are the bytes between offset and
6673
* length, which are both absolute indexes. There is no capacity property, use {@link ByteBuffer#capacity}
@@ -320,7 +327,7 @@
320327
};
321328

322329
/**
323-
* Flips the ByteBuffer. Sets length=offset and offset=0.
330+
* Makes the buffer ready for a new sequence of write or relative read operations. Sets length=offset and offset=0.
324331
* @return {ByteBuffer} this
325332
* @expose
326333
*/
@@ -331,13 +338,41 @@
331338
};
332339

333340
/**
334-
* Resets the ByteBuffer. Sets offset=0 and length=0.
341+
* Marks the current offset in {@link ByteBuffer#markedOffset}.
342+
* @param {number=} offset Offset to mark. Defaults to {@link ByteBuffer#offset}.
335343
* @return {ByteBuffer} this
344+
* @throws {Error} If the mark cannot be set
345+
* @see ByteBuffer#reset
346+
* @expose
347+
*/
348+
ByteBuffer.prototype.mark = function(offset) {
349+
if (this.array == null) {
350+
throw(new Error(this+" cannot be marked: Already destroyed"));
351+
}
352+
offset = typeof offset != 'undefined' ? parseInt(offset, 10) : this.offset;
353+
if (offset < 0 || offset > this.array.byteLength) {
354+
throw(new Error(this+" cannot be marked: Offset to mark is less than 0 or bigger than the capacity ("+this.array.byteLength+"): "+offset));
355+
}
356+
this.markedOffset = offset;
357+
return this;
358+
};
359+
360+
/**
361+
* Resets the ByteBuffer. If an offset has been marked through {@link ByteBuffer#mark} before, the offset will be
362+
* set to the marked offset and the marked offset will be discarded. Length will not be altered. If there is no
363+
* marked offset, sets offset=0 and length=0.
364+
* @return {ByteBuffer} this
365+
* @see ByteBuffer#mark
336366
* @expose
337367
*/
338368
ByteBuffer.prototype.reset = function() {
339-
this.offset = 0;
340-
this.length = 0;
369+
if (this.markedOffset >= 0) {
370+
this.offset = this.markedOffset;
371+
this.markedOffset = -1;
372+
} else {
373+
this.offset = 0;
374+
this.length = 0;
375+
}
341376
return this;
342377
};
343378

@@ -1488,7 +1523,7 @@
14881523
* @expose
14891524
*/
14901525
ByteBuffer.prototype.printDebug = function(out) {
1491-
var s = (this.array != null ? "ByteBuffer(offset="+this.offset+",length="+this.length+",capacity="+this.array.byteLength+")" : "ByteBuffer(DESTROYED)")+"\n"+
1526+
var s = (this.array != null ? "ByteBuffer(offset="+this.offset+",markedOffset="+this.markedOffset+",length="+this.length+",capacity="+this.array.byteLength+")" : "ByteBuffer(DESTROYED)")+"\n"+
14921527
"-------------------------------------------------------------------\n";
14931528
var h = this.toHex(16, true);
14941529
var a = this.toASCII(16, true);
@@ -1583,14 +1618,14 @@
15831618

15841619
/**
15851620
* Returns a string representation.
1586-
* @return {string} String representation as of "ByteBuffer(offset=...,length=...,capacity=...)"
1621+
* @return {string} String representation as of "ByteBuffer(offset=...,markedOffset=...,length=...,capacity=...)"
15871622
* @expose
15881623
*/
15891624
ByteBuffer.prototype.toString = function() {
15901625
if (this.array == null) {
15911626
return "ByteBuffer(DESTROYED)";
15921627
}
1593-
return "ByteBuffer(offset="+this.offset+",length="+this.length+",capacity="+this.array.byteLength+")";
1628+
return "ByteBuffer(offset="+this.offset+",markedOffset="+this.markedOffset+",length="+this.length+",capacity="+this.array.byteLength+")";
15941629
};
15951630

15961631
/**

ByteBuffer.min.js

Lines changed: 34 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ByteBuffer.min.map

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

ByteBuffer.noexpose.js

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@
5757
*/
5858
this.offset = 0;
5959

60+
/**
61+
* Marked offset set through {@link ByteBuffer#mark}. Evaluates to -1 if there is no marked offset.
62+
* @type {number}
63+
*/
64+
this.markedOffset = -1;
65+
6066
/**
6167
* Length of the contained data. Offset- and capacity-independent index. Contents are the bytes between offset and
6268
* length, which are both absolute indexes. There is no capacity property, use {@link ByteBuffer#capacity}
@@ -294,7 +300,7 @@
294300
};
295301

296302
/**
297-
* Flips the ByteBuffer. Sets length=offset and offset=0.
303+
* Makes the buffer ready for a new sequence of write or relative read operations. Sets length=offset and offset=0.
298304
* @return {ByteBuffer} this
299305
*/
300306
ByteBuffer.prototype.flip = function() {
@@ -304,12 +310,39 @@
304310
};
305311

306312
/**
307-
* Resets the ByteBuffer. Sets offset=0 and length=0.
313+
* Marks the current offset in {@link ByteBuffer#markedOffset}.
314+
* @param {number=} offset Offset to mark. Defaults to {@link ByteBuffer#offset}.
308315
* @return {ByteBuffer} this
316+
* @throws {Error} If the mark cannot be set
317+
* @see ByteBuffer#reset
318+
*/
319+
ByteBuffer.prototype.mark = function(offset) {
320+
if (this.array == null) {
321+
throw(new Error(this+" cannot be marked: Already destroyed"));
322+
}
323+
offset = typeof offset != 'undefined' ? parseInt(offset, 10) : this.offset;
324+
if (offset < 0 || offset > this.array.byteLength) {
325+
throw(new Error(this+" cannot be marked: Offset to mark is less than 0 or bigger than the capacity ("+this.array.byteLength+"): "+offset));
326+
}
327+
this.markedOffset = offset;
328+
return this;
329+
};
330+
331+
/**
332+
* Resets the ByteBuffer. If an offset has been marked through {@link ByteBuffer#mark} before, the offset will be
333+
* set to the marked offset and the marked offset will be discarded. Length will not be altered. If there is no
334+
* marked offset, sets offset=0 and length=0.
335+
* @return {ByteBuffer} this
336+
* @see ByteBuffer#mark
309337
*/
310338
ByteBuffer.prototype.reset = function() {
311-
this.offset = 0;
312-
this.length = 0;
339+
if (this.markedOffset >= 0) {
340+
this.offset = this.markedOffset;
341+
this.markedOffset = -1;
342+
} else {
343+
this.offset = 0;
344+
this.length = 0;
345+
}
313346
return this;
314347
};
315348

@@ -1399,7 +1432,7 @@
13991432
* Defaults to call console.log.
14001433
*/
14011434
ByteBuffer.prototype.printDebug = function(out) {
1402-
var s = (this.array != null ? "ByteBuffer(offset="+this.offset+",length="+this.length+",capacity="+this.array.byteLength+")" : "ByteBuffer(DESTROYED)")+"\n"+
1435+
var s = (this.array != null ? "ByteBuffer(offset="+this.offset+",markedOffset="+this.markedOffset+",length="+this.length+",capacity="+this.array.byteLength+")" : "ByteBuffer(DESTROYED)")+"\n"+
14031436
"-------------------------------------------------------------------\n";
14041437
var h = this.toHex(16, true);
14051438
var a = this.toASCII(16, true);
@@ -1492,13 +1525,13 @@
14921525

14931526
/**
14941527
* Returns a string representation.
1495-
* @return {string} String representation as of "ByteBuffer(offset=...,length=...,capacity=...)"
1528+
* @return {string} String representation as of "ByteBuffer(offset=...,markedOffset=...,length=...,capacity=...)"
14961529
*/
14971530
ByteBuffer.prototype.toString = function() {
14981531
if (this.array == null) {
14991532
return "ByteBuffer(DESTROYED)";
15001533
}
1501-
return "ByteBuffer(offset="+this.offset+",length="+this.length+",capacity="+this.array.byteLength+")";
1534+
return "ByteBuffer(offset="+this.offset+",markedOffset="+this.markedOffset+",length="+this.length+",capacity="+this.array.byteLength+")";
15021535
};
15031536

15041537
/**

0 commit comments

Comments
 (0)