1. /*
  2. * @(#)MappedByteBuffer.java 1.23 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.nio;
  8. /**
  9. * A direct byte buffer whose content is a memory-mapped region of a file.
  10. *
  11. * <p> Mapped byte buffers are created via the {@link
  12. * java.nio.channels.FileChannel#map FileChannel.map} method. This class
  13. * extends the {@link ByteBuffer} class with operations that are specific to
  14. * memory-mapped file regions.
  15. *
  16. * <p> A mapped byte buffer and the file mapping that it represents remain
  17. * valid until the buffer itself is garbage-collected.
  18. *
  19. * <p> The content of a mapped byte buffer can change at any time, for example
  20. * if the content of the corresponding region of the mapped file is changed by
  21. * this program or another. Whether or not such changes occur, and when they
  22. * occur, is operating-system dependent and therefore unspecified.
  23. *
  24. * <a name="inaccess"><p> All or part of a mapped byte buffer may become
  25. * inaccessible at any time, for example if the mapped file is truncated. An
  26. * attempt to access an inaccessible region of a mapped byte buffer will not
  27. * change the buffer's content and will cause an unspecified exception to be
  28. * thrown either at the time of the access or at some later time. It is
  29. * therefore strongly recommended that appropriate precautions be taken to
  30. * avoid the manipulation of a mapped file by this program, or by a
  31. * concurrently running program, except to read or write the file's content.
  32. *
  33. * <p> Mapped byte buffers otherwise behave no differently than ordinary direct
  34. * byte buffers. </p>
  35. *
  36. *
  37. * @author Mark Reinhold
  38. * @author JSR-51 Expert Group
  39. * @version 1.23, 03/12/19
  40. * @since 1.4
  41. */
  42. public abstract class MappedByteBuffer
  43. extends ByteBuffer
  44. {
  45. // This is a little bit backwards: By rights MappedByteBuffer should be a
  46. // subclass of DirectByteBuffer, but to keep the spec clear and simple, and
  47. // for optimization purposes, it's easier to do it the other way around.
  48. // This works because DirectByteBuffer is a package-private class.
  49. // Volatile to make sure that the finalization thread sees the current
  50. // value of this so that a region is not accidentally unmapped again later.
  51. volatile boolean isAMappedBuffer; // package-private
  52. // This should only be invoked by the DirectByteBuffer constructors
  53. //
  54. MappedByteBuffer(int mark, int pos, int lim, int cap, // package-private
  55. boolean mapped)
  56. {
  57. super(mark, pos, lim, cap);
  58. isAMappedBuffer = mapped;
  59. }
  60. MappedByteBuffer(int mark, int pos, int lim, int cap) { // package-private
  61. super(mark, pos, lim, cap);
  62. isAMappedBuffer = false;
  63. }
  64. private void checkMapped() {
  65. if (!isAMappedBuffer)
  66. // Can only happen if a luser explicitly casts a direct byte buffer
  67. throw new UnsupportedOperationException();
  68. }
  69. /**
  70. * Tells whether or not this buffer's content is resident in physical
  71. * memory.
  72. *
  73. * <p> A return value of <tt>true</tt> implies that it is highly likely
  74. * that all of the data in this buffer is resident in physical memory and
  75. * may therefore be accessed without incurring any virtual-memory page
  76. * faults or I/O operations. A return value of <tt>false</tt> does not
  77. * necessarily imply that the buffer's content is not resident in physical
  78. * memory.
  79. *
  80. * <p> The returned value is a hint, rather than a guarantee, because the
  81. * underlying operating system may have paged out some of the buffer's data
  82. * by the time that an invocation of this method returns. </p>
  83. *
  84. * @return <tt>true</tt> if it is likely that this buffer's content
  85. * is resident in physical memory
  86. */
  87. public final boolean isLoaded() {
  88. checkMapped();
  89. if ((address == 0) || (capacity() == 0))
  90. return true;
  91. return isLoaded0(((DirectByteBuffer)this).address(), capacity());
  92. }
  93. /**
  94. * Loads this buffer's content into physical memory.
  95. *
  96. * <p> This method makes a best effort to ensure that, when it returns,
  97. * this buffer's content is resident in physical memory. Invoking this
  98. * method may cause some number of page faults and I/O operations to
  99. * occur. </p>
  100. *
  101. * @return This buffer
  102. */
  103. public final MappedByteBuffer load() {
  104. checkMapped();
  105. if ((address == 0) || (capacity() == 0))
  106. return this;
  107. load0(((DirectByteBuffer)this).address(), capacity(), Bits.pageSize());
  108. return this;
  109. }
  110. /**
  111. * Forces any changes made to this buffer's content to be written to the
  112. * storage device containing the mapped file.
  113. *
  114. * <p> If the file mapped into this buffer resides on a local storage
  115. * device then when this method returns it is guaranteed that all changes
  116. * made to the buffer since it was created, or since this method was last
  117. * invoked, will have been written to that device.
  118. *
  119. * <p> If the file does not reside on a local device then no such guarantee
  120. * is made.
  121. *
  122. * <p> If this buffer was not mapped in read/write mode ({@link
  123. * java.nio.channels.FileChannel.MapMode#READ_WRITE}) then invoking this
  124. * method has no effect. </p>
  125. *
  126. * @return This buffer
  127. */
  128. public final MappedByteBuffer force() {
  129. checkMapped();
  130. if ((address == 0) || (capacity() == 0))
  131. return this;
  132. force0(((DirectByteBuffer)this).address(), capacity());
  133. return this;
  134. }
  135. private native boolean isLoaded0(long address, long length);
  136. private native int load0(long address, long length, int pageSize);
  137. private native void force0(long address, long length);
  138. }