Skip to content

Commit 42a77df

Browse files
committed
Source code uploaded
1 parent 9113689 commit 42a77df

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+10817
-0
lines changed

Source/Source/ByteBuffer.cs

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/***************************************************************************
2+
3+
Rtf Dom Parser
4+
5+
Copyright (c) 2010 sinosoft , written by yuans.
6+
http://www.sinoreport.net
7+
8+
This program is free software; you can redistribute it and/or
9+
modify it under the terms of the GNU General Public License
10+
as published by the Free Software Foundation; either version 2
11+
of the License, or (at your option) any later version.
12+
13+
This program is distributed in the hope that it will be useful,
14+
but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
GNU General Public License for more details.
17+
18+
You should have received a copy of the GNU General Public License
19+
along with this program; if not, write to the Free Software
20+
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21+
22+
****************************************************************************/
23+
24+
using System;
25+
using System.Windows.Forms;
26+
27+
namespace XDesigner.RTF
28+
{
29+
/// <summary>
30+
/// Binary data buffer
31+
/// </summary>
32+
public class ByteBuffer
33+
{
34+
/// <summary>
35+
/// Initialize instance
36+
/// </summary>
37+
public ByteBuffer()
38+
{
39+
}
40+
41+
/// <summary>
42+
/// Current contains validate bytes
43+
/// </summary>
44+
protected int intCount = 0 ;
45+
/// <summary>
46+
/// byte array
47+
/// </summary>
48+
protected byte[] bsBuffer = new byte[ 16 ];
49+
50+
/// <summary>
51+
/// Clear object's data
52+
/// </summary>
53+
public virtual void Clear()
54+
{
55+
bsBuffer = new byte[ 16 ];
56+
intCount = 0 ;
57+
}
58+
59+
/// <summary>
60+
/// Reset current position without clear buffer
61+
/// </summary>
62+
public void Reset()
63+
{
64+
intCount = 0 ;
65+
}
66+
67+
/// <summary>
68+
/// Set of get byte at special index which starts with 0
69+
/// </summary>
70+
public byte this[ int index ]
71+
{
72+
get
73+
{
74+
if( index >= 0 && index < intCount )
75+
return bsBuffer[ index ] ;
76+
else
77+
throw new IndexOutOfRangeException("index");
78+
}
79+
set
80+
{
81+
if( index >= 0 && index < intCount )
82+
bsBuffer[ index ] = value ;
83+
else
84+
throw new IndexOutOfRangeException("index");
85+
}
86+
}
87+
/// <summary>
88+
/// Validate bytes count
89+
/// </summary>
90+
public virtual int Count
91+
{
92+
get
93+
{
94+
return intCount;
95+
}
96+
}
97+
98+
/// <summary>
99+
/// Add a byte
100+
/// </summary>
101+
/// <param name="b">byte data</param>
102+
public void Add( byte b )
103+
{
104+
FixBuffer( intCount + 1 );
105+
bsBuffer[intCount] = b ;
106+
intCount ++;
107+
}
108+
109+
/// <summary>
110+
/// Add bytes
111+
/// </summary>
112+
/// <param name="bs">bytes</param>
113+
public void Add( byte[] bs )
114+
{
115+
if( bs != null)
116+
Add( bs , 0 , bs.Length );
117+
}
118+
/// <summary>
119+
/// Add bytes
120+
/// </summary>
121+
/// <param name="bs">Bytes</param>
122+
/// <param name="StartIndex">Start index</param>
123+
/// <param name="Length">Length</param>
124+
public void Add(byte[] bs , int StartIndex , int Length )
125+
{
126+
if( bs != null && StartIndex >= 0 && (StartIndex + Length ) <= bs.Length && Length > 0 )
127+
{
128+
FixBuffer(intCount + Length );
129+
Array.Copy(bs , StartIndex , bsBuffer , intCount , Length );
130+
intCount += Length ;
131+
}
132+
}
133+
134+
/// <summary>
135+
/// Get validate bytes array
136+
/// </summary>
137+
/// <returns>bytes array</returns>
138+
public byte[] ToArray()
139+
{
140+
if( intCount > 0 )
141+
{
142+
byte[] bs = new byte[ intCount ];
143+
Array.Copy( bsBuffer , 0 , bs , 0 , intCount );
144+
return bs ;
145+
}
146+
else
147+
return null;
148+
}
149+
150+
/// <summary>
151+
/// Convert bytes data to a string
152+
/// </summary>
153+
/// <param name="encoding">string encoding</param>
154+
/// <returns>string data</returns>
155+
public string GetString( System.Text.Encoding encoding )
156+
{
157+
if( encoding == null )
158+
throw new System.ArgumentNullException("encoding");
159+
if( intCount > 0 )
160+
return encoding.GetString( bsBuffer , 0 , intCount );
161+
else
162+
return "";
163+
}
164+
/// <summary>
165+
/// Fix inner buffer so it can fit to new size of buffer
166+
/// </summary>
167+
/// <param name="NewSize">new size</param>
168+
protected void FixBuffer( int NewSize )
169+
{
170+
if( NewSize <= bsBuffer.Length )
171+
return ;
172+
if( NewSize < (int)( bsBuffer.Length * 1.5 ))
173+
NewSize = (int)( bsBuffer.Length * 1.5 );
174+
175+
byte[] bs = new byte[ NewSize ];
176+
Buffer.BlockCopy( bsBuffer , 0 , bs , 0 , bsBuffer.Length );
177+
//Array.Copy( bsBuffer , 0 , bs , 0 , bsBuffer.Length );
178+
bsBuffer = bs ;
179+
}
180+
}
181+
}

0 commit comments

Comments
 (0)