1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
| public MappedByteBuffer map(MapMode mode, long position, long size) throws IOException { ensureOpen(); if (position < 0L) throw new IllegalArgumentException("Negative position"); if (size < 0L) throw new IllegalArgumentException("Negative size"); if (position + size < 0) throw new IllegalArgumentException("Position + size overflow"); if (size > Integer.MAX_VALUE) throw new IllegalArgumentException("Size exceeds Integer.MAX_VALUE"); int imode = -1; if (mode == MapMode.READ_ONLY) imode = MAP_RO; else if (mode == MapMode.READ_WRITE) imode = MAP_RW; else if (mode == MapMode.PRIVATE) imode = MAP_PV; assert (imode >= 0); if ((mode != MapMode.READ_ONLY) && !writable) throw new NonWritableChannelException(); if (!readable) throw new NonReadableChannelException();
long addr = -1; int ti = -1; try { begin(); ti = threads.add(); if (!isOpen()) return null; if (size() < position + size) { if (!writable) { throw new IOException("Channel not open for writing " + "- cannot extend file to required size"); } int rv; do { rv = nd.truncate(fd, position + size); } while ((rv == IOStatus.INTERRUPTED) && isOpen()); } if (size == 0) { addr = 0; FileDescriptor dummy = new FileDescriptor(); if ((!writable) || (imode == MAP_RO)) return Util.newMappedByteBufferR(0, 0, dummy, null); else return Util.newMappedByteBuffer(0, 0, dummy, null); }
int pagePosition = (int)(position % allocationGranularity); long mapPosition = position - pagePosition; long mapSize = size + pagePosition; try { addr = map0(imode, mapPosition, mapSize); } catch (OutOfMemoryError x) { System.gc(); try { Thread.sleep(100); } catch (InterruptedException y) { Thread.currentThread().interrupt(); } try { addr = map0(imode, mapPosition, mapSize); } catch (OutOfMemoryError y) { throw new IOException("Map failed", y); } }
FileDescriptor mfd; try { mfd = nd.duplicateForMapping(fd); } catch (IOException ioe) { unmap0(addr, mapSize); throw ioe; }
assert (IOStatus.checkAll(addr)); assert (addr % allocationGranularity == 0); int isize = (int)size; Unmapper um = new Unmapper(addr, mapSize, isize, mfd); if ((!writable) || (imode == MAP_RO)) { return Util.newMappedByteBufferR(isize, addr + pagePosition, mfd, um); } else { return Util.newMappedByteBuffer(isize, addr + pagePosition, mfd, um); } } finally { threads.remove(ti); end(IOStatus.checkAll(addr)); } }
JNIEXPORT jlong JNICALL Java_sun_nio_ch_FileChannelImpl_map0(JNIEnv *env, jobject this, jint prot, jlong off, jlong len) { void *mapAddress = 0; jint lowOffset = (jint)off; jint highOffset = (jint)(off >> 32); jlong maxSize = off + len; jint lowLen = (jint)(maxSize); jint highLen = (jint)(maxSize >> 32); jobject fdo = (*env)->GetObjectField(env, this, chan_fd); HANDLE fileHandle = (HANDLE)(handleval(env, fdo)); HANDLE mapping; DWORD mapAccess = FILE_MAP_READ; DWORD fileProtect = PAGE_READONLY; DWORD mapError; BOOL result;
if (prot == sun_nio_ch_FileChannelImpl_MAP_RO) { fileProtect = PAGE_READONLY; mapAccess = FILE_MAP_READ; } else if (prot == sun_nio_ch_FileChannelImpl_MAP_RW) { fileProtect = PAGE_READWRITE; mapAccess = FILE_MAP_WRITE; } else if (prot == sun_nio_ch_FileChannelImpl_MAP_PV) { fileProtect = PAGE_WRITECOPY; mapAccess = FILE_MAP_COPY; }
mapping = CreateFileMapping( fileHandle, NULL, fileProtect, highLen, lowLen, NULL);
if (mapping == NULL) { JNU_ThrowIOExceptionWithLastError(env, "Map failed"); return IOS_THROWN; }
mapAddress = MapViewOfFile( mapping, mapAccess, highOffset, lowOffset, (DWORD)len); mapError = GetLastError();
result = CloseHandle(mapping); if (result == 0) { JNU_ThrowIOExceptionWithLastError(env, "Map failed"); return IOS_THROWN; }
if (mapAddress == NULL) { if (mapError == ERROR_NOT_ENOUGH_MEMORY) JNU_ThrowOutOfMemoryError(env, "Map failed"); else JNU_ThrowIOExceptionWithLastError(env, "Map failed"); return IOS_THROWN; }
return ptr_to_jlong(mapAddress); }
JNIEXPORT jlong JNICALL Java_sun_nio_ch_FileChannelImpl_map0(JNIEnv *env, jobject this, jint prot, jlong off, jlong len) { void *mapAddress = 0; jobject fdo = (*env)->GetObjectField(env, this, chan_fd); jint fd = fdval(env, fdo); int protections = 0; int flags = 0;
if (prot == sun_nio_ch_FileChannelImpl_MAP_RO) { protections = PROT_READ; flags = MAP_SHARED; } else if (prot == sun_nio_ch_FileChannelImpl_MAP_RW) { protections = PROT_WRITE | PROT_READ; flags = MAP_SHARED; } else if (prot == sun_nio_ch_FileChannelImpl_MAP_PV) { protections = PROT_WRITE | PROT_READ; flags = MAP_PRIVATE; }
mapAddress = mmap64( 0, len, protections, flags, fd, off);
if (mapAddress == MAP_FAILED) { if (errno == ENOMEM) { JNU_ThrowOutOfMemoryError(env, "Map failed"); return IOS_THROWN; } return handle(env, -1, "Map failed"); }
return ((jlong) (unsigned long) mapAddress); }
|