From 704f91d07962716a3545dac95cc88d29e7982c9c Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Mon, 5 Dec 2011 12:12:11 +0100 Subject: [PATCH] Cleanup (a bit more reliable close()) --- .../util/io/ReadOnlyMappedBufferWrapper.java | 24 +++++++------ .../util/io/ReadWriteMappedBufferWrapper.java | 36 +++++++++---------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/platform/util/src/com/intellij/util/io/ReadOnlyMappedBufferWrapper.java b/platform/util/src/com/intellij/util/io/ReadOnlyMappedBufferWrapper.java index 9280bcaab5e4..f40f0834db99 100644 --- a/platform/util/src/com/intellij/util/io/ReadOnlyMappedBufferWrapper.java +++ b/platform/util/src/com/intellij/util/io/ReadOnlyMappedBufferWrapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2011 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,10 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -/* - * @author max - */ package com.intellij.util.io; import java.io.File; @@ -25,19 +21,27 @@ import java.io.IOException; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; +/** + * @author max + */ public class ReadOnlyMappedBufferWrapper extends MappedBufferWrapper { public ReadOnlyMappedBufferWrapper(final File file, final int pos) { super(file, pos, file.length() - pos); } - public MappedByteBuffer map() throws IOException { - FileInputStream stream = new FileInputStream(myFile); - FileChannel channel = stream.getChannel(); + @Override + protected MappedByteBuffer map() throws IOException { + final FileInputStream stream = new FileInputStream(myFile); try { - return channel.map(FileChannel.MapMode.READ_ONLY, myPosition, myLength); + final FileChannel channel = stream.getChannel(); + try { + return channel.map(FileChannel.MapMode.READ_ONLY, myPosition, myLength); + } + finally { + channel.close(); + } } finally { - channel.close(); stream.close(); } } diff --git a/platform/util/src/com/intellij/util/io/ReadWriteMappedBufferWrapper.java b/platform/util/src/com/intellij/util/io/ReadWriteMappedBufferWrapper.java index 68367ac0f112..359d3efae78e 100644 --- a/platform/util/src/com/intellij/util/io/ReadWriteMappedBufferWrapper.java +++ b/platform/util/src/com/intellij/util/io/ReadWriteMappedBufferWrapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2011 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,10 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -/* - * @author max - */ package com.intellij.util.io; import org.jetbrains.annotations.NonNls; @@ -27,28 +23,30 @@ import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; +/** + * @author max + */ public class ReadWriteMappedBufferWrapper extends MappedBufferWrapper { @NonNls private static final String RW = "rw"; - public ReadWriteMappedBufferWrapper(final File file, int offset, int len) { + public ReadWriteMappedBufferWrapper(final File file, final int offset, final int len) { super(file, offset, len); } - public MappedByteBuffer map() throws IOException { - RandomAccessFile raf = null; - FileChannel channel = null; + @Override + protected MappedByteBuffer map() throws IOException { + final RandomAccessFile file = new RandomAccessFile(myFile, RW); try { - raf = new RandomAccessFile(myFile, RW); - channel = raf.getChannel(); - return channel.map(FileChannel.MapMode.READ_WRITE, myPosition, myLength); - } - finally { - if (channel != null) { + final FileChannel channel = file.getChannel(); + try { + return channel.map(FileChannel.MapMode.READ_WRITE, myPosition, myLength); + } + finally { channel.close(); } - if (raf != null) { - raf.close(); - } + } + finally { + file.close(); } } -} \ No newline at end of file +}