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 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. /** Conversion utilities for network byte order handling. */
  45. public final class NB {
  46. /**
  47. * Compare a 32 bit unsigned integer stored in a 32 bit signed integer.
  48. * <p>
  49. * This function performs an unsigned compare operation, even though Java
  50. * does not natively support unsigned integer values. Negative numbers are
  51. * treated as larger than positive ones.
  52. *
  53. * @param a
  54. * the first value to compare.
  55. * @param b
  56. * the second value to compare.
  57. * @return &lt; 0 if a &lt; b; 0 if a == b; &gt; 0 if a &gt; b.
  58. */
  59. public static int compareUInt32(final int a, final int b) {
  60. final int cmp = (a >>> 1) - (b >>> 1);
  61. if (cmp != 0)
  62. return cmp;
  63. return (a & 1) - (b & 1);
  64. }
  65. /**
  66. * Compare a 64 bit unsigned integer stored in a 64 bit signed integer.
  67. * <p>
  68. * This function performs an unsigned compare operation, even though Java
  69. * does not natively support unsigned integer values. Negative numbers are
  70. * treated as larger than positive ones.
  71. *
  72. * @param a
  73. * the first value to compare.
  74. * @param b
  75. * the second value to compare.
  76. * @return &lt; 0 if a &lt; b; 0 if a == b; &gt; 0 if a &gt; b.
  77. * @since 4.3
  78. */
  79. public static int compareUInt64(final long a, final long b) {
  80. long cmp = (a >>> 1) - (b >>> 1);
  81. if (cmp > 0) {
  82. return 1;
  83. } else if (cmp < 0) {
  84. return -1;
  85. }
  86. cmp = ((a & 1) - (b & 1));
  87. if (cmp > 0) {
  88. return 1;
  89. } else if (cmp < 0) {
  90. return -1;
  91. } else {
  92. return 0;
  93. }
  94. }
  95. /**
  96. * Convert sequence of 2 bytes (network byte order) into unsigned value.
  97. *
  98. * @param intbuf
  99. * buffer to acquire the 2 bytes of data from.
  100. * @param offset
  101. * position within the buffer to begin reading from. This
  102. * position and the next byte after it (for a total of 2 bytes)
  103. * will be read.
  104. * @return unsigned integer value that matches the 16 bits read.
  105. */
  106. public static int decodeUInt16(final byte[] intbuf, final int offset) {
  107. int r = (intbuf[offset] & 0xff) << 8;
  108. return r | (intbuf[offset + 1] & 0xff);
  109. }
  110. /**
  111. * Convert sequence of 4 bytes (network byte order) into signed value.
  112. *
  113. * @param intbuf
  114. * buffer to acquire the 4 bytes of data from.
  115. * @param offset
  116. * position within the buffer to begin reading from. This
  117. * position and the next 3 bytes after it (for a total of 4
  118. * bytes) will be read.
  119. * @return signed integer value that matches the 32 bits read.
  120. */
  121. public static int decodeInt32(final byte[] intbuf, final int offset) {
  122. int r = intbuf[offset] << 8;
  123. r |= intbuf[offset + 1] & 0xff;
  124. r <<= 8;
  125. r |= intbuf[offset + 2] & 0xff;
  126. return (r << 8) | (intbuf[offset + 3] & 0xff);
  127. }
  128. /**
  129. * Convert sequence of 8 bytes (network byte order) into signed value.
  130. *
  131. * @param intbuf
  132. * buffer to acquire the 8 bytes of data from.
  133. * @param offset
  134. * position within the buffer to begin reading from. This
  135. * position and the next 7 bytes after it (for a total of 8
  136. * bytes) will be read.
  137. * @return signed integer value that matches the 64 bits read.
  138. * @since 3.0
  139. */
  140. public static long decodeInt64(final byte[] intbuf, final int offset) {
  141. long r = intbuf[offset] << 8;
  142. r |= intbuf[offset + 1] & 0xff;
  143. r <<= 8;
  144. r |= intbuf[offset + 2] & 0xff;
  145. r <<= 8;
  146. r |= intbuf[offset + 3] & 0xff;
  147. r <<= 8;
  148. r |= intbuf[offset + 4] & 0xff;
  149. r <<= 8;
  150. r |= intbuf[offset + 5] & 0xff;
  151. r <<= 8;
  152. r |= intbuf[offset + 6] & 0xff;
  153. return (r << 8) | (intbuf[offset + 7] & 0xff);
  154. }
  155. /**
  156. * Convert sequence of 4 bytes (network byte order) into unsigned value.
  157. *
  158. * @param intbuf
  159. * buffer to acquire the 4 bytes of data from.
  160. * @param offset
  161. * position within the buffer to begin reading from. This
  162. * position and the next 3 bytes after it (for a total of 4
  163. * bytes) will be read.
  164. * @return unsigned integer value that matches the 32 bits read.
  165. */
  166. public static long decodeUInt32(final byte[] intbuf, final int offset) {
  167. int low = (intbuf[offset + 1] & 0xff) << 8;
  168. low |= (intbuf[offset + 2] & 0xff);
  169. low <<= 8;
  170. low |= (intbuf[offset + 3] & 0xff);
  171. return ((long) (intbuf[offset] & 0xff)) << 24 | low;
  172. }
  173. /**
  174. * Convert sequence of 8 bytes (network byte order) into unsigned value.
  175. *
  176. * @param intbuf
  177. * buffer to acquire the 8 bytes of data from.
  178. * @param offset
  179. * position within the buffer to begin reading from. This
  180. * position and the next 7 bytes after it (for a total of 8
  181. * bytes) will be read.
  182. * @return unsigned integer value that matches the 64 bits read.
  183. */
  184. public static long decodeUInt64(final byte[] intbuf, final int offset) {
  185. return (decodeUInt32(intbuf, offset) << 32)
  186. | decodeUInt32(intbuf, offset + 4);
  187. }
  188. /**
  189. * Write a 16 bit integer as a sequence of 2 bytes (network byte order).
  190. *
  191. * @param intbuf
  192. * buffer to write the 2 bytes of data into.
  193. * @param offset
  194. * position within the buffer to begin writing to. This position
  195. * and the next byte after it (for a total of 2 bytes) will be
  196. * replaced.
  197. * @param v
  198. * the value to write.
  199. */
  200. public static void encodeInt16(final byte[] intbuf, final int offset, int v) {
  201. intbuf[offset + 1] = (byte) v;
  202. v >>>= 8;
  203. intbuf[offset] = (byte) v;
  204. }
  205. /**
  206. * Write a 32 bit integer as a sequence of 4 bytes (network byte order).
  207. *
  208. * @param intbuf
  209. * buffer to write the 4 bytes of data into.
  210. * @param offset
  211. * position within the buffer to begin writing to. This position
  212. * and the next 3 bytes after it (for a total of 4 bytes) will be
  213. * replaced.
  214. * @param v
  215. * the value to write.
  216. */
  217. public static void encodeInt32(final byte[] intbuf, final int offset, int v) {
  218. intbuf[offset + 3] = (byte) v;
  219. v >>>= 8;
  220. intbuf[offset + 2] = (byte) v;
  221. v >>>= 8;
  222. intbuf[offset + 1] = (byte) v;
  223. v >>>= 8;
  224. intbuf[offset] = (byte) v;
  225. }
  226. /**
  227. * Write a 64 bit integer as a sequence of 8 bytes (network byte order).
  228. *
  229. * @param intbuf
  230. * buffer to write the 8 bytes of data into.
  231. * @param offset
  232. * position within the buffer to begin writing to. This position
  233. * and the next 7 bytes after it (for a total of 8 bytes) will be
  234. * replaced.
  235. * @param v
  236. * the value to write.
  237. */
  238. public static void encodeInt64(final byte[] intbuf, final int offset, long v) {
  239. intbuf[offset + 7] = (byte) v;
  240. v >>>= 8;
  241. intbuf[offset + 6] = (byte) v;
  242. v >>>= 8;
  243. intbuf[offset + 5] = (byte) v;
  244. v >>>= 8;
  245. intbuf[offset + 4] = (byte) v;
  246. v >>>= 8;
  247. intbuf[offset + 3] = (byte) v;
  248. v >>>= 8;
  249. intbuf[offset + 2] = (byte) v;
  250. v >>>= 8;
  251. intbuf[offset + 1] = (byte) v;
  252. v >>>= 8;
  253. intbuf[offset] = (byte) v;
  254. }
  255. private NB() {
  256. // Don't create instances of a static only utility.
  257. }
  258. }