New C# port from Suraj Supekar
[zxing.git] / csharp / common / ByteArray.cs
1 /*\r
2 * Copyright 2008 ZXing authors\r
3 *\r
4 * Licensed under the Apache License, Version 2.0 (the "License");\r
5 * you may not use this file except in compliance with the License.\r
6 * You may obtain a copy of the License at\r
7 *\r
8 *      http://www.apache.org/licenses/LICENSE-2.0\r
9 *\r
10 * Unless required by applicable law or agreed to in writing, software\r
11 * distributed under the License is distributed on an "AS IS" BASIS,\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13 * See the License for the specific language governing permissions and\r
14 * limitations under the License.\r
15 */\r
16 using System;\r
17 namespace com.google.zxing.common\r
18 {\r
19         \r
20         /// <summary> This class implements an array of unsigned bytes.\r
21         /// \r
22         /// </summary>\r
23         /// <author>  dswitkin@google.com (Daniel Switkin)\r
24         /// </author>\r
25         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
26         /// </author>\r
27         public sealed class ByteArray\r
28         {\r
29                 public bool Empty\r
30                 {\r
31                         get\r
32                         {\r
33                                 return size_Renamed_Field == 0;\r
34                         }\r
35                         \r
36                 }\r
37                 \r
38                 private const int INITIAL_SIZE = 32;\r
39                 \r
40                 private sbyte[] bytes;\r
41                 private int size_Renamed_Field;\r
42                 \r
43                 public ByteArray()\r
44                 {\r
45                         bytes = null;\r
46                         size_Renamed_Field = 0;\r
47                 }\r
48                 \r
49                 public ByteArray(int size)\r
50                 {\r
51                         bytes = new sbyte[size];\r
52                         this.size_Renamed_Field = size;\r
53                 }\r
54                 \r
55                 public ByteArray(sbyte[] byteArray)\r
56                 {\r
57                         bytes = byteArray;\r
58                         size_Renamed_Field = bytes.Length;\r
59                 }\r
60                 \r
61                 /// <summary> Access an unsigned byte at location index.</summary>\r
62                 /// <param name="index">The index in the array to access.\r
63                 /// </param>\r
64                 /// <returns> The unsigned value of the byte as an int.\r
65                 /// </returns>\r
66                 public int at(int index)\r
67                 {\r
68                         return bytes[index] & 0xff;\r
69                 }\r
70                 \r
71                 public void  set_Renamed(int index, int value_Renamed)\r
72                 {\r
73                         bytes[index] = (sbyte) value_Renamed;\r
74                 }\r
75                 \r
76                 public int size()\r
77                 {\r
78                         return size_Renamed_Field;\r
79                 }\r
80                 \r
81                 public void  appendByte(int value_Renamed)\r
82                 {\r
83                         if (size_Renamed_Field == 0 || size_Renamed_Field >= bytes.Length)\r
84                         {\r
85                                 int newSize = System.Math.Max(INITIAL_SIZE, size_Renamed_Field << 1);\r
86                                 reserve(newSize);\r
87                         }\r
88                         bytes[size_Renamed_Field] = (sbyte) value_Renamed;\r
89                         size_Renamed_Field++;\r
90                 }\r
91                 \r
92                 public void  reserve(int capacity)\r
93                 {\r
94                         if (bytes == null || bytes.Length < capacity)\r
95                         {\r
96                                 sbyte[] newArray = new sbyte[capacity];\r
97                                 if (bytes != null)\r
98                                 {\r
99                                         Array.Copy(bytes, 0, newArray, 0, bytes.Length);\r
100                                 }\r
101                                 bytes = newArray;\r
102                         }\r
103                 }\r
104                 \r
105                 // Copy count bytes from array source starting at offset.\r
106                 public void  set_Renamed(sbyte[] source, int offset, int count)\r
107                 {\r
108                         bytes = new sbyte[count];\r
109                         size_Renamed_Field = count;\r
110                         for (int x = 0; x < count; x++)\r
111                         {\r
112                                 bytes[x] = source[offset + x];\r
113                         }\r
114                 }\r
115         }\r
116 }