ByteBufferHelper.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* ByteBufferImpl.java --
  2. Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.nio;
  32. /**
  33. * @author Michael Koch (konqueror@gmx.de)
  34. */
  35. final class ByteBufferHelper
  36. {
  37. public static char getChar (ByteBuffer buffer, ByteOrder order)
  38. {
  39. return (char) getShort (buffer, order);
  40. }
  41. public static void putChar (ByteBuffer buffer, char value, ByteOrder order)
  42. {
  43. putShort (buffer, (short) value, order);
  44. }
  45. public static char getChar (ByteBuffer buffer, int index, ByteOrder order)
  46. {
  47. return (char) getShort (buffer, index, order);
  48. }
  49. public static void putChar (ByteBuffer buffer, int index,
  50. char value, ByteOrder order)
  51. {
  52. putShort (buffer, index, (short) value, order);
  53. }
  54. public static short getShort (ByteBuffer buffer, ByteOrder order)
  55. {
  56. buffer.checkForUnderflow(2);
  57. if (order == ByteOrder.LITTLE_ENDIAN)
  58. {
  59. return (short) ((buffer.get() & 0xff)
  60. + (buffer.get() << 8));
  61. }
  62. return (short) ((buffer.get() << 8)
  63. + (buffer.get() & 0xff));
  64. }
  65. public static void putShort (ByteBuffer buffer, short value, ByteOrder order)
  66. {
  67. buffer.checkForOverflow(2);
  68. if (order == ByteOrder.LITTLE_ENDIAN)
  69. {
  70. buffer.put ((byte) value);
  71. buffer.put ((byte) (value >> 8));
  72. }
  73. else
  74. {
  75. buffer.put ((byte) (value >> 8));
  76. buffer.put ((byte) value);
  77. }
  78. }
  79. public static short getShort (ByteBuffer buffer,
  80. int index, ByteOrder order)
  81. {
  82. if (order == ByteOrder.LITTLE_ENDIAN)
  83. {
  84. return (short) ((buffer.get (index) & 0xff)
  85. + (buffer.get (++index) << 8));
  86. }
  87. return (short) ((buffer.get (index) << 8)
  88. + (buffer.get (++index) & 0xff));
  89. }
  90. public static void putShort (ByteBuffer buffer, int index,
  91. short value, ByteOrder order)
  92. {
  93. if (order == ByteOrder.LITTLE_ENDIAN)
  94. {
  95. buffer.put (index, (byte) value);
  96. buffer.put (++index, (byte) (value >> 8));
  97. }
  98. else
  99. {
  100. buffer.put (index, (byte) (value >> 8));
  101. buffer.put (++index, (byte) value);
  102. }
  103. }
  104. public static int getInt (ByteBuffer buffer, ByteOrder order)
  105. {
  106. buffer.checkForUnderflow(4);
  107. if (order == ByteOrder.LITTLE_ENDIAN)
  108. {
  109. return ((buffer.get() & 0xff)
  110. + ((buffer.get() & 0xff) << 8)
  111. + ((buffer.get() & 0xff) << 16)
  112. + (buffer.get() << 24));
  113. }
  114. return (int) ((buffer.get() << 24)
  115. + ((buffer.get() & 0xff) << 16)
  116. + ((buffer.get() & 0xff) << 8)
  117. + (buffer.get() & 0xff));
  118. }
  119. public static void putInt (ByteBuffer buffer, int value, ByteOrder order)
  120. {
  121. buffer.checkForOverflow(4);
  122. if (order == ByteOrder.LITTLE_ENDIAN)
  123. {
  124. buffer.put ((byte) value);
  125. buffer.put ((byte) (value >> 8));
  126. buffer.put ((byte) (value >> 16));
  127. buffer.put ((byte) (value >> 24));
  128. }
  129. else
  130. {
  131. buffer.put ((byte) (value >> 24));
  132. buffer.put ((byte) (value >> 16));
  133. buffer.put ((byte) (value >> 8));
  134. buffer.put ((byte) value);
  135. }
  136. }
  137. public static int getInt (ByteBuffer buffer, int index, ByteOrder order)
  138. {
  139. if (order == ByteOrder.LITTLE_ENDIAN)
  140. {
  141. return ((buffer.get (index) & 0xff)
  142. + ((buffer.get (++index) & 0xff) << 8)
  143. + ((buffer.get (++index) & 0xff) << 16)
  144. + (buffer.get (++index) << 24));
  145. }
  146. return ((buffer.get (index) << 24)
  147. + ((buffer.get (++index) & 0xff) << 16)
  148. + ((buffer.get (++index) & 0xff) << 8)
  149. + (buffer.get (++index) & 0xff));
  150. }
  151. public static void putInt (ByteBuffer buffer, int index,
  152. int value, ByteOrder order)
  153. {
  154. if (order == ByteOrder.LITTLE_ENDIAN)
  155. {
  156. buffer.put (index, (byte) value);
  157. buffer.put (++index, (byte) (value >> 8));
  158. buffer.put (++index, (byte) (value >> 16));
  159. buffer.put (++index, (byte) (value >> 24));
  160. }
  161. else
  162. {
  163. buffer.put (index, (byte) (value >> 24));
  164. buffer.put (++index, (byte) (value >> 16));
  165. buffer.put (++index, (byte) (value >> 8));
  166. buffer.put (++index, (byte) value);
  167. }
  168. }
  169. public static long getLong (ByteBuffer buffer, ByteOrder order)
  170. {
  171. buffer.checkForUnderflow(8);
  172. if (order == ByteOrder.LITTLE_ENDIAN)
  173. {
  174. return ((buffer.get() & 0xff)
  175. + (((buffer.get() & 0xff)) << 8)
  176. + (((buffer.get() & 0xff)) << 16)
  177. + (((buffer.get() & 0xffL)) << 24)
  178. + (((buffer.get() & 0xffL)) << 32)
  179. + (((buffer.get() & 0xffL)) << 40)
  180. + (((buffer.get() & 0xffL)) << 48)
  181. + (((long) buffer.get()) << 56));
  182. }
  183. return ((((long) buffer.get()) << 56)
  184. + ((buffer.get() & 0xffL) << 48)
  185. + ((buffer.get() & 0xffL) << 40)
  186. + ((buffer.get() & 0xffL) << 32)
  187. + ((buffer.get() & 0xffL) << 24)
  188. + ((buffer.get() & 0xff) << 16)
  189. + ((buffer.get() & 0xff) << 8)
  190. + (buffer.get() & 0xff));
  191. }
  192. public static void putLong (ByteBuffer buffer, long value, ByteOrder order)
  193. {
  194. buffer.checkForOverflow(8);
  195. if (order == ByteOrder.LITTLE_ENDIAN)
  196. {
  197. buffer.put ((byte) value);
  198. buffer.put ((byte) (value >> 8));
  199. buffer.put ((byte) (value >> 16));
  200. buffer.put ((byte) (value >> 24));
  201. buffer.put ((byte) (value >> 32));
  202. buffer.put ((byte) (value >> 40));
  203. buffer.put ((byte) (value >> 48));
  204. buffer.put ((byte) (value >> 56));
  205. }
  206. else
  207. {
  208. buffer.put ((byte) (value >> 56));
  209. buffer.put ((byte) (value >> 48));
  210. buffer.put ((byte) (value >> 40));
  211. buffer.put ((byte) (value >> 32));
  212. buffer.put ((byte) (value >> 24));
  213. buffer.put ((byte) (value >> 16));
  214. buffer.put ((byte) (value >> 8));
  215. buffer.put ((byte) value);
  216. }
  217. }
  218. public static long getLong (ByteBuffer buffer, int index, ByteOrder order)
  219. {
  220. if (order == ByteOrder.LITTLE_ENDIAN)
  221. {
  222. return ((buffer.get (index) & 0xff)
  223. + ((buffer.get (++index) & 0xff) << 8)
  224. + ((buffer.get (++index) & 0xff) << 16)
  225. + ((buffer.get (++index) & 0xffL) << 24)
  226. + ((buffer.get (++index) & 0xffL) << 32)
  227. + ((buffer.get (++index) & 0xffL) << 40)
  228. + ((buffer.get (++index) & 0xffL) << 48)
  229. + (((long) buffer.get (++index)) << 56));
  230. }
  231. return ((((long) buffer.get (index)) << 56)
  232. + ((buffer.get (++index) & 0xffL) << 48)
  233. + ((buffer.get (++index) & 0xffL) << 40)
  234. + ((buffer.get (++index) & 0xffL) << 32)
  235. + ((buffer.get (++index) & 0xffL) << 24)
  236. + ((buffer.get (++index) & 0xff) << 16)
  237. + ((buffer.get (++index) & 0xff) << 8)
  238. + (buffer.get (++index) & 0xff));
  239. }
  240. public static void putLong (ByteBuffer buffer, int index,
  241. long value, ByteOrder order)
  242. {
  243. if (order == ByteOrder.LITTLE_ENDIAN)
  244. {
  245. buffer.put (index, (byte) value);
  246. buffer.put (++index, (byte) (value >> 8));
  247. buffer.put (++index, (byte) (value >> 16));
  248. buffer.put (++index, (byte) (value >> 24));
  249. buffer.put (++index, (byte) (value >> 32));
  250. buffer.put (++index, (byte) (value >> 40));
  251. buffer.put (++index, (byte) (value >> 48));
  252. buffer.put (++index, (byte) (value >> 56));
  253. }
  254. else
  255. {
  256. buffer.put (index, (byte) (value >> 56));
  257. buffer.put (++index, (byte) (value >> 48));
  258. buffer.put (++index, (byte) (value >> 40));
  259. buffer.put (++index, (byte) (value >> 32));
  260. buffer.put (++index, (byte) (value >> 24));
  261. buffer.put (++index, (byte) (value >> 16));
  262. buffer.put (++index, (byte) (value >> 8));
  263. buffer.put (++index, (byte) value);
  264. }
  265. }
  266. public static float getFloat (ByteBuffer buffer, ByteOrder order)
  267. {
  268. return Float.intBitsToFloat (getInt (buffer, order));
  269. }
  270. public static void putFloat (ByteBuffer buffer, float value, ByteOrder order)
  271. {
  272. putInt (buffer, Float.floatToRawIntBits (value), order);
  273. }
  274. public static float getFloat (ByteBuffer buffer, int index, ByteOrder order)
  275. {
  276. return Float.intBitsToFloat (getInt (buffer, index, order));
  277. }
  278. public static void putFloat (ByteBuffer buffer, int index,
  279. float value, ByteOrder order)
  280. {
  281. putInt (buffer, index, Float.floatToRawIntBits (value), order);
  282. }
  283. public static double getDouble (ByteBuffer buffer, ByteOrder order)
  284. {
  285. return Double.longBitsToDouble (getLong (buffer, order));
  286. }
  287. public static void putDouble (ByteBuffer buffer, double value, ByteOrder order)
  288. {
  289. putLong (buffer, Double.doubleToRawLongBits (value), order);
  290. }
  291. public static double getDouble (ByteBuffer buffer, int index, ByteOrder order)
  292. {
  293. return Double.longBitsToDouble (getLong (buffer, index, order));
  294. }
  295. public static void putDouble (ByteBuffer buffer, int index,
  296. double value, ByteOrder order)
  297. {
  298. putLong (buffer, index, Double.doubleToRawLongBits (value), order);
  299. }
  300. } // ByteBufferHelper