MappedByteBufferImpl.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* MappedByteBufferImpl.java --
  2. Copyright (C) 2002, 2003, 2004 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. import gnu.classpath.Pointer;
  33. import java.io.IOException;
  34. final class MappedByteBufferImpl extends MappedByteBuffer
  35. {
  36. private final boolean readOnly;
  37. /** Posix uses this for the pointer returned by mmap;
  38. * Win32 uses it for the pointer returned by MapViewOfFile. */
  39. public Pointer implPtr;
  40. /** Posix uses this for the actual length passed to mmap;
  41. * Win32 uses it for the pointer returned by CreateFileMapping. */
  42. public long implLen;
  43. public MappedByteBufferImpl(Pointer address, int size, boolean readOnly)
  44. throws IOException
  45. {
  46. super(size, size, 0, -1, address);
  47. this.readOnly = readOnly;
  48. }
  49. public boolean isReadOnly()
  50. {
  51. return readOnly;
  52. }
  53. public byte get()
  54. {
  55. checkForUnderflow();
  56. int pos = position();
  57. byte result = VMDirectByteBuffer.get(address, pos);
  58. position(pos + 1);
  59. return result;
  60. }
  61. public ByteBuffer put(byte value)
  62. {
  63. checkIfReadOnly();
  64. checkForOverflow();
  65. int pos = position();
  66. VMDirectByteBuffer.put(address, pos, value);
  67. position(pos + 1);
  68. return this;
  69. }
  70. public byte get(int index)
  71. {
  72. checkIndex(index);
  73. return VMDirectByteBuffer.get(address, index);
  74. }
  75. public ByteBuffer get(byte[] dst, int offset, int length)
  76. {
  77. checkArraySize(dst.length, offset, length);
  78. checkForUnderflow(length);
  79. int index = position();
  80. VMDirectByteBuffer.get(address, index, dst, offset, length);
  81. position(index+length);
  82. return this;
  83. }
  84. public ByteBuffer put(int index, byte value)
  85. {
  86. checkIfReadOnly();
  87. checkIndex(index);
  88. VMDirectByteBuffer.put(address, index, value);
  89. return this;
  90. }
  91. public ByteBuffer compact()
  92. {
  93. checkIfReadOnly();
  94. mark = -1;
  95. int pos = position();
  96. if (pos > 0)
  97. {
  98. int count = remaining();
  99. // Call shiftDown method optimized for direct buffers.
  100. VMDirectByteBuffer.shiftDown(address, 0, pos, count);
  101. position(count);
  102. limit(capacity());
  103. }
  104. else
  105. {
  106. position(limit());
  107. limit(capacity());
  108. }
  109. return this;
  110. }
  111. public boolean isDirect()
  112. {
  113. return true;
  114. }
  115. public ByteBuffer slice()
  116. {
  117. int rem = remaining();
  118. if (isReadOnly())
  119. return new DirectByteBufferImpl.ReadOnly
  120. (this, VMDirectByteBuffer.adjustAddress(address, position()),
  121. rem, rem, 0);
  122. else
  123. return new DirectByteBufferImpl.ReadWrite
  124. (this, VMDirectByteBuffer.adjustAddress(address, position()),
  125. rem, rem, 0);
  126. }
  127. private ByteBuffer duplicate(boolean readOnly)
  128. {
  129. int pos = position();
  130. reset();
  131. int mark = position();
  132. position(pos);
  133. DirectByteBufferImpl result;
  134. if (readOnly)
  135. result = new DirectByteBufferImpl.ReadOnly(this, address, capacity(),
  136. limit(), pos);
  137. else
  138. result = new DirectByteBufferImpl.ReadWrite(this, address, capacity(),
  139. limit(), pos);
  140. if (mark != pos)
  141. {
  142. result.position(mark);
  143. result.mark();
  144. result.position(pos);
  145. }
  146. return result;
  147. }
  148. public ByteBuffer duplicate()
  149. {
  150. return duplicate(isReadOnly());
  151. }
  152. public ByteBuffer asReadOnlyBuffer()
  153. {
  154. return duplicate(true);
  155. }
  156. public CharBuffer asCharBuffer()
  157. {
  158. return new CharViewBufferImpl(this, remaining() >> 1);
  159. }
  160. public ShortBuffer asShortBuffer()
  161. {
  162. return new ShortViewBufferImpl(this, remaining() >> 1);
  163. }
  164. public IntBuffer asIntBuffer()
  165. {
  166. return new IntViewBufferImpl(this, remaining() >> 2);
  167. }
  168. public LongBuffer asLongBuffer()
  169. {
  170. return new LongViewBufferImpl(this, remaining() >> 3);
  171. }
  172. public FloatBuffer asFloatBuffer()
  173. {
  174. return new FloatViewBufferImpl(this, remaining() >> 2);
  175. }
  176. public DoubleBuffer asDoubleBuffer()
  177. {
  178. return new DoubleViewBufferImpl(this, remaining() >> 3);
  179. }
  180. public char getChar()
  181. {
  182. return ByteBufferHelper.getChar(this, order());
  183. }
  184. public ByteBuffer putChar(char value)
  185. {
  186. ByteBufferHelper.putChar(this, value, order());
  187. return this;
  188. }
  189. public char getChar(int index)
  190. {
  191. return ByteBufferHelper.getChar(this, index, order());
  192. }
  193. public ByteBuffer putChar(int index, char value)
  194. {
  195. ByteBufferHelper.putChar(this, index, value, order());
  196. return this;
  197. }
  198. public short getShort()
  199. {
  200. return ByteBufferHelper.getShort(this, order());
  201. }
  202. public ByteBuffer putShort(short value)
  203. {
  204. ByteBufferHelper.putShort(this, value, order());
  205. return this;
  206. }
  207. public short getShort(int index)
  208. {
  209. return ByteBufferHelper.getShort(this, index, order());
  210. }
  211. public ByteBuffer putShort(int index, short value)
  212. {
  213. ByteBufferHelper.putShort(this, index, value, order());
  214. return this;
  215. }
  216. public int getInt()
  217. {
  218. return ByteBufferHelper.getInt(this, order());
  219. }
  220. public ByteBuffer putInt(int value)
  221. {
  222. ByteBufferHelper.putInt(this, value, order());
  223. return this;
  224. }
  225. public int getInt(int index)
  226. {
  227. return ByteBufferHelper.getInt(this, index, order());
  228. }
  229. public ByteBuffer putInt(int index, int value)
  230. {
  231. ByteBufferHelper.putInt(this, index, value, order());
  232. return this;
  233. }
  234. public long getLong()
  235. {
  236. return ByteBufferHelper.getLong(this, order());
  237. }
  238. public ByteBuffer putLong(long value)
  239. {
  240. ByteBufferHelper.putLong(this, value, order());
  241. return this;
  242. }
  243. public long getLong(int index)
  244. {
  245. return ByteBufferHelper.getLong(this, index, order());
  246. }
  247. public ByteBuffer putLong(int index, long value)
  248. {
  249. ByteBufferHelper.putLong(this, index, value, order());
  250. return this;
  251. }
  252. public float getFloat()
  253. {
  254. return ByteBufferHelper.getFloat(this, order());
  255. }
  256. public ByteBuffer putFloat(float value)
  257. {
  258. ByteBufferHelper.putFloat(this, value, order());
  259. return this;
  260. }
  261. public float getFloat(int index)
  262. {
  263. return ByteBufferHelper.getFloat(this, index, order());
  264. }
  265. public ByteBuffer putFloat(int index, float value)
  266. {
  267. ByteBufferHelper.putFloat(this, index, value, order());
  268. return this;
  269. }
  270. public double getDouble()
  271. {
  272. return ByteBufferHelper.getDouble(this, order());
  273. }
  274. public ByteBuffer putDouble(double value)
  275. {
  276. ByteBufferHelper.putDouble(this, value, order());
  277. return this;
  278. }
  279. public double getDouble(int index)
  280. {
  281. return ByteBufferHelper.getDouble(this, index, order());
  282. }
  283. public ByteBuffer putDouble(int index, double value)
  284. {
  285. ByteBufferHelper.putDouble(this, index, value, order());
  286. return this;
  287. }
  288. // NOTE: In libgcj these methods are implemented in natFileChannelXxx.cc,
  289. // because they're small, and to put them next to FileChannelImpl::mapImpl.
  290. native void unmapImpl();
  291. native boolean isLoadedImpl();
  292. // FIXME: Try to load all pages into memory.
  293. native void loadImpl();
  294. native void forceImpl();
  295. }