Skip to content

Commit

Permalink
Fix deprecation warnings from commons-io and other compiler warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
centic9 committed Jan 26, 2017
1 parent dff6e65 commit 178baa4
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public String simpleGet(String url) throws IOException {
@Override
public void accept(InputStream inputStream) {
try {
str.set(IOUtils.toString(inputStream));
str.set(IOUtils.toString(inputStream, "UTF-8"));
} catch (IOException e) {
throw new IllegalStateException(e);
}
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/org/dstadler/commons/zip/ZipUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ public static void findZip(String zipName, InputStream zipInput, FileFilter sear
final ZipEntry en;
try {
en = zin.getNextEntry();
} catch (IOException e) {
throw new IOException("While handling file " + zipName, e);
} catch (IllegalArgumentException e) {
} catch (IOException | IllegalArgumentException e) {
throw new IOException("While handling file " + zipName, e);
}
if(en == null) {
Expand Down Expand Up @@ -266,7 +264,7 @@ public static String getZipStringContentsRecursive(final String file) throws IOE
try {
try (InputStream str = new FileInputStream(file)) {
if (str.available() > 0) {
return IOUtils.toString(str);
return IOUtils.toString(str, "UTF-8");
}

return "";
Expand Down Expand Up @@ -318,7 +316,7 @@ public static String getZipStringContentsRecursive(final String file) throws IOE

try (InputStream str = zipfile.getInputStream(entry)) {
if (str.available() > 0) {
return IOUtils.toString(str);
return IOUtils.toString(str, "UTF-8");
}

return "";
Expand All @@ -337,7 +335,7 @@ public static String getZipStringContentsRecursive(final String file) throws IOE
*
* @throws IOException Thrown if files can not be read or any other error occurs while handling the Zip-files
*/
public static final void extractZip(File zip, File toDir) throws IOException{
public static void extractZip(File zip, File toDir) throws IOException{
if(!toDir.exists()) {
throw new IOException("Directory '" + toDir + "' does not exist.");
}
Expand Down Expand Up @@ -389,7 +387,7 @@ public static final void extractZip(File zip, File toDir) throws IOException{
*
* @throws IOException Thrown if files can not be read or any other error occurs while handling the Zip-files
*/
public static final void extractZip(InputStream zip, final File toDir) throws IOException{
public static void extractZip(InputStream zip, final File toDir) throws IOException{
if(!toDir.exists()) {
throw new IOException("Directory '" + toDir + "' does not exist.");
}
Expand Down Expand Up @@ -492,7 +490,7 @@ public static void replaceInZip(File zip, String file, String data, String encod
if(!found) {
zos.putNextEntry(new ZipEntry(file));
try {
IOUtils.write(data, zos);
IOUtils.write(data, zos, "UTF-8");
} finally {
zos.closeEntry();
}
Expand All @@ -504,6 +502,7 @@ public static void replaceInZip(File zip, String file, String data, String encod
FileUtils.copyFile(zipOutFile, zip);
} finally {
if(!zipOutFile.delete()) {
//noinspection ThrowFromFinallyBlock
throw new IOException("Error deleting file: " + zipOutFile);
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/test/java/org/dstadler/commons/exec/ExecutionHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void testGetCommandResult() throws IOException {
log.info("Working dir: " + new File(".").getAbsolutePath());
try (InputStream result = ExecutionHelper.getCommandResult(cmdLine, new File("."), 0, 60000)) {
assertNotNull(result);
log.info("Svn-Update reported:\n" + IOUtils.toString(result));
log.info("Svn-Update reported:\n" + IOUtils.toString(result, "UTF-8"));
}
}

Expand All @@ -61,7 +61,7 @@ public void testGetCommandResultIgnoreExitValueStatus() throws IOException {

try (InputStream result = ExecutionHelper.getCommandResult(cmdLine, new File("."), -1, 60000)) {
assertNotNull(result);
log.info("Svn-Update reported:\n" + IOUtils.toString(result));
log.info("Svn-Update reported:\n" + IOUtils.toString(result, "UTF-8"));
}
}

Expand All @@ -72,7 +72,7 @@ public void testGetCommandResultIgnoreExitValueHelp() throws IOException {

try (InputStream result = ExecutionHelper.getCommandResult(cmdLine, new File("."), -1, 60000)) {
assertNotNull(result);
log.info("Svn-Update reported:\n" + IOUtils.toString(result));
log.info("Svn-Update reported:\n" + IOUtils.toString(result, "UTF-8"));
}
}

Expand All @@ -83,7 +83,7 @@ public void testGetCommandResultIgnoreExitValueWrongCmd() throws IOException {

try (InputStream result = ExecutionHelper.getCommandResult(cmdLine, new File("."), -1, 60000)) {
assertNotNull(result);
log.info("Svn-Update reported:\n" + IOUtils.toString(result));
log.info("Svn-Update reported:\n" + IOUtils.toString(result, "UTF-8"));
}
}

Expand All @@ -95,7 +95,7 @@ public void testGetCommandResultInputStream() throws IOException {
log.info("Working dir: " + new File(".").getAbsolutePath());
try (InputStream result = ExecutionHelper.getCommandResult(cmdLine, new File("."), 0, 60000, new ByteArrayInputStream(new byte[] {}))) {
assertNotNull(result);
log.info("Svn-Update reported:\n" + IOUtils.toString(result));
log.info("Svn-Update reported:\n" + IOUtils.toString(result, "UTF-8"));
}
}

Expand Down Expand Up @@ -125,7 +125,7 @@ public void testGetCommandResultIgnoreExitValueInputStream() throws IOException

try (InputStream result = ExecutionHelper.getCommandResult(cmdLine, new File("."), -1, 60000, new ByteArrayInputStream(new byte[] {}))) {
assertNotNull(result);
log.info("Svn-Update reported:\n" + IOUtils.toString(result));
log.info("Svn-Update reported:\n" + IOUtils.toString(result, "UTF-8"));
}
}

Expand All @@ -136,7 +136,7 @@ public void testGetCommandResultIgnoreExitValueWrongCmdInputStream() throws IOEx

try (InputStream result = ExecutionHelper.getCommandResult(cmdLine, new File("."), -1, 60000, new ByteArrayInputStream(new byte[] {}))) {
assertNotNull(result);
log.info("Svn-Update reported:\n" + IOUtils.toString(result));
log.info("Svn-Update reported:\n" + IOUtils.toString(result, "UTF-8"));
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/test/java/org/dstadler/commons/graphviz/DotUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void setUp() {
public void testRenderGraph() throws Exception {
File file = File.createTempFile("DotUtilsTest", ".dot");
try {
FileUtils.write(file, DOT_FILE);
FileUtils.write(file, DOT_FILE, "UTF-8");

File out = File.createTempFile("DotUtilsTest", ".png");
try {
Expand All @@ -77,7 +77,7 @@ public void testRenderGraph() throws Exception {
public void testRenderGraphInvalidFile() throws Exception {
File file = File.createTempFile("DotUtilsTest", ".dot");
try {
FileUtils.write(file, "{ digraph");
FileUtils.write(file, "{ digraph", "UTF-8");

try {
File out = File.createTempFile("DotUtilsTest", ".png");
Expand Down Expand Up @@ -135,7 +135,7 @@ public void testWriteHeaderContent() throws Exception {
try (FileWriter fileWriter = new FileWriter(temp, false)) {
DotUtils.writeHeader(fileWriter, 0, null, "G", null);
fileWriter.flush();
assertEquals(FileUtils.readFileToString(temp),
assertEquals(FileUtils.readFileToString(temp, "UTF-8"),
"digraph G {\n" +
"rankdir=LR;\n" +
"node [shape=box];\n\n");
Expand All @@ -145,7 +145,7 @@ public void testWriteHeaderContent() throws Exception {
try (FileWriter fileWriter = new FileWriter(temp, false)) {
DotUtils.writeHeader(fileWriter, 200, null, "G", null);
fileWriter.flush();
assertEquals(FileUtils.readFileToString(temp),
assertEquals(FileUtils.readFileToString(temp, "UTF-8"),
"digraph G {\n" +
"dpi=200;\n" +
"rankdir=LR;\n" +
Expand All @@ -156,7 +156,7 @@ public void testWriteHeaderContent() throws Exception {
try (FileWriter fileWriter = new FileWriter(temp, false)) {
DotUtils.writeHeader(fileWriter, 0, "AB", "G", null);
fileWriter.flush();
assertEquals(FileUtils.readFileToString(temp),
assertEquals(FileUtils.readFileToString(temp, "UTF-8"),
"digraph G {\n" +
"rankdir=AB;\n" +
"node [shape=box];\n\n");
Expand All @@ -170,19 +170,19 @@ public void testWriteHeaderContent() throws Exception {

DotUtils.writeHeader(fileWriter, 0, null, "G", lines);
fileWriter.flush();
assertEquals(FileUtils.readFileToString(temp),
assertEquals(FileUtils.readFileToString(temp, "UTF-8"),
"digraph G {\n" +
"rankdir=LR;\n" +
"someline;\n" +
"someotherline;\n" +
"node [shape=box];\n\n");
}

// different titlke
// different title
try (FileWriter fileWriter = new FileWriter(temp, false)) {
DotUtils.writeHeader(fileWriter, 0, null, "mygraph", null);
fileWriter.flush();
assertEquals(FileUtils.readFileToString(temp),
assertEquals(FileUtils.readFileToString(temp, "UTF-8"),
"digraph mygraph {\n" +
"rankdir=LR;\n" +
"node [shape=box];\n\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void testHttpClientWrapperSimpleGetStream() throws Exception {
@Override
public void accept(InputStream inputStream) {
try {
str.set(IOUtils.toString(inputStream));
str.set(IOUtils.toString(inputStream, "UTF-8"));
} catch (IOException e) {
throw new IllegalStateException(e);
}
Expand All @@ -70,7 +70,7 @@ public void testHttpClientWrapperNormalGet() throws Exception {
HttpEntity entity = response.getEntity();

try {
String string = IOUtils.toString(entity.getContent());
String string = IOUtils.toString(entity.getContent(), "UTF-8");
assertNotNull(string);
} finally {
// ensure all content is taken out to free resources
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/dstadler/commons/http/NanoHTTPDTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public void testServeTimeoutInitial() throws Exception {
// wait some time to trigger the timeout
Thread.sleep(2000);

assertTrue(IOUtils.toString(socket.getInputStream()).startsWith("HTTP/1.0 500 Internal Server Error"));
assertTrue(IOUtils.toString(socket.getInputStream(), "UTF-8").startsWith("HTTP/1.0 500 Internal Server Error"));
}

httpd.stop();
Expand All @@ -227,7 +227,7 @@ public void testServeTimeoutStarted() throws Exception {
// wait some time to trigger the timeout
Thread.sleep(2000);

assertTrue(IOUtils.toString(socket.getInputStream()).startsWith("HTTP/1.0 500 Internal Server Error"));
assertTrue(IOUtils.toString(socket.getInputStream(), "UTF-8").startsWith("HTTP/1.0 500 Internal Server Error"));
}

httpd.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class DeleteOnCloseInputStreamTest {
public void testDeleteOnCloseInputStream() throws Exception {
File file = File.createTempFile("deleteOnClose1_", ".test");
FileUtils.writeStringToFile(file,
"somedata to have something to read...");
"somedata to have something to read...", "UTF-8");
try (InputStream input = new FileInputStream(file)) {
assertTrue(file.exists());
assertTrue(file.length() > 0);
Expand All @@ -33,7 +33,7 @@ public void testDeleteOnCloseInputStream() throws Exception {
assertTrue(file.exists());
assertTrue(file.length() > 0);

List<String> lines = IOUtils.readLines(stream);
List<String> lines = IOUtils.readLines(stream, "UTF-8");
assertEquals(1, lines.size());

assertTrue(file.exists());
Expand All @@ -49,7 +49,7 @@ public void testDeleteOnCloseInputStream() throws Exception {
public void testCoverMethods() throws Exception {
File file = File.createTempFile("deleteOnClose2_", ".test");
FileUtils.writeStringToFile(file,
"somedata to have something to read...");
"somedata to have something to read...", "UTF-8");
try (InputStream input = new FileInputStream(file)) {
try (DeleteOnCloseInputStream stream = new DeleteOnCloseInputStream(input,
file)) {
Expand All @@ -71,7 +71,7 @@ public void testCoverMethods() throws Exception {

stream.mark(2);

stream.skip(1);
assertEquals(1, stream.skip(1));
try {
stream.reset();
fail("Mark/Reset not supported");
Expand All @@ -90,7 +90,7 @@ public void testCoverMethods() throws Exception {
public void testReset() throws Exception {
File file = File.createTempFile("deleteOnClose3_", ".test");
FileUtils.writeStringToFile(file,
"somedata to have something to read...");
"somedata to have something to read...", "UTF-8");
try (InputStream input = new FileInputStream(file) {
@Override
public synchronized void reset() throws IOException {
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/org/dstadler/commons/io/TailInputStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class TailInputStreamTest {
public void testTailInputStream() throws Exception {
File file = File.createTempFile("TailInputStream", ".test");
try {
FileUtils.write(file, StringUtils.repeat("1234567890\n", 100));
FileUtils.write(file, StringUtils.repeat("1234567890\n", 100), "UTF-8");

assertEquals(1100, file.length());

Expand All @@ -42,7 +42,7 @@ public void testTailInputStream() throws Exception {
public void testTailInputStreamRN() throws Exception {
File file = File.createTempFile("TailInputStream", ".test");
try {
FileUtils.write(file, StringUtils.repeat("1234567890\r\n", 100));
FileUtils.write(file, StringUtils.repeat("1234567890\r\n", 100), "UTF-8");

assertEquals(1200, file.length());

Expand All @@ -64,7 +64,7 @@ public void testTailInputStreamRN() throws Exception {
public void testTailInputStreamR() throws Exception {
File file = File.createTempFile("TailInputStream", ".test");
try {
FileUtils.write(file, StringUtils.repeat("1234567890\r", 100));
FileUtils.write(file, StringUtils.repeat("1234567890\r", 100), "UTF-8");

assertEquals(1100, file.length());

Expand All @@ -86,7 +86,7 @@ public void testTailInputStreamR() throws Exception {
public void testTailInputStreamRNOnly() throws Exception {
File file = File.createTempFile("TailInputStream", ".test");
try {
FileUtils.write(file, StringUtils.repeat("\r\n", 100));
FileUtils.write(file, StringUtils.repeat("\r\n", 100), "UTF-8");

assertEquals(200, file.length());

Expand All @@ -107,7 +107,7 @@ public void testTailInputStreamRNOnly() throws Exception {
public void testTailInputStreamRead() throws Exception {
File file = File.createTempFile("TailInputStream", ".test");
try {
FileUtils.write(file, StringUtils.repeat("1234567890\r", 100));
FileUtils.write(file, StringUtils.repeat("1234567890\r", 100), "UTF-8");

assertEquals(1100, file.length());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private void runWithSpecialName(DocumentStarter starter, String str) throws IOEx
public void testStartURLWithBlanks() throws IOException {
File file = File.createTempFile("DocumentStarterTest-some file with blanks", ".txt");
try {
FileUtils.writeStringToFile(file, "test");
FileUtils.writeStringToFile(file, "test", "UTF-8");
new DocumentStarter().openURL(file.getAbsolutePath());
} finally {
assertTrue(file.delete());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void testParseContentURL() throws Exception {
AbstractSimpleContentHandler< String, String> handler = new AbstractSimpleContentHandler<String, String>() {
};

try (MockRESTServer server = new MockRESTServer("200", "text/xml", FileUtils.readFileToString(new File("src/test/data/svnlog.xml")))) {
try (MockRESTServer server = new MockRESTServer("200", "text/xml", FileUtils.readFileToString(new File("src/test/data/svnlog.xml"), "UTF-8"))) {
handler.parseContent(new URL("http://localhost:" + server.getPort()), "", null, 10_000);
}
}
Expand All @@ -49,7 +49,7 @@ public SortedMap<String, String> parseContent(InputStream strm) throws SAXExcept
};

try {
try (MockRESTServer server = new MockRESTServer("200", "text/xml", FileUtils.readFileToString(new File("src/test/data/svnlog.xml")))) {
try (MockRESTServer server = new MockRESTServer("200", "text/xml", FileUtils.readFileToString(new File("src/test/data/svnlog.xml"), "UTF-8"))) {
handler.parseContent(new URL("http://localhost:" + server.getPort()), "", null, 10_000);
}
fail("Should catch exception here");
Expand Down Expand Up @@ -94,7 +94,7 @@ public void testParseContentNotFound() throws Exception {
AbstractSimpleContentHandler< String, String> handler = new AbstractSimpleContentHandler<String, String>() {
};

try (MockRESTServer server = new MockRESTServer("404", "text/xml", FileUtils.readFileToString(new File("src/test/data/svnlog.xml")))) {
try (MockRESTServer server = new MockRESTServer("404", "text/xml", FileUtils.readFileToString(new File("src/test/data/svnlog.xml"), "UTF-8"))) {
try {
handler.parseContent(new URL("http://localhost:" + server.getPort() + "/notfound"), "", null, 10_000);
fail("Should catch exception");
Expand Down
Loading

0 comments on commit 178baa4

Please sign in to comment.