Added a check so that the PDF417 reader can get through the partial blackbox test.
[zxing.git] / csharp / SupportClass.cs
1 /*\r
2 * Licensed under the Apache License, Version 2.0 (the "License");\r
3 * you may not use this file except in compliance with the License.\r
4 * You may obtain a copy of the License at\r
5 *\r
6 *      http://www.apache.org/licenses/LICENSE-2.0\r
7 *\r
8 * Unless required by applicable law or agreed to in writing, software\r
9 * distributed under the License is distributed on an "AS IS" BASIS,\r
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
11 * See the License for the specific language governing permissions and\r
12 * limitations under the License.\r
13 */\r
14 \r
15 using System;\r
16 \r
17         /// <summary>\r
18         /// This interface should be implemented by any class whose instances are intended \r
19         /// to be executed by a thread.\r
20         /// </summary>\r
21         public interface IThreadRunnable\r
22         {\r
23                 /// <summary>\r
24                 /// This method has to be implemented in order that starting of the thread causes the object's \r
25                 /// run method to be called in that separately executing thread.\r
26                 /// </summary>\r
27                 void Run();\r
28         }\r
29 \r
30 /// <summary>\r
31 /// Contains conversion support elements such as classes, interfaces and static methods.\r
32 /// </summary>\r
33 public class SupportClass\r
34 {\r
35 \r
36 \r
37 \r
38         /// <summary>\r
39         /// Performs an unsigned bitwise right shift with the specified number\r
40         /// </summary>\r
41         /// <param name="number">Number to operate on</param>\r
42         /// <param name="bits">Ammount of bits to shift</param>\r
43         /// <returns>The resulting number from the shift operation</returns>\r
44         public static int URShift(int number, int bits)\r
45         {\r
46                 if ( number >= 0)\r
47                         return number >> bits;\r
48                 else\r
49                         return (number >> bits) + (2 << ~bits);\r
50         }\r
51 \r
52         /// <summary>\r
53         /// Performs an unsigned bitwise right shift with the specified number\r
54         /// </summary>\r
55         /// <param name="number">Number to operate on</param>\r
56         /// <param name="bits">Ammount of bits to shift</param>\r
57         /// <returns>The resulting number from the shift operation</returns>\r
58         public static int URShift(int number, long bits)\r
59         {\r
60                 return URShift(number, (int)bits);\r
61         }\r
62 \r
63         /// <summary>\r
64         /// Performs an unsigned bitwise right shift with the specified number\r
65         /// </summary>\r
66         /// <param name="number">Number to operate on</param>\r
67         /// <param name="bits">Ammount of bits to shift</param>\r
68         /// <returns>The resulting number from the shift operation</returns>\r
69         public static long URShift(long number, int bits)\r
70         {\r
71                 if ( number >= 0)\r
72                         return number >> bits;\r
73                 else\r
74                         return (number >> bits) + (2L << ~bits);\r
75         }\r
76 \r
77         /// <summary>\r
78         /// Performs an unsigned bitwise right shift with the specified number\r
79         /// </summary>\r
80         /// <param name="number">Number to operate on</param>\r
81         /// <param name="bits">Ammount of bits to shift</param>\r
82         /// <returns>The resulting number from the shift operation</returns>\r
83         public static long URShift(long number, long bits)\r
84         {\r
85                 return URShift(number, (int)bits);\r
86         }\r
87 \r
88         /*******************************/\r
89         /// <summary>\r
90         /// Copies an array of chars obtained from a String into a specified array of chars\r
91         /// </summary>\r
92         /// <param name="sourceString">The String to get the chars from</param>\r
93         /// <param name="sourceStart">Position of the String to start getting the chars</param>\r
94         /// <param name="sourceEnd">Position of the String to end getting the chars</param>\r
95         /// <param name="destinationArray">Array to return the chars</param>\r
96         /// <param name="destinationStart">Position of the destination array of chars to start storing the chars</param>\r
97         /// <returns>An array of chars</returns>\r
98         public static void GetCharsFromString(System.String sourceString, int sourceStart, int sourceEnd, char[] destinationArray, int destinationStart)\r
99         {       \r
100                 int sourceCounter;\r
101                 int destinationCounter;\r
102                 sourceCounter = sourceStart;\r
103                 destinationCounter = destinationStart;\r
104                 while (sourceCounter < sourceEnd)\r
105                 {\r
106                         destinationArray[destinationCounter] = (char) sourceString[sourceCounter];\r
107                         sourceCounter++;\r
108                         destinationCounter++;\r
109                 }\r
110         }\r
111 \r
112         /*******************************/\r
113         /// <summary>\r
114         /// Converts an array of sbytes to an array of bytes\r
115         /// </summary>\r
116         /// <param name="sbyteArray">The array of sbytes to be converted</param>\r
117         /// <returns>The new array of bytes</returns>\r
118         public static byte[] ToByteArray(sbyte[] sbyteArray)\r
119         {\r
120                 byte[] byteArray = null;\r
121 \r
122                 if (sbyteArray != null)\r
123                 {\r
124                         byteArray = new byte[sbyteArray.Length];\r
125                         for(int index=0; index < sbyteArray.Length; index++)\r
126                                 byteArray[index] = (byte) sbyteArray[index];\r
127                 }\r
128                 return byteArray;\r
129         }\r
130 \r
131         /// <summary>\r
132         /// Converts a string to an array of bytes\r
133         /// </summary>\r
134         /// <param name="sourceString">The string to be converted</param>\r
135         /// <returns>The new array of bytes</returns>\r
136         public static byte[] ToByteArray(System.String sourceString)\r
137         {\r
138                 return System.Text.UTF8Encoding.UTF8.GetBytes(sourceString);\r
139         }\r
140 \r
141         /// <summary>\r
142         /// Converts a array of object-type instances to a byte-type array.\r
143         /// </summary>\r
144         /// <param name="tempObjectArray">Array to convert.</param>\r
145         /// <returns>An array of byte type elements.</returns>\r
146         public static byte[] ToByteArray(System.Object[] tempObjectArray)\r
147         {\r
148                 byte[] byteArray = null;\r
149                 if (tempObjectArray != null)\r
150                 {\r
151                         byteArray = new byte[tempObjectArray.Length];\r
152                         for (int index = 0; index < tempObjectArray.Length; index++)\r
153                                 byteArray[index] = (byte)tempObjectArray[index];\r
154                 }\r
155                 return byteArray;\r
156         }\r
157 \r
158         /*******************************/\r
159         /// <summary>\r
160         /// Sets the capacity for the specified ArrayList\r
161         /// </summary>\r
162         /// <param name="vector">The ArrayList which capacity will be set</param>\r
163         /// <param name="newCapacity">The new capacity value</param>\r
164         public static void SetCapacity(System.Collections.ArrayList vector, int newCapacity)\r
165         {\r
166                 if (newCapacity > vector.Count)\r
167                         vector.AddRange(new Array[newCapacity-vector.Count]);\r
168                 else if (newCapacity < vector.Count)\r
169                         vector.RemoveRange(newCapacity, vector.Count - newCapacity);\r
170                 vector.Capacity = newCapacity;\r
171         }\r
172 \r
173 \r
174 \r
175         /*******************************/\r
176         /// <summary>\r
177         /// This method returns the literal value received\r
178         /// </summary>\r
179         /// <param name="literal">The literal to return</param>\r
180         /// <returns>The received value</returns>\r
181         public static long Identity(long literal)\r
182         {\r
183                 return literal;\r
184         }\r
185 \r
186         /// <summary>\r
187         /// This method returns the literal value received\r
188         /// </summary>\r
189         /// <param name="literal">The literal to return</param>\r
190         /// <returns>The received value</returns>\r
191         public static ulong Identity(ulong literal)\r
192         {\r
193                 return literal;\r
194         }\r
195 \r
196         /// <summary>\r
197         /// This method returns the literal value received\r
198         /// </summary>\r
199         /// <param name="literal">The literal to return</param>\r
200         /// <returns>The received value</returns>\r
201         public static float Identity(float literal)\r
202         {\r
203                 return literal;\r
204         }\r
205 \r
206         /// <summary>\r
207         /// This method returns the literal value received\r
208         /// </summary>\r
209         /// <param name="literal">The literal to return</param>\r
210         /// <returns>The received value</returns>\r
211         public static double Identity(double literal)\r
212         {\r
213                 return literal;\r
214         }\r
215 \r
216         /*******************************/\r
217         /// <summary>\r
218         /// Support class used to handle threads\r
219         /// </summary>\r
220         public class ThreadClass : IThreadRunnable\r
221         {\r
222                 /// <summary>\r
223                 /// The instance of System.Threading.Thread\r
224                 /// </summary>\r
225                 private System.Threading.Thread threadField;\r
226               \r
227                 /// <summary>\r
228                 /// Initializes a new instance of the ThreadClass class\r
229                 /// </summary>\r
230                 public ThreadClass()\r
231                 {\r
232                         threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));\r
233                 }\r
234          \r
235                 /// <summary>\r
236                 /// Initializes a new instance of the Thread class.\r
237                 /// </summary>\r
238                 /// <param name="Name">The name of the thread</param>\r
239                 public ThreadClass(System.String Name)\r
240                 {\r
241                         threadField = new System.Threading.Thread(new System.Threading.ThreadStart(Run));\r
242                         this.Name = Name;\r
243                 }\r
244               \r
245                 /// <summary>\r
246                 /// Initializes a new instance of the Thread class.\r
247                 /// </summary>\r
248                 /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param>\r
249                 public ThreadClass(System.Threading.ThreadStart Start)\r
250                 {\r
251                         threadField = new System.Threading.Thread(Start);\r
252                 }\r
253          \r
254                 /// <summary>\r
255                 /// Initializes a new instance of the Thread class.\r
256                 /// </summary>\r
257                 /// <param name="Start">A ThreadStart delegate that references the methods to be invoked when this thread begins executing</param>\r
258                 /// <param name="Name">The name of the thread</param>\r
259                 public ThreadClass(System.Threading.ThreadStart Start, System.String Name)\r
260                 {\r
261                         threadField = new System.Threading.Thread(Start);\r
262                         this.Name = Name;\r
263                 }\r
264               \r
265                 /// <summary>\r
266                 /// This method has no functionality unless the method is overridden\r
267                 /// </summary>\r
268                 public virtual void Run()\r
269                 {\r
270                 }\r
271               \r
272                 /// <summary>\r
273                 /// Causes the operating system to change the state of the current thread instance to ThreadState.Running\r
274                 /// </summary>\r
275                 public virtual void Start()\r
276                 {\r
277                         threadField.Start();\r
278                 }\r
279               \r
280                 ///// <summary>\r
281                 ///// Interrupts a thread that is in the WaitSleepJoin thread state\r
282                 ///// </summary>\r
283                 //public virtual void Interrupt()\r
284                 //{\r
285                 //  threadField.Interrupt();\r
286                 //}\r
287               \r
288                 /// <summary>\r
289                 /// Gets the current thread instance\r
290                 /// </summary>\r
291                 public System.Threading.Thread Instance\r
292                 {\r
293                         get\r
294                         {\r
295                                 return threadField;\r
296                         }\r
297                         set\r
298                         {\r
299                                 threadField = value;\r
300                         }\r
301                 }\r
302               \r
303                 /// <summary>\r
304                 /// Gets or sets the name of the thread\r
305                 /// </summary>\r
306                 public System.String Name\r
307                 {\r
308                         get\r
309                         {\r
310                                 return threadField.Name;\r
311                         }\r
312                         set\r
313                         {\r
314                                 if (threadField.Name == null)\r
315                                         threadField.Name = value; \r
316                         }\r
317                 }\r
318               \r
319                 /// <summary>\r
320                 /// Gets or sets a value indicating the scheduling priority of a thread\r
321                 /// </summary>\r
322                 public System.Threading.ThreadPriority Priority\r
323                 {\r
324                         get\r
325                         {\r
326                                 return threadField.Priority;\r
327                         }\r
328                         set\r
329                         {\r
330                                 threadField.Priority = value;\r
331                         }\r
332                 }\r
333               \r
334                 ///// <summary>\r
335                 ///// Gets a value indicating the execution status of the current thread\r
336                 ///// </summary>\r
337                 //public bool IsAlive\r
338                 //{\r
339                 //  get\r
340                 //  {\r
341                 //    return threadField.IsAlive;\r
342                 //  }\r
343                 //}\r
344               \r
345                 /// <summary>\r
346                 /// Gets or sets a value indicating whether or not a thread is a background thread.\r
347                 /// </summary>\r
348                 public bool IsBackground\r
349                 {\r
350                         get\r
351                         {\r
352                                 return threadField.IsBackground;\r
353                         } \r
354                         set\r
355                         {\r
356                                 threadField.IsBackground = value;\r
357                         }\r
358                 }\r
359               \r
360                 /// <summary>\r
361                 /// Blocks the calling thread until a thread terminates\r
362                 /// </summary>\r
363                 public void Join()\r
364                 {\r
365                         threadField.Join();\r
366                 }\r
367               \r
368                 /// <summary>\r
369                 /// Blocks the calling thread until a thread terminates or the specified time elapses\r
370                 /// </summary>\r
371                 /// <param name="MiliSeconds">Time of wait in milliseconds</param>\r
372                 public void Join(int MiliSeconds)\r
373                 {\r
374                         lock(this)\r
375                         {\r
376                                 threadField.Join(MiliSeconds);\r
377                         }\r
378                 }\r
379               \r
380                 ///// <summary>\r
381                 ///// Blocks the calling thread until a thread terminates or the specified time elapses\r
382                 ///// </summary>\r
383                 ///// <param name="MiliSeconds">Time of wait in milliseconds</param>\r
384                 ///// <param name="NanoSeconds">Time of wait in nanoseconds</param>\r
385                 //public void Join(long MiliSeconds, int NanoSeconds)\r
386                 //{\r
387                 //  lock(this)\r
388                 //  {\r
389                 //    threadField.Join(new System.TimeSpan(MiliSeconds * 10000 + NanoSeconds * 100));\r
390                 //  }\r
391                 //}\r
392               \r
393                 ///// <summary>\r
394                 ///// Resumes a thread that has been suspended\r
395                 ///// </summary>\r
396                 //public void Resume()\r
397                 //{\r
398                 //  threadField.Resume();\r
399                 //}\r
400               \r
401                 /// <summary>\r
402                 /// Raises a ThreadAbortException in the thread on which it is invoked, \r
403                 /// to begin the process of terminating the thread. Calling this method \r
404                 /// usually terminates the thread\r
405                 /// </summary>\r
406                 public void Abort()\r
407                 {\r
408                         threadField.Abort();\r
409                 }\r
410               \r
411                 /// <summary>\r
412                 /// Raises a ThreadAbortException in the thread on which it is invoked, \r
413                 /// to begin the process of terminating the thread while also providing\r
414                 /// exception information about the thread termination. \r
415                 /// Calling this method usually terminates the thread.\r
416                 /// </summary>\r
417                 /// <param name="stateInfo">An object that contains application-specific information, such as state, which can be used by the thread being aborted</param>\r
418                 public void Abort(System.Object stateInfo)\r
419                 {\r
420                         lock(this)\r
421                         {\r
422                                 threadField.Abort(stateInfo);\r
423                         }\r
424                 }\r
425               \r
426                 ///// <summary>\r
427                 ///// Suspends the thread, if the thread is already suspended it has no effect\r
428                 ///// </summary>\r
429                 //public void Suspend()\r
430                 //{\r
431                 //  threadField.Suspend();\r
432                 //}\r
433               \r
434                 /// <summary>\r
435                 /// Obtain a String that represents the current Object\r
436                 /// </summary>\r
437                 /// <returns>A String that represents the current Object</returns>\r
438                 public override System.String ToString()\r
439                 {\r
440                         return "Thread[" + Name + "," + Priority.ToString() + "," + "" + "]";\r
441                 }\r
442              \r
443                 /// <summary>\r
444                 /// Gets the currently running thread\r
445                 /// </summary>\r
446                 /// <returns>The currently running thread</returns>\r
447                 public static ThreadClass Current()\r
448                 {\r
449                         ThreadClass CurrentThread = new ThreadClass();\r
450                         CurrentThread.Instance = System.Threading.Thread.CurrentThread;\r
451                         return CurrentThread;\r
452                 }\r
453         }\r
454 \r
455 \r
456 }\r