You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NB.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Copyright (C) 2008, 2015 Shawn O. Pearce <spearce@spearce.org>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.util;
  44. /**
  45. * Conversion utilities for network byte order handling.
  46. */
  47. public final class NB {
  48. /**
  49. * Compare a 32 bit unsigned integer stored in a 32 bit signed integer.
  50. * <p>
  51. * This function performs an unsigned compare operation, even though Java
  52. * does not natively support unsigned integer values. Negative numbers are
  53. * treated as larger than positive ones.
  54. *
  55. * @param a
  56. * the first value to compare.
  57. * @param b
  58. * the second value to compare.
  59. * @return &lt; 0 if a &lt; b; 0 if a == b; &gt; 0 if a &gt; b.
  60. */
  61. public static int compareUInt32(final int a, final int b) {
  62. final int cmp = (a >>> 1) - (b >>> 1);
  63. if (cmp != 0)
  64. return cmp;
  65. return (a & 1) - (b & 1);
  66. }
  67. /**
  68. * Compare a 64 bit unsigned integer stored in a 64 bit signed integer.
  69. * <p>
  70. * This function performs an unsigned compare operation, even though Java
  71. * does not natively support unsigned integer values. Negative numbers are
  72. * treated as larger than positive ones.
  73. *
  74. * @param a
  75. * the first value to compare.
  76. * @param b
  77. * the second value to compare.
  78. * @return &lt; 0 if a &lt; b; 0 if a == b; &gt; 0 if a &gt; b.
  79. * @since 4.3
  80. */
  81. public static int compareUInt64(final long a, final long b) {
  82. long cmp = (a >>> 1) - (b >>> 1);
  83. if (cmp > 0) {
  84. return 1;
  85. } else if (cmp < 0) {
  86. return -1;
  87. }
  88. cmp = ((a & 1) - (b & 1));
  89. if (cmp > 0) {
  90. return 1;
  91. } else if (cmp < 0) {
  92. return -1;
  93. } else {
  94. return 0;
  95. }
  96. }
  97. /**
  98. * Convert sequence of 2 bytes (network byte order) into unsigned value.
  99. *
  100. * @param intbuf
  101. * buffer to acquire the 2 bytes of data from.
  102. * @param offset
  103. * position within the buffer to begin reading from. This
  104. * position and the next byte after it (for a total of 2 bytes)
  105. * will be read.
  106. * @return unsigned integer value that matches the 16 bits read.
  107. */
  108. public static int decodeUInt16(final byte[] intbuf, final int offset) {
  109. int r = (intbuf[offset] & 0xff) << 8;
  110. return r | (intbuf[offset + 1] & 0xff);
  111. }
  112. /**
  113. * Convert sequence of 3 bytes (network byte order) into unsigned value.
  114. *
  115. * @param intbuf
  116. * buffer to acquire the 3 bytes of data from.
  117. * @param offset
  118. * position within the buffer to begin reading from. This
  119. * position and the next 2 bytes after it (for a total of 3
  120. * bytes) will be read.
  121. * @return signed integer value that matches the 24 bits read.
  122. * @since 4.9
  123. */
  124. public static int decodeUInt24(byte[] intbuf, int offset) {
  125. int r = (intbuf[offset] & 0xff) << 8;
  126. r |= intbuf[offset + 1] & 0xff;
  127. return (r << 8) | (intbuf[offset + 2] & 0xff);
  128. }
  129. /**
  130. * Convert sequence of 4 bytes (network byte order) into signed value.
  131. *
  132. * @param intbuf
  133. * buffer to acquire the 4 bytes of data from.
  134. * @param offset
  135. * position within the buffer to begin reading from. This
  136. * position and the next 3 bytes after it (for a total of 4
  137. * bytes) will be read.
  138. * @return signed integer value that matches the 32 bits read.
  139. */
  140. public static int decodeInt32(final byte[] intbuf, final int offset) {
  141. int r = intbuf[offset] << 8;
  142. r |= intbuf[offset + 1] & 0xff;
  143. r <<= 8;
  144. r |= intbuf[offset + 2] & 0xff;
  145. return (r << 8) | (intbuf[offset + 3] & 0xff);
  146. }
  147. /**
  148. * Convert sequence of 8 bytes (network byte order) into signed value.
  149. *
  150. * @param intbuf
  151. * buffer to acquire the 8 bytes of data from.
  152. * @param offset
  153. * position within the buffer to begin reading from. This
  154. * position and the next 7 bytes after it (for a total of 8
  155. * bytes) will be read.
  156. * @return signed integer value that matches the 64 bits read.
  157. * @since 3.0
  158. */
  159. public static long decodeInt64(final byte[] intbuf, final int offset) {
  160. long r = intbuf[offset] << 8;
  161. r |= intbuf[offset + 1] & 0xff;
  162. r <<= 8;
  163. r |= intbuf[offset + 2] & 0xff;
  164. r <<= 8;
  165. r |= intbuf[offset + 3] & 0xff;
  166. r <<= 8;
  167. r |= intbuf[offset + 4] & 0xff;
  168. r <<= 8;
  169. r |= intbuf[offset + 5] & 0xff;
  170. r <<= 8;
  171. r |= intbuf[offset + 6] & 0xff;
  172. return (r << 8) | (intbuf[offset + 7] & 0xff);
  173. }
  174. /**
  175. * Convert sequence of 4 bytes (network byte order) into unsigned value.
  176. *
  177. * @param intbuf
  178. * buffer to acquire the 4 bytes of data from.
  179. * @param offset
  180. * position within the buffer to begin reading from. This
  181. * position and the next 3 bytes after it (for a total of 4
  182. * bytes) will be read.
  183. * @return unsigned integer value that matches the 32 bits read.
  184. */
  185. public static long decodeUInt32(final byte[] intbuf, final int offset) {
  186. int low = (intbuf[offset + 1] & 0xff) << 8;
  187. low |= (intbuf[offset + 2] & 0xff);
  188. low <<= 8;
  189. low |= (intbuf[offset + 3] & 0xff);
  190. return ((long) (intbuf[offset] & 0xff)) << 24 | low;
  191. }
  192. /**
  193. * Convert sequence of 8 bytes (network byte order) into unsigned value.
  194. *
  195. * @param intbuf
  196. * buffer to acquire the 8 bytes of data from.
  197. * @param offset
  198. * position within the buffer to begin reading from. This
  199. * position and the next 7 bytes after it (for a total of 8
  200. * bytes) will be read.
  201. * @return unsigned integer value that matches the 64 bits read.
  202. */
  203. public static long decodeUInt64(final byte[] intbuf, final int offset) {
  204. return (decodeUInt32(intbuf, offset) << 32)
  205. | decodeUInt32(intbuf, offset + 4);
  206. }
  207. /**
  208. * Write a 16 bit integer as a sequence of 2 bytes (network byte order).
  209. *
  210. * @param intbuf
  211. * buffer to write the 2 bytes of data into.
  212. * @param offset
  213. * position within the buffer to begin writing to. This position
  214. * and the next byte after it (for a total of 2 bytes) will be
  215. * replaced.
  216. * @param v
  217. * the value to write.
  218. */
  219. public static void encodeInt16(final byte[] intbuf, final int offset, int v) {
  220. intbuf[offset + 1] = (byte) v;
  221. v >>>= 8;
  222. intbuf[offset] = (byte) v;
  223. }
  224. /**
  225. * Write a 24 bit integer as a sequence of 3 bytes (network byte order).
  226. *
  227. * @param intbuf
  228. * buffer to write the 3 bytes of data into.
  229. * @param offset
  230. * position within the buffer to begin writing to. This position
  231. * and the next 2 bytes after it (for a total of 3 bytes) will be
  232. * replaced.
  233. * @param v
  234. * the value to write.
  235. * @since 4.9
  236. */
  237. public static void encodeInt24(byte[] intbuf, int offset, int v) {
  238. intbuf[offset + 2] = (byte) v;
  239. v >>>= 8;
  240. intbuf[offset + 1] = (byte) v;
  241. v >>>= 8;
  242. intbuf[offset] = (byte) v;
  243. }
  244. /**
  245. * Write a 32 bit integer as a sequence of 4 bytes (network byte order).
  246. *
  247. * @param intbuf
  248. * buffer to write the 4 bytes of data into.
  249. * @param offset
  250. * position within the buffer to begin writing to. This position
  251. * and the next 3 bytes after it (for a total of 4 bytes) will be
  252. * replaced.
  253. * @param v
  254. * the value to write.
  255. */
  256. public static void encodeInt32(final byte[] intbuf, final int offset, int v) {
  257. intbuf[offset + 3] = (byte) v;
  258. v >>>= 8;
  259. intbuf[offset + 2] = (byte) v;
  260. v >>>= 8;
  261. intbuf[offset + 1] = (byte) v;
  262. v >>>= 8;
  263. intbuf[offset] = (byte) v;
  264. }
  265. /**
  266. * Write a 64 bit integer as a sequence of 8 bytes (network byte order).
  267. *
  268. * @param intbuf
  269. * buffer to write the 8 bytes of data into.
  270. * @param offset
  271. * position within the buffer to begin writing to. This position
  272. * and the next 7 bytes after it (for a total of 8 bytes) will be
  273. * replaced.
  274. * @param v
  275. * the value to write.
  276. */
  277. public static void encodeInt64(final byte[] intbuf, final int offset, long v) {
  278. intbuf[offset + 7] = (byte) v;
  279. v >>>= 8;
  280. intbuf[offset + 6] = (byte) v;
  281. v >>>= 8;
  282. intbuf[offset + 5] = (byte) v;
  283. v >>>= 8;
  284. intbuf[offset + 4] = (byte) v;
  285. v >>>= 8;
  286. intbuf[offset + 3] = (byte) v;
  287. v >>>= 8;
  288. intbuf[offset + 2] = (byte) v;
  289. v >>>= 8;
  290. intbuf[offset + 1] = (byte) v;
  291. v >>>= 8;
  292. intbuf[offset] = (byte) v;
  293. }
  294. private NB() {
  295. // Don't create instances of a static only utility.
  296. }
  297. }