Skip to content

Commit

Permalink
optimize IoUtils and Add some unit test (alibaba#8398)
Browse files Browse the repository at this point in the history
* optimize IoUtils and Add some unit test

reformat codes

fix Too many unapproved license issue

fix Too many unapproved license issue

fix Too many unapproved license issue

fix Too many unapproved license issue

fix Too many unapproved license issue

* fix test case issue

* fix test case issue
  • Loading branch information
karsonto authored May 23, 2022
1 parent 750e4f7 commit 89a0f89
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 15 deletions.
23 changes: 8 additions & 15 deletions common/src/main/java/com/alibaba/nacos/common/utils/IoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.net.HttpURLConnection;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
Expand Down Expand Up @@ -64,10 +65,8 @@ public static byte[] tryDecompress(InputStream raw) throws IOException {
} catch (IOException e) {
e.printStackTrace();
} finally {
closeQuietly(out);
closeQuietly(gis);
closeQuietly(out, gis);
}

return null;
}

Expand All @@ -91,8 +90,7 @@ public static byte[] tryDecompress(byte[] raw) throws Exception {
IoUtils.copy(gis, out);
return out.toByteArray();
} finally {
closeQuietly(out);
closeQuietly(gis);
closeQuietly(out, gis);
}
}

Expand Down Expand Up @@ -325,8 +323,7 @@ public static void copyFile(String source, String target) throws IOException {
sc = new FileInputStream(sf).getChannel();
sc.transferTo(0, sc.size(), tc);
} finally {
closeQuietly(sc);
closeQuietly(tc);
closeQuietly(sc, tc);
}
}

Expand Down Expand Up @@ -360,14 +357,6 @@ public static void closeQuietly(HttpURLConnection connection) {
}
}

public static void closeQuietly(InputStream input) {
closeQuietly((Closeable) input);
}

public static void closeQuietly(OutputStream output) {
closeQuietly((Closeable) output);
}

/**
* Close closable object quietly.
*
Expand All @@ -381,5 +370,9 @@ public static void closeQuietly(Closeable closeable) {
} catch (IOException ignored) {
}
}

public static void closeQuietly(Closeable... closeable) {
Arrays.stream(closeable).forEach(IoUtils::closeQuietly);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.common.utils;

import org.apache.commons.io.Charsets;
import org.junit.Assert;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
* Unit test of IoUtils.
*
* @author karsonto
*/
public class IoUtilsTest {

@Test()
public void testCloseQuietly() throws IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(new ByteArrayInputStream("111".getBytes(Charsets.toCharset("UTF-8")))));
Assert.assertEquals("111", br.readLine());
IoUtils.closeQuietly(br);
try {
br.readLine();
} catch (IOException e) {
Assert.assertNotNull(e);
return;
}
Assert.fail();
}

@Test()
public void testCloseQuietly2() throws IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(new ByteArrayInputStream("123".getBytes(Charsets.toCharset("UTF-8")))));
Assert.assertEquals("123", br.readLine());
BufferedReader br2 = new BufferedReader(
new InputStreamReader(new ByteArrayInputStream("456".getBytes(Charsets.toCharset("UTF-8")))));
Assert.assertEquals("456", br2.readLine());
IoUtils.closeQuietly(br, br2);
try {
br.readLine();
} catch (IOException e) {
Assert.assertNotNull(e);
}
try {
br2.readLine();
} catch (IOException e) {
Assert.assertNotNull(e);
return;
}
Assert.fail();
}

}

0 comments on commit 89a0f89

Please sign in to comment.