Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 < 0 if a < b; 0 if a == b; > 0 if a > 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 4 bytes (network byte order) into unsigned value.
  100. *
  101. * @param intbuf
  102. * buffer to acquire the 4 bytes of data from.
  103. * @param offset
  104. * position within the buffer to begin reading from. This
  105. * position and the next 3 bytes after it (for a total of 4
  106. * bytes) will be read.
  107. * @return unsigned integer value that matches the 32 bits read.
  108. */
  109. public static long decodeUInt32(final byte[] intbuf, final int offset) {
  110. int low = (intbuf[offset + 1] & 0xff) << 8;
  111. low |= (intbuf[offset + 2] & 0xff);
  112. low <<= 8;
  113. low |= (intbuf[offset + 3] & 0xff);
  114. return ((long) (intbuf[offset] & 0xff)) << 24 | low;
  115. }
  116. /**
  117. * Convert sequence of 8 bytes (network byte order) into unsigned value.
  118. *
  119. * @param intbuf
  120. * buffer to acquire the 8 bytes of data from.
  121. * @param offset
  122. * position within the buffer to begin reading from. This
  123. * position and the next 7 bytes after it (for a total of 8
  124. * bytes) will be read.
  125. * @return unsigned integer value that matches the 64 bits read.
  126. */
  127. public static long decodeUInt64(final byte[] intbuf, final int offset) {
  128. return (decodeUInt32(intbuf, offset) << 32)
  129. | decodeUInt32(intbuf, offset + 4);
  130. }
  131. /**
  132. * Write a 16 bit integer as a sequence of 2 bytes (network byte order).
  133. *
  134. * @param intbuf
  135. * buffer to write the 2 bytes of data into.
  136. * @param offset
  137. * position within the buffer to begin writing to. This position
  138. * and the next byte after it (for a total of 2 bytes) will be
  139. * replaced.
  140. * @param v
  141. * the value to write.
  142. */
  143. public static void encodeInt16(final byte[] intbuf, final int offset, int v) {
  144. intbuf[offset + 1] = (byte) v;
  145. v >>>= 8;
  146. intbuf[offset] = (byte) v;
  147. }
  148. /**
  149. * Write a 32 bit integer as a sequence of 4 bytes (network byte order).
  150. *
  151. * @param intbuf
  152. * buffer to write the 4 bytes of data into.
  153. * @param offset
  154. * position within the buffer to begin writing to. This position
  155. * and the next 3 bytes after it (for a total of 4 bytes) will be
  156. * replaced.
  157. * @param v
  158. * the value to write.
  159. */
  160. public static void encodeInt32(final byte[] intbuf, final int offset, int v) {
  161. intbuf[offset + 3] = (byte) v;
  162. v >>>= 8;
  163. intbuf[offset + 2] = (byte) v;
  164. v >>>= 8;
  165. intbuf[offset + 1] = (byte) v;
  166. v >>>= 8;
  167. intbuf[offset] = (byte) v;
  168. }
  169. /**
  170. * Write a 64 bit integer as a sequence of 8 bytes (network byte order).
  171. *
  172. * @param intbuf
  173. * buffer to write the 48bytes of data into.
  174. * @param offset
  175. * position within the buffer to begin writing to. This position
  176. * and the next 7 bytes after it (for a total of 8 bytes) will be
  177. * replaced.
  178. * @param v
  179. * the value to write.
  180. */
  181. public static void encodeInt64(final byte[] intbuf, final int offset, long v) {
  182. intbuf[offset + 7] = (byte) v;
  183. v >>>= 8;
  184. intbuf[offset + 6] = (byte) v;
  185. v >>>= 8;
  186. intbuf[offset + 5] = (byte) v;
  187. v >>>= 8;
  188. intbuf[offset + 4] = (byte) v;
  189. v >>>= 8;
  190. intbuf[offset + 3] = (byte) v;
  191. v >>>= 8;
  192. intbuf[offset + 2] = (byte) v;
  193. v >>>= 8;
  194. intbuf[offset + 1] = (byte) v;
  195. v >>>= 8;
  196. intbuf[offset] = (byte) v;
  197. }
  198. private NB() {
  199. // Don't create instances of a static only utility.
  200. }
  201. }