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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (C) 2008, 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. * Convert sequence of 2 bytes (network byte order) into unsigned value.
  67. *
  68. * @param intbuf
  69. * buffer to acquire the 2 bytes of data from.
  70. * @param offset
  71. * position within the buffer to begin reading from. This
  72. * position and the next byte after it (for a total of 2 bytes)
  73. * will be read.
  74. * @return unsigned integer value that matches the 16 bits read.
  75. */
  76. public static int decodeUInt16(final byte[] intbuf, final int offset) {
  77. int r = (intbuf[offset] & 0xff) << 8;
  78. return r | (intbuf[offset + 1] & 0xff);
  79. }
  80. /**
  81. * Convert sequence of 4 bytes (network byte order) into signed value.
  82. *
  83. * @param intbuf
  84. * buffer to acquire the 4 bytes of data from.
  85. * @param offset
  86. * position within the buffer to begin reading from. This
  87. * position and the next 3 bytes after it (for a total of 4
  88. * bytes) will be read.
  89. * @return signed integer value that matches the 32 bits read.
  90. */
  91. public static int decodeInt32(final byte[] intbuf, final int offset) {
  92. int r = intbuf[offset] << 8;
  93. r |= intbuf[offset + 1] & 0xff;
  94. r <<= 8;
  95. r |= intbuf[offset + 2] & 0xff;
  96. return (r << 8) | (intbuf[offset + 3] & 0xff);
  97. }
  98. /**
  99. * Convert sequence of 8 bytes (network byte order) into signed value.
  100. *
  101. * @param intbuf
  102. * buffer to acquire the 8 bytes of data from.
  103. * @param offset
  104. * position within the buffer to begin reading from. This
  105. * position and the next 7 bytes after it (for a total of 8
  106. * bytes) will be read.
  107. * @return signed integer value that matches the 64 bits read.
  108. * @since 3.0
  109. */
  110. public static long decodeInt64(final byte[] intbuf, final int offset) {
  111. long r = intbuf[offset] << 8;
  112. r |= intbuf[offset + 1] & 0xff;
  113. r <<= 8;
  114. r |= intbuf[offset + 2] & 0xff;
  115. r <<= 8;
  116. r |= intbuf[offset + 3] & 0xff;
  117. r <<= 8;
  118. r |= intbuf[offset + 4] & 0xff;
  119. r <<= 8;
  120. r |= intbuf[offset + 5] & 0xff;
  121. r <<= 8;
  122. r |= intbuf[offset + 6] & 0xff;
  123. return (r << 8) | (intbuf[offset + 7] & 0xff);
  124. }
  125. /**
  126. * Convert sequence of 4 bytes (network byte order) into unsigned value.
  127. *
  128. * @param intbuf
  129. * buffer to acquire the 4 bytes of data from.
  130. * @param offset
  131. * position within the buffer to begin reading from. This
  132. * position and the next 3 bytes after it (for a total of 4
  133. * bytes) will be read.
  134. * @return unsigned integer value that matches the 32 bits read.
  135. */
  136. public static long decodeUInt32(final byte[] intbuf, final int offset) {
  137. int low = (intbuf[offset + 1] & 0xff) << 8;
  138. low |= (intbuf[offset + 2] & 0xff);
  139. low <<= 8;
  140. low |= (intbuf[offset + 3] & 0xff);
  141. return ((long) (intbuf[offset] & 0xff)) << 24 | low;
  142. }
  143. /**
  144. * Convert sequence of 8 bytes (network byte order) into unsigned value.
  145. *
  146. * @param intbuf
  147. * buffer to acquire the 8 bytes of data from.
  148. * @param offset
  149. * position within the buffer to begin reading from. This
  150. * position and the next 7 bytes after it (for a total of 8
  151. * bytes) will be read.
  152. * @return unsigned integer value that matches the 64 bits read.
  153. */
  154. public static long decodeUInt64(final byte[] intbuf, final int offset) {
  155. return (decodeUInt32(intbuf, offset) << 32)
  156. | decodeUInt32(intbuf, offset + 4);
  157. }
  158. /**
  159. * Write a 16 bit integer as a sequence of 2 bytes (network byte order).
  160. *
  161. * @param intbuf
  162. * buffer to write the 2 bytes of data into.
  163. * @param offset
  164. * position within the buffer to begin writing to. This position
  165. * and the next byte after it (for a total of 2 bytes) will be
  166. * replaced.
  167. * @param v
  168. * the value to write.
  169. */
  170. public static void encodeInt16(final byte[] intbuf, final int offset, int v) {
  171. intbuf[offset + 1] = (byte) v;
  172. v >>>= 8;
  173. intbuf[offset] = (byte) v;
  174. }
  175. /**
  176. * Write a 32 bit integer as a sequence of 4 bytes (network byte order).
  177. *
  178. * @param intbuf
  179. * buffer to write the 4 bytes of data into.
  180. * @param offset
  181. * position within the buffer to begin writing to. This position
  182. * and the next 3 bytes after it (for a total of 4 bytes) will be
  183. * replaced.
  184. * @param v
  185. * the value to write.
  186. */
  187. public static void encodeInt32(final byte[] intbuf, final int offset, int v) {
  188. intbuf[offset + 3] = (byte) v;
  189. v >>>= 8;
  190. intbuf[offset + 2] = (byte) v;
  191. v >>>= 8;
  192. intbuf[offset + 1] = (byte) v;
  193. v >>>= 8;
  194. intbuf[offset] = (byte) v;
  195. }
  196. /**
  197. * Write a 64 bit integer as a sequence of 8 bytes (network byte order).
  198. *
  199. * @param intbuf
  200. * buffer to write the 8 bytes of data into.
  201. * @param offset
  202. * position within the buffer to begin writing to. This position
  203. * and the next 7 bytes after it (for a total of 8 bytes) will be
  204. * replaced.
  205. * @param v
  206. * the value to write.
  207. */
  208. public static void encodeInt64(final byte[] intbuf, final int offset, long v) {
  209. intbuf[offset + 7] = (byte) v;
  210. v >>>= 8;
  211. intbuf[offset + 6] = (byte) v;
  212. v >>>= 8;
  213. intbuf[offset + 5] = (byte) v;
  214. v >>>= 8;
  215. intbuf[offset + 4] = (byte) v;
  216. v >>>= 8;
  217. intbuf[offset + 3] = (byte) v;
  218. v >>>= 8;
  219. intbuf[offset + 2] = (byte) v;
  220. v >>>= 8;
  221. intbuf[offset + 1] = (byte) v;
  222. v >>>= 8;
  223. intbuf[offset] = (byte) v;
  224. }
  225. private NB() {
  226. // Don't create instances of a static only utility.
  227. }
  228. }