Committed C# port from Mohamad
[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 namespace com.google.zxing.common\r
17 {\r
18     using System;\r
19     using System.Text;\r
20 \r
21     /// <summary> A class which wraps a 2D array of bytes. The default usage is signed. If you want to use it as a\r
22     /// unsigned container, it's up to you to do byteValue & 0xff at each location.\r
23     /// *\r
24     /// JAVAPORT: I'm not happy about the argument ordering throughout the file, as I always like to have\r
25     /// the horizontal component first, but this is for compatibility with the C++ code. The original\r
26     /// code was a 2D array of ints, but since it only ever gets assigned -1, 0, and 1, I'm going to use\r
27     /// less memory and go with bytes.\r
28     /// *\r
29     /// </summary>\r
30     /// <author>  dswitkin@google.com (Daniel Switkin)\r
31     /// \r
32     /// </author>\r
33     public sealed class ByteArray\r
34     {\r
35         private static int INITIAL_SIZE = 32;\r
36         private sbyte[] bytes;\r
37         private int Size;\r
38 \r
39         public ByteArray()\r
40         {\r
41             bytes = null;\r
42             this.Size = 0;\r
43         }\r
44 \r
45         public ByteArray(int size)\r
46         {\r
47             bytes = new sbyte[size];\r
48             this.Size = size;\r
49         }\r
50 \r
51         public ByteArray(sbyte[] byteArray)\r
52         {\r
53             bytes = byteArray;\r
54             this.Size = bytes.Length;\r
55         }\r
56 \r
57         /**\r
58          * Access an unsigned byte at location index.\r
59          * @param index The index in the array to access.\r
60          * @return The unsigned value of the byte as an int.\r
61          */\r
62         public int at(int index)\r
63         {\r
64             return bytes[index] & 0xff;\r
65         }\r
66 \r
67         public void set(int index, int value)\r
68         {\r
69             bytes[index] = (sbyte)value;\r
70         }\r
71 \r
72         public int size()\r
73         {\r
74             return Size;\r
75         }\r
76 \r
77         public bool empty()\r
78         {\r
79             return Size == 0;\r
80         }\r
81 \r
82         public void appendByte(int value)\r
83         {\r
84             if (Size == 0 || Size >= bytes.Length)\r
85             {\r
86                 int newSize = Math.Max(INITIAL_SIZE, Size << 1);\r
87                 reserve(newSize);\r
88             }\r
89             bytes[Size] = (sbyte)value;\r
90             Size++;\r
91         }\r
92 \r
93         public void reserve(int capacity)\r
94         {\r
95             if (bytes == null || bytes.Length < capacity)\r
96             {\r
97                 sbyte[] newArray = new sbyte[capacity];\r
98                 if (bytes != null)\r
99                 {\r
100                     System.Array.Copy(bytes, 0, newArray, 0, bytes.Length);\r
101                 }\r
102                 bytes = newArray;\r
103             }\r
104         }\r
105 \r
106         // Copy count bytes from array source starting at offset.\r
107         public void set(sbyte[] source, int offset, int count)\r
108         {\r
109             bytes = new sbyte[count];\r
110             Size = count;\r
111             for (int x = 0; x < count; x++)\r
112             {\r
113                 bytes[x] = source[offset + x];\r
114             }\r
115         }\r
116     }\r
117 }