Repository: maven
Updated Branches:
  refs/heads/master fde3b28fb -> 7cd7bd864


[MNG-5629] ClosedChannelException from DefaultUpdateCheckManager.read

o Updated to stop producing 'ClosedChannelException's when reading tracking 
files.
o Updated to use 'Long.MAX_VALUE' as the size of any locked regions to prevent 
writing beyond locked regions.


Project: http://git-wip-us.apache.org/repos/asf/maven/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven/commit/7cd7bd86
Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/7cd7bd86
Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/7cd7bd86

Branch: refs/heads/master
Commit: 7cd7bd8648dd4755f4ef1b09e031f03d8bdc59b2
Parents: fde3b28
Author: Christian Schulte <schu...@apache.org>
Authored: Fri Dec 11 21:42:09 2015 +0100
Committer: Christian Schulte <schu...@apache.org>
Committed: Fri Dec 11 21:48:29 2015 +0100

----------------------------------------------------------------------
 .../legacy/DefaultUpdateCheckManager.java       | 61 ++++++++------------
 1 file changed, 24 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven/blob/7cd7bd86/maven-compat/src/main/java/org/apache/maven/repository/legacy/DefaultUpdateCheckManager.java
----------------------------------------------------------------------
diff --git 
a/maven-compat/src/main/java/org/apache/maven/repository/legacy/DefaultUpdateCheckManager.java
 
b/maven-compat/src/main/java/org/apache/maven/repository/legacy/DefaultUpdateCheckManager.java
index dfc463b..2c26f71 100644
--- 
a/maven-compat/src/main/java/org/apache/maven/repository/legacy/DefaultUpdateCheckManager.java
+++ 
b/maven-compat/src/main/java/org/apache/maven/repository/legacy/DefaultUpdateCheckManager.java
@@ -28,15 +28,12 @@ import org.apache.maven.repository.Proxy;
 import org.codehaus.plexus.component.annotations.Component;
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 import org.codehaus.plexus.logging.Logger;
-import org.codehaus.plexus.util.IOUtil;
 
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.RandomAccessFile;
-import java.nio.ByteBuffer;
+import java.nio.channels.Channels;
 import java.nio.channels.FileChannel;
 import java.nio.channels.FileLock;
 import java.util.Date;
@@ -242,18 +239,12 @@ public class DefaultUpdateCheckManager
                 Properties props = new Properties();
 
                 channel = new RandomAccessFile( touchfile, "rw" ).getChannel();
-                lock = channel.lock( 0, channel.size(), false );
+                lock = channel.lock();
 
                 if ( touchfile.canRead() )
                 {
                     getLogger().debug( "Reading resolution-state from: " + 
touchfile );
-                    ByteBuffer buffer = ByteBuffer.allocate( (int) 
channel.size() );
-
-                    channel.read( buffer );
-                    buffer.flip();
-
-                    ByteArrayInputStream stream = new ByteArrayInputStream( 
buffer.array() );
-                    props.load( stream );
+                    props.load( Channels.newInputStream( channel ) );
                 }
 
                 props.setProperty( key, Long.toString( 
System.currentTimeMillis() ) );
@@ -267,18 +258,15 @@ public class DefaultUpdateCheckManager
                     props.remove( key + ERROR_KEY_SUFFIX );
                 }
 
-                ByteArrayOutputStream stream = new ByteArrayOutputStream();
-
                 getLogger().debug( "Writing resolution-state to: " + touchfile 
);
-                props.store( stream, "Last modified on: " + new Date() );
+                channel.position( 0 );
+                props.store( Channels.newOutputStream( channel ), "Last 
modified on: " + new Date() );
 
-                byte[] data = stream.toByteArray();
-                ByteBuffer buffer = ByteBuffer.allocate( data.length );
-                buffer.put( data );
-                buffer.flip();
+                lock.release();
+                lock = null;
 
-                channel.position( 0 );
-                channel.write( buffer );
+                channel.close();
+                channel = null;
             }
             catch ( IOException e )
             {
@@ -359,27 +347,26 @@ public class DefaultUpdateCheckManager
 
         synchronized ( touchfile.getAbsolutePath().intern() )
         {
+            FileInputStream in = null;
             FileLock lock = null;
-            FileChannel channel = null;
+
             try
             {
                 Properties props = new Properties();
 
-                FileInputStream stream = new FileInputStream( touchfile );
-                try
-                {
-                    channel = stream.getChannel();
-                    lock = channel.lock( 0, channel.size(), true );
+                in = new FileInputStream( touchfile );
+                lock = in.getChannel().lock( 0, Long.MAX_VALUE, true );
 
-                    getLogger().debug( "Reading resolution-state from: " + 
touchfile );
-                    props.load( stream );
+                getLogger().debug( "Reading resolution-state from: " + 
touchfile );
+                props.load( in );
 
-                    return props;
-                }
-                finally
-                {
-                    IOUtil.close( stream );
-                }
+                lock.release();
+                lock = null;
+
+                in.close();
+                in = null;
+
+                return props;
             }
             catch ( IOException e )
             {
@@ -402,11 +389,11 @@ public class DefaultUpdateCheckManager
                     }
                 }
 
-                if ( channel != null )
+                if ( in != null )
                 {
                     try
                     {
-                        channel.close();
+                        in.close();
                     }
                     catch ( IOException e )
                     {

Reply via email to