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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Byte.Block;
interface
uses
Boolean,
Byte,
Integer._64.Positive,
Mathematics.Sign,
Stream.Input,
Stream.Output,
Stream.SerializableObject;
type
TByteBlock = class(TSerializableObject)
public
procedure Read(Stream: TInputStream); override;
procedure Write(Stream: TOutputStream); override;
function GetByteCount(): TPositiveInteger64; virtual; abstract;
procedure SetByteCount(const Value: TPositiveInteger64); virtual; abstract;
function CreateInputStream(Offset: TPositiveInteger64 = 0): TInputStream; virtual; abstract;
function CreateOverwriteStream(Offset: TPositiveInteger64 = 0): TOutputStream; virtual; abstract;
function CreateRewriteStream(): TOutputStream; virtual; abstract;
function CreateAppendStream(): TOutputStream; virtual; abstract;
procedure Clear(); virtual;
procedure Append(Stream: TInputStream); virtual;
procedure AssignFrom(Block: TByteBlock); virtual;
procedure CopyFrom(Block: TByteBlock); virtual;
procedure ReadFromObject(SerializableObject: TSerializableObject);
procedure WriteToObject(SerializableObject: TSerializableObject);
function Equal(Block: TByteBlock): TBoolean; virtual;
function ToByteArray(): TArray<TByte>; virtual;
public
class function Compare(Left: TByteBlock; Right: TByteBlock): TSign;
end;
implementation
uses
Exception.NotImplemented,
Integer._32,
Integer._32.Positive,
Mathematics,
Stream;
{ TByteBlock }
procedure TByteBlock.Append(Stream: TInputStream);
var
Output: TOutputStream;
begin
Output := CreateAppendStream();
while not Stream.AtEnd() do
CopyStream(Stream, Output, Stream.Available());
Output.Free();
end;
procedure TByteBlock.AssignFrom(Block: TByteBlock);
begin
raise ENotImplemented.Create(ClassType, 'AssignFrom');
end;
procedure TByteBlock.Clear();
begin
raise ENotImplemented.Create(ClassType, 'Clear');
end;
class function TByteBlock.Compare(Left: TByteBlock; Right: TByteBlock): TSign;
var
LeftStream: TInputStream;
RightStream: TInputStream;
LeftSize: TPositiveInteger64;
RightSize: TPositiveInteger64;
BytesToRead: TInteger32;
BufferLeft: array [0..1023] of TByte;
BufferRight: array [0..1023] of TByte;
BufferSize: TInteger32;
Count: TPositiveInteger64;
i: TInteger32;
begin
LeftStream := Left.CreateInputStream();
RightStream := Right.CreateInputStream();
LeftSize := Left.GetByteCount();
RightSize := Right.GetByteCount();
Result := sZero;
Count := Minimum(LeftSize, RightSize);
BufferSize := Length(BufferLeft);
while (Result = sZero) and (Count > 0) do
begin
BytesToRead := Minimum(Count, BufferSize);
LeftStream.Read(BufferLeft, BytesToRead);
RightStream.Read(BufferRight, BytesToRead);
i := 0;
while (Result = sZero) and (i < BytesToRead) do
begin
if BufferLeft[i] > BufferRight[i] then
Result := sPositive
else if BufferLeft[i] < BufferRight[i] then
Result := sNegative;
Inc(i);
end;
Dec(Count, BytesToRead);
end;
if (Result = sZero) and (LeftSize <> RightSize) then
begin
if LeftSize > RightSize then
Result := sPositive
else
Result := sNegative;
end;
LeftStream.Free();
RightStream.Free();
end;
procedure TByteBlock.CopyFrom(Block: TByteBlock);
var
Stream: TInputStream;
begin
Stream := Block.CreateInputStream();
try
Read(Stream);
finally
Stream.Free();
end;
end;
function TByteBlock.Equal(Block: TByteBlock): TBoolean;
begin
Result := Compare(Self, Block) = sZero;
end;
procedure TByteBlock.Read(Stream: TInputStream);
begin
raise ENotImplemented.Create(ClassType, 'Read');
end;
procedure TByteBlock.ReadFromObject(SerializableObject: TSerializableObject);
var
Stream: TOutputStream;
begin
Stream := CreateAppendStream();
SerializableObject.Write(Stream);
Stream.Free();
end;
function TByteBlock.ToByteArray(): TArray<TByte>;
var
Stream: TInputStream;
ByteCount: TPositiveInteger32;
begin
ByteCount := GetByteCount();
SetLength(Result, ByteCount);
if ByteCount > 0 then
begin
Stream := CreateInputStream();
Stream.Read(Result[0], ByteCount);
Stream.Free();
end;
end;
procedure TByteBlock.Write(Stream: TOutputStream);
var
Input: TInputStream;
begin
Input := CreateInputStream();
CopyStream(Input, Stream, GetByteCount());
Input.Free();
end;
procedure TByteBlock.WriteToObject(SerializableObject: TSerializableObject);
var
Stream: TInputStream;
begin
Stream := CreateInputStream();
SerializableObject.Read(Stream);
Stream.Free();
end;
end.
|