Skip to content

Commit

Permalink
HADOOP-7194. Fix resource leak in IOUtils.copyBytes(..). Contributed …
Browse files Browse the repository at this point in the history
…by Devaraj K

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-0.21@1086311 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
szetszwo committed Mar 28, 2011
1 parent 8d8b8eb commit 3c91be0
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.txt
Expand Up @@ -51,6 +51,9 @@ Release 0.21.1 - Unreleased
HADOOP-7174. Null is displayed in the "fs -copyToLocal" command.
(Uma Maheswara Rao G via szetszwo)

HADOOP-7194. Fix resource leak in IOUtils.copyBytes(..).
(Devaraj K via szetszwo)

Release 0.21.0 - 2010-08-13

INCOMPATIBLE CHANGES
Expand Down
8 changes: 7 additions & 1 deletion src/java/org/apache/hadoop/io/IOUtils.java
Expand Up @@ -47,10 +47,16 @@ public static void copyBytes(InputStream in, OutputStream out, int buffSize, boo

try {
copyBytes(in, out, buffSize);
} finally {
if(close) {
out.close();
out = null;
in.close();
in = null;
}
} finally {
if(close) {
closeStream(out);
closeStream(in);
}
}
}
Expand Down
68 changes: 68 additions & 0 deletions src/test/core/org/apache/hadoop/io/TestIOUtils.java
@@ -0,0 +1,68 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.junit.Test;
import org.mockito.Mockito;

/**
* Test cases for IOUtils.java
*/
public class TestIOUtils {

@Test
public void testCopyBytesShouldCloseStreamsWhenCloseIsTrue() throws Exception {
InputStream inputStream = Mockito.mock(InputStream.class);
OutputStream outputStream = Mockito.mock(OutputStream.class);
Mockito.doReturn(-1).when(inputStream).read(new byte[1]);
IOUtils.copyBytes(inputStream, outputStream, 1, true);
Mockito.verify(inputStream, Mockito.atLeastOnce()).close();
Mockito.verify(outputStream, Mockito.atLeastOnce()).close();
}

@Test
public void testCopyBytesShouldCloseInputSteamWhenOutputStreamCloseThrowsException()
throws Exception {
InputStream inputStream = Mockito.mock(InputStream.class);
OutputStream outputStream = Mockito.mock(OutputStream.class);
Mockito.doReturn(-1).when(inputStream).read(new byte[1]);
Mockito.doThrow(new IOException()).when(outputStream).close();
try{
IOUtils.copyBytes(inputStream, outputStream, 1, true);
} catch (IOException e) {
}
Mockito.verify(inputStream, Mockito.atLeastOnce()).close();
Mockito.verify(outputStream, Mockito.atLeastOnce()).close();
}

@Test
public void testCopyBytesShouldNotCloseStreamsWhenCloseIsFalse()
throws Exception {
InputStream inputStream = Mockito.mock(InputStream.class);
OutputStream outputStream = Mockito.mock(OutputStream.class);
Mockito.doReturn(-1).when(inputStream).read(new byte[1]);
IOUtils.copyBytes(inputStream, outputStream, 1, false);
Mockito.verify(inputStream, Mockito.atMost(0)).close();
Mockito.verify(outputStream, Mockito.atMost(0)).close();
}
}

0 comments on commit 3c91be0

Please sign in to comment.