984 }
985
986 private static final int MAP_INVALID = -1;
987 private static final int MAP_RO = 0;
988 private static final int MAP_RW = 1;
989 private static final int MAP_PV = 2;
990
991 public MappedByteBuffer map(MapMode mode, long position, long size) throws IOException {
992 if (size > Integer.MAX_VALUE)
993 throw new IllegalArgumentException("Size exceeds Integer.MAX_VALUE");
994 boolean isSync = isSync(Objects.requireNonNull(mode, "Mode is null"));
995 int prot = toProt(mode);
996 Unmapper unmapper = mapInternal(mode, position, size, prot, isSync);
997 if (unmapper == null) {
998 // a valid file descriptor is not required
999 FileDescriptor dummy = new FileDescriptor();
1000 if ((!writable) || (prot == MAP_RO))
1001 return Util.newMappedByteBufferR(0, 0, dummy, null, isSync);
1002 else
1003 return Util.newMappedByteBuffer(0, 0, dummy, null, isSync);
1004 }
1005 else if ((!writable) || (prot == MAP_RO)) {
1006 return Util.newMappedByteBufferR((int)unmapper.cap,
1007 unmapper.address + unmapper.pagePosition,
1008 unmapper.fd,
1009 unmapper, isSync);
1010 } else {
1011 return Util.newMappedByteBuffer((int)unmapper.cap,
1012 unmapper.address + unmapper.pagePosition,
1013 unmapper.fd,
1014 unmapper, isSync);
1015 }
1016 }
1017
1018 public Unmapper mapInternal(MapMode mode, long position, long size) throws IOException {
1019 boolean isSync = isSync(Objects.requireNonNull(mode, "Mode is null"));
1020 int prot = toProt(mode);
1021 return mapInternal(mode, position, size, prot, isSync);
1022 }
1023
1024 private Unmapper mapInternal(MapMode mode, long position, long size, int prot, boolean isSync)
1025 throws IOException
1106
1107 assert (IOStatus.checkAll(addr));
1108 assert (addr % allocationGranularity == 0);
1109 Unmapper um = (isSync
1110 ? new SyncUnmapper(addr, mapSize, size, mfd, pagePosition)
1111 : new DefaultUnmapper(addr, mapSize, size, mfd, pagePosition));
1112 return um;
1113 } finally {
1114 threads.remove(ti);
1115 endBlocking(IOStatus.checkAll(addr));
1116 }
1117 }
1118
1119 private boolean isSync(MapMode mode) {
1120 return mode == ExtendedMapMode.READ_ONLY_SYNC ||
1121 mode == ExtendedMapMode.READ_WRITE_SYNC;
1122 }
1123
1124 private int toProt(MapMode mode) {
1125 int prot;
1126 if (mode == MapMode.READ_ONLY)
1127 prot = MAP_RO;
1128 else if (mode == MapMode.READ_WRITE)
1129 prot = MAP_RW;
1130 else if (mode == MapMode.PRIVATE)
1131 prot = MAP_PV;
1132 else if (mode == ExtendedMapMode.READ_ONLY_SYNC) {
1133 prot = MAP_RO;
1134 } else if (mode == ExtendedMapMode.READ_WRITE_SYNC) {
1135 prot = MAP_RW;
1136 } else {
1137 prot = MAP_INVALID;
1138 }
1139 return prot;
1140 }
1141
1142 private void checkMode(MapMode mode, int prot, boolean isSync) {
1143 if (prot == MAP_INVALID) {
1144 throw new UnsupportedOperationException();
1145 }
1146 if ((mode != MapMode.READ_ONLY) && mode != ExtendedMapMode.READ_ONLY_SYNC && !writable)
1147 throw new NonWritableChannelException();
1148 if (!readable)
1149 throw new NonReadableChannelException();
1150 // reject SYNC request if writeback is not enabled for this platform
1151 if (isSync && !Unsafe.isWritebackEnabled()) {
1152 throw new UnsupportedOperationException();
|
984 }
985
986 private static final int MAP_INVALID = -1;
987 private static final int MAP_RO = 0;
988 private static final int MAP_RW = 1;
989 private static final int MAP_PV = 2;
990
991 public MappedByteBuffer map(MapMode mode, long position, long size) throws IOException {
992 if (size > Integer.MAX_VALUE)
993 throw new IllegalArgumentException("Size exceeds Integer.MAX_VALUE");
994 boolean isSync = isSync(Objects.requireNonNull(mode, "Mode is null"));
995 int prot = toProt(mode);
996 Unmapper unmapper = mapInternal(mode, position, size, prot, isSync);
997 if (unmapper == null) {
998 // a valid file descriptor is not required
999 FileDescriptor dummy = new FileDescriptor();
1000 if ((!writable) || (prot == MAP_RO))
1001 return Util.newMappedByteBufferR(0, 0, dummy, null, isSync);
1002 else
1003 return Util.newMappedByteBuffer(0, 0, dummy, null, isSync);
1004 } else if ((!writable) || (prot == MAP_RO)) {
1005 return Util.newMappedByteBufferR((int)unmapper.cap,
1006 unmapper.address + unmapper.pagePosition,
1007 unmapper.fd,
1008 unmapper, isSync);
1009 } else {
1010 return Util.newMappedByteBuffer((int)unmapper.cap,
1011 unmapper.address + unmapper.pagePosition,
1012 unmapper.fd,
1013 unmapper, isSync);
1014 }
1015 }
1016
1017 public Unmapper mapInternal(MapMode mode, long position, long size) throws IOException {
1018 boolean isSync = isSync(Objects.requireNonNull(mode, "Mode is null"));
1019 int prot = toProt(mode);
1020 return mapInternal(mode, position, size, prot, isSync);
1021 }
1022
1023 private Unmapper mapInternal(MapMode mode, long position, long size, int prot, boolean isSync)
1024 throws IOException
1105
1106 assert (IOStatus.checkAll(addr));
1107 assert (addr % allocationGranularity == 0);
1108 Unmapper um = (isSync
1109 ? new SyncUnmapper(addr, mapSize, size, mfd, pagePosition)
1110 : new DefaultUnmapper(addr, mapSize, size, mfd, pagePosition));
1111 return um;
1112 } finally {
1113 threads.remove(ti);
1114 endBlocking(IOStatus.checkAll(addr));
1115 }
1116 }
1117
1118 private boolean isSync(MapMode mode) {
1119 return mode == ExtendedMapMode.READ_ONLY_SYNC ||
1120 mode == ExtendedMapMode.READ_WRITE_SYNC;
1121 }
1122
1123 private int toProt(MapMode mode) {
1124 int prot;
1125 if (mode == MapMode.READ_ONLY) {
1126 prot = MAP_RO;
1127 } else if (mode == MapMode.READ_WRITE) {
1128 prot = MAP_RW;
1129 } else if (mode == MapMode.PRIVATE) {
1130 prot = MAP_PV;
1131 } else if (mode == ExtendedMapMode.READ_ONLY_SYNC) {
1132 prot = MAP_RO;
1133 } else if (mode == ExtendedMapMode.READ_WRITE_SYNC) {
1134 prot = MAP_RW;
1135 } else {
1136 prot = MAP_INVALID;
1137 }
1138 return prot;
1139 }
1140
1141 private void checkMode(MapMode mode, int prot, boolean isSync) {
1142 if (prot == MAP_INVALID) {
1143 throw new UnsupportedOperationException();
1144 }
1145 if ((mode != MapMode.READ_ONLY) && mode != ExtendedMapMode.READ_ONLY_SYNC && !writable)
1146 throw new NonWritableChannelException();
1147 if (!readable)
1148 throw new NonReadableChannelException();
1149 // reject SYNC request if writeback is not enabled for this platform
1150 if (isSync && !Unsafe.isWritebackEnabled()) {
1151 throw new UnsupportedOperationException();
|