blob: e5850509df06cff0e01aeb7f424b6527565209e4 (
plain)
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
|
%
% Copyright (c) ZeroC, Inc. All rights reserved.
%
classdef Buffer < matlab.mixin.Copyable
methods
function obj = Buffer(data)
if nargin == 1
%
% Caller provided a uint8 array to be used for unmarshaling.
%
obj.buf = data;
obj.size = length(data);
obj.capacity = obj.size;
else
%
% This buffer will be used for marshaling and grows as necessary.
%
% Use zeros() here. Using "uint8(0)" isn't sufficient.
%
obj.buf = zeros(1, 1024, 'uint8');
obj.size = 0;
obj.capacity = length(obj.buf);
end
end
function reset(obj, data)
if nargin == 2
obj.buf = data;
obj.size = length(data);
obj.capacity = obj.size;
else
%
% Reuse the existing buffer.
%
obj.size = 0;
end
end
function expand(obj, n)
obj.resize(obj.size + n);
end
function resize(obj, n)
if n > obj.capacity
obj.capacity = max(n, 2 * obj.capacity);
obj.buf(obj.capacity) = uint8(0); % Expand the array
end
obj.size = n;
end
function pushByte(obj, b)
s = obj.size + 1;
if s <= obj.capacity
obj.buf(s) = b;
obj.size = s;
else
obj.resize(s);
obj.buf(s) = b;
end
end
function push(obj, b)
n = length(b);
if n == 1
obj.pushByte(b);
else
sz = obj.size;
if sz + n <= obj.capacity
obj.buf(sz + 1:sz + n) = b;
obj.size = obj.size + n;
else
obj.resize(sz + n);
obj.buf(sz + 1:sz + n) = b;
end
end
end
end
properties
%
% Public properties to reduce method calls
%
buf
size uint32
capacity uint32
end
end
|