forked from libapps/libapps-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnassh_buffer_concat.js
More file actions
59 lines (53 loc) · 1.31 KB
/
nassh_buffer_concat.js
File metadata and controls
59 lines (53 loc) · 1.31 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
// Copyright 2020 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Basic buffer that concats inputs together.
*/
import {concatTyped} from './lib_array.js';
import {BufferInterface} from './nassh_buffer_interface.js';
/**
* A very simple buffer that concats inputs together.
*/
export class ConcatBuffer extends BufferInterface {
/**
* @param {boolean=} autoack
* @override
*/
constructor(autoack = false) {
super(autoack);
this.buffer_ = new Uint8Array(0);
this.readPos_ = 0;
}
/**
* @param {!ArrayBuffer|!TypedArray} buffer
* @override
*/
write(buffer) {
const u8 = new Uint8Array(buffer);
this.buffer_ = concatTyped(this.buffer_, u8);
this.unreadCount_ += u8.length;
}
/**
* @param {number} length
* @return {!Uint8Array}
* @override
*/
read(length) {
const ret = this.buffer_.subarray(this.readPos_, this.readPos_ + length);
this.readPos_ += ret.length;
if (this.autoack_) {
this.ack(ret.length);
}
this.unreadCount_ -= ret.length;
return ret;
}
/**
* @param {number} length How many bytes to ack.
* @override
*/
ack(length) {
this.buffer_ = this.buffer_.subarray(length);
this.readPos_ -= length;
}
}