提交 54bbc678 authored 作者: Thomas Mueller's avatar Thomas Mueller

Memory mapped files: There was a bug in version 1.2.139 so that memory mapped…

Memory mapped files: There was a bug in version 1.2.139 so that memory mapped files could only be used together with split
上级 67123266
...@@ -47,29 +47,29 @@ public class FileObjectDiskMapped implements FileObject { ...@@ -47,29 +47,29 @@ public class FileObjectDiskMapped implements FileObject {
} }
private void unMap() throws IOException { private void unMap() throws IOException {
if (mapped != null) { if (mapped == null) {
return;
}
// first write all data // first write all data
mapped.force(); mapped.force();
// need to dispose old direct buffer, see bug // need to dispose old direct buffer, see bug
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038
boolean useSystemGc; boolean useSystemGc = true;
if (SysProperties.NIO_CLEANER_HACK) { if (SysProperties.NIO_CLEANER_HACK) {
try { try {
useSystemGc = false;
Method cleanerMethod = mapped.getClass().getMethod("cleaner"); Method cleanerMethod = mapped.getClass().getMethod("cleaner");
cleanerMethod.setAccessible(true); cleanerMethod.setAccessible(true);
Object cleaner = cleanerMethod.invoke(mapped); Object cleaner = cleanerMethod.invoke(mapped);
Method clearMethod = cleaner.getClass().getMethod("clear"); Method clearMethod = cleaner.getClass().getMethod("clear");
clearMethod.invoke(cleaner); clearMethod.invoke(cleaner);
useSystemGc = false;
} catch (Throwable e) { } catch (Throwable e) {
useSystemGc = true; // useSystemGc is already true
} finally { } finally {
mapped = null; mapped = null;
} }
} else {
useSystemGc = true;
} }
if (useSystemGc) { if (useSystemGc) {
WeakReference<MappedByteBuffer> bufferWeakRef = new WeakReference<MappedByteBuffer>(mapped); WeakReference<MappedByteBuffer> bufferWeakRef = new WeakReference<MappedByteBuffer>(mapped);
...@@ -85,7 +85,6 @@ public class FileObjectDiskMapped implements FileObject { ...@@ -85,7 +85,6 @@ public class FileObjectDiskMapped implements FileObject {
} }
} }
} }
}
/** /**
* Re-map byte buffer into memory, called when file size has changed or file * Re-map byte buffer into memory, called when file size has changed or file
...@@ -95,7 +94,6 @@ public class FileObjectDiskMapped implements FileObject { ...@@ -95,7 +94,6 @@ public class FileObjectDiskMapped implements FileObject {
int oldPos = 0; int oldPos = 0;
if (mapped != null) { if (mapped != null) {
oldPos = pos; oldPos = pos;
mapped.force();
unMap(); unMap();
} }
long length = file.length(); long length = file.length();
...@@ -141,6 +139,7 @@ public class FileObjectDiskMapped implements FileObject { ...@@ -141,6 +139,7 @@ public class FileObjectDiskMapped implements FileObject {
try { try {
mapped.position(pos); mapped.position(pos);
mapped.get(b, off, len); mapped.get(b, off, len);
pos += len;
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
EOFException e2 = new EOFException("EOF"); EOFException e2 = new EOFException("EOF");
e2.initCause(e); e2.initCause(e);
...@@ -159,8 +158,11 @@ public class FileObjectDiskMapped implements FileObject { ...@@ -159,8 +158,11 @@ public class FileObjectDiskMapped implements FileObject {
public void setFileLength(long newLength) throws IOException { public void setFileLength(long newLength) throws IOException {
checkFileSizeLimit(newLength); checkFileSizeLimit(newLength);
int oldPos = pos;
unMap();
IOUtils.setLength(file, newLength); IOUtils.setLength(file, newLength);
reMap(); reMap();
pos = (int) Math.min(newLength, oldPos);
} }
public void sync() throws IOException { public void sync() throws IOException {
...@@ -175,6 +177,7 @@ public class FileObjectDiskMapped implements FileObject { ...@@ -175,6 +177,7 @@ public class FileObjectDiskMapped implements FileObject {
} }
mapped.position(pos); mapped.position(pos);
mapped.put(b, off, len); mapped.put(b, off, len);
pos += len;
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论