1. /*
  2. * @(#)MappedByteBuffer.java 1.21 03/01/23
  3. *
  4. * Copyright 2003 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.21, 03/01/23
  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. return isLoaded0(((DirectByteBuffer)this).address(), capacity());
  90. }
  91. /**
  92. * Loads this buffer's content into physical memory.
  93. *
  94. * <p> This method makes a best effort to ensure that, when it returns,
  95. * this buffer's content is resident in physical memory. Invoking this
  96. * method may cause some number of page faults and I/O operations to
  97. * occur. </p>
  98. *
  99. * @return This buffer
  100. */
  101. public final MappedByteBuffer load() {
  102. checkMapped();
  103. load0(((DirectByteBuffer)this).address(), capacity(), Bits.pageSize());
  104. return this;
  105. }
  106. /**
  107. * Forces any changes made to this buffer's content to be written to the
  108. * storage device containing the mapped file.
  109. *
  110. * <p> If the file mapped into this buffer resides on a local storage
  111. * device then when this method returns it is guaranteed that all changes
  112. * made to the buffer since it was created, or since this method was last
  113. * invoked, will have been written to that device.
  114. *
  115. * <p> If the file does not reside on a local device then no such guarantee
  116. * is made.
  117. *
  118. * <p> If this buffer was not mapped in read/write mode ({@link
  119. * java.nio.channels.FileChannel.MapMode#READ_WRITE}) then invoking this
  120. * method has no effect. </p>
  121. *
  122. * @return This buffer
  123. */
  124. public final MappedByteBuffer force() {
  125. checkMapped();
  126. force0(((DirectByteBuffer)this).address(), capacity());
  127. return this;
  128. }
  129. private native boolean isLoaded0(long address, long length);
  130. private native int load0(long address, long length, int pageSize);
  131. private native void force0(long address, long length);
  132. }