Skip to content

Commit

Permalink
cleanup:use standard methods instead of alternatives from third party…
Browse files Browse the repository at this point in the history
… library
  • Loading branch information
aleksandy committed May 7, 2024
1 parent 0cfb8d5 commit 5ec27be
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 181 deletions.
27 changes: 4 additions & 23 deletions framework/src/play/classloading/ApplicationClassloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableMap;
import static org.apache.commons.io.IOUtils.closeQuietly;

import java.io.File;
import java.io.IOException;
Expand All @@ -22,14 +21,10 @@
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import play.Logger;
import play.Play;
import play.cache.Cache;
Expand Down Expand Up @@ -221,12 +216,10 @@ byte[] getClassDefinition(String name) {
if (is == null) {
return null;
}
try {
return IOUtils.toByteArray(is);
try (is) {
return is.readAllBytes();
} catch (Exception e) {
throw new UnexpectedException(e);
} finally {
closeQuietly(is);
}
}

Expand All @@ -238,7 +231,6 @@ public InputStream getResourceAsStream(String name) {
return res.inputstream();
}
}
URL url = getResource(name);

return super.getResourceAsStream(name);
}
Expand Down Expand Up @@ -293,19 +285,8 @@ public Enumeration<URL> getResources(String name) throws IOException {
urls.add(next);
}
}
final Iterator<URL> it = urls.iterator();
return new Enumeration<URL>() {

@Override
public boolean hasMoreElements() {
return it.hasNext();
}

@Override
public URL nextElement() {
return it.next();
}
};
return Collections.enumeration(urls);
}

/**
Expand Down Expand Up @@ -452,7 +433,7 @@ public List<Class<?>> getAllClasses() {
for (ApplicationClass clazz : Play.classes.all()) {
byNormalizedName.put(clazz.name.toLowerCase(), clazz);
if (clazz.name.contains("$")) {
byNormalizedName.put(StringUtils.replace(clazz.name.toLowerCase(), "$", "."), clazz);
byNormalizedName.put(clazz.name.toLowerCase().replace('$', '.'), clazz);
}
}

Expand Down
12 changes: 5 additions & 7 deletions framework/src/play/libs/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import play.Logger;
import play.exceptions.UnexpectedException;

Expand Down Expand Up @@ -74,7 +72,7 @@ public static void copy(File from, File to) {
}

try {
FileUtils.copyFile(from, to);
java.nio.file.Files.copy(from.toPath(), to.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -118,9 +116,9 @@ public static boolean deleteDirectory(File path) {

public static boolean copyDir(File from, File to) {
try {
FileUtils.copyDirectory(from, to);
IO.copyDirectory(from, to);
return true;
} catch (IOException e) {
} catch (Exception e) {
return false;
}
}
Expand Down Expand Up @@ -221,7 +219,7 @@ static void zipDirectory(File root, File directory, ZipOutputStream zos) throws
String path = item.getAbsolutePath().substring(root.getAbsolutePath().length() + 1);
ZipEntry anEntry = new ZipEntry(path);
zos.putNextEntry(anEntry);
IOUtils.copyLarge(fis, zos);
fis.transferTo(zos);
}
}
}
Expand Down
57 changes: 20 additions & 37 deletions framework/src/play/libs/IO.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.apache.commons.io.Charsets.toCharset;
import static org.apache.commons.io.IOUtils.closeQuietly;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.nio.channels.Channels;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.io.IOUtils;

import play.exceptions.UnexpectedException;
import play.utils.OrderSafeProperties;

Expand All @@ -37,13 +37,11 @@ public class IO {
*/
public static Properties readUtf8Properties(InputStream is) {
Properties properties = new OrderSafeProperties();
try {
try (is) {
properties.load(is);
return properties;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
closeQuietly(is);
}
}

Expand All @@ -69,7 +67,7 @@ public static String readContentAsString(InputStream is) {
*/
public static String readContentAsString(InputStream is, String encoding) {
try {
return IOUtils.toString(is, encoding);
return new String(is.readAllBytes(), toCharset(encoding));
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -108,11 +106,7 @@ public static String readContentAsString(File file, String encoding) {
}

public static List<String> readLines(InputStream is) {
try {
return IOUtils.readLines(is, defaultCharset());
} catch (UncheckedIOException ex) {
throw new UnexpectedException(ex);
}
return new BufferedReader(new InputStreamReader(is, defaultCharset())).lines().collect(Collectors.toList());
}

public static List<String> readLines(File file, String encoding) {
Expand Down Expand Up @@ -155,7 +149,7 @@ public static byte[] readContent(File file) {
*/
public static byte[] readContent(InputStream is) {
try {
return IOUtils.toByteArray(is);
return is.readAllBytes();
} catch (IOException e) {
throw new UnexpectedException(e);
}
Expand All @@ -170,12 +164,12 @@ public static byte[] readContent(InputStream is) {
* The stream to write
*/
public static void writeContent(CharSequence content, OutputStream os) {
try {
IOUtils.write(content, os, UTF_8);
try (os) {
if (content != null) {
Channels.newChannel(os).write(UTF_8.encode(content.toString()));
}
} catch (IOException e) {
throw new UnexpectedException(e);
} finally {
closeQuietly(os);
}
}

Expand All @@ -190,12 +184,12 @@ public static void writeContent(CharSequence content, OutputStream os) {
* Encoding to used
*/
public static void writeContent(CharSequence content, OutputStream os, String encoding) {
try {
IOUtils.write(content, os, encoding);
try (os) {
if (content != null) {
Channels.newChannel(os).write(toCharset(encoding).encode(content.toString()));
}
} catch (IOException e) {
throw new UnexpectedException(e);
} finally {
closeQuietly(os);
}
}

Expand Down Expand Up @@ -258,12 +252,10 @@ public static void write(byte[] data, File file) {
* The destination stream
*/
public static void copy(InputStream is, OutputStream os) {
try {
try (is) {
is.transferTo(os);
} catch (IOException e) {
throw new UnexpectedException(e);
} finally {
closeQuietly(is);
}
}

Expand All @@ -276,13 +268,10 @@ public static void copy(InputStream is, OutputStream os) {
* The destination stream
*/
public static void write(InputStream is, OutputStream os) {
try {
try (is; os) {
is.transferTo(os);
} catch (IOException e) {
throw new UnexpectedException(e);
} finally {
closeQuietly(is);
closeQuietly(os);
}
}

Expand All @@ -295,14 +284,8 @@ public static void write(InputStream is, OutputStream os) {
* The destination file
*/
public static void write(InputStream is, File f) {
try {
OutputStream os = new FileOutputStream(f);
try {
is.transferTo(os);
} finally {
closeQuietly(is);
closeQuietly(os);
}
try (is; var os = new FileOutputStream(f)) {
is.transferTo(os);
} catch (IOException e) {
throw new UnexpectedException(e);
}
Expand Down
15 changes: 4 additions & 11 deletions framework/src/play/mvc/results/RenderBinary.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package play.mvc.results;

import static org.apache.commons.io.IOUtils.closeQuietly;
import static java.nio.charset.StandardCharsets.US_ASCII;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.StandardCharsets;

import org.apache.commons.codec.net.URLCodec;
import org.apache.commons.io.IOUtils;

import play.exceptions.UnexpectedException;
import play.libs.MimeTypes;
Expand Down Expand Up @@ -204,16 +200,13 @@ private String dispositionType() {
}

private static void copyInputStreamAndClose(InputStream is, OutputStream out) throws IOException {
try {
IOUtils.copyLarge(is, out);
} finally {
closeQuietly(is);
try (is) {
is.transferTo(out);
}
}

private boolean canAsciiEncode(String string) {
CharsetEncoder asciiEncoder = StandardCharsets.US_ASCII.newEncoder();
return asciiEncoder.canEncode(string);
return US_ASCII.newEncoder().canEncode(string);
}

public boolean isInline() {
Expand Down
5 changes: 1 addition & 4 deletions framework/src/play/server/FileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@
import java.util.Arrays;
import java.util.Comparator;

import static org.apache.commons.io.IOUtils.closeQuietly;
import static org.jboss.netty.buffer.ChannelBuffers.wrappedBuffer;
import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;

public class FileService {

public static void serve(File localFile, HttpRequest nettyRequest, HttpResponse nettyResponse, ChannelHandlerContext ctx, Request request, Response response, Channel channel) throws FileNotFoundException {
RandomAccessFile raf = new RandomAccessFile(localFile, "r");
try {
try (RandomAccessFile raf = new RandomAccessFile(localFile, "r")) {
long fileLength = raf.length();

boolean isKeepAlive = HttpHeaders.isKeepAlive(nettyRequest) && nettyRequest.getProtocolVersion().equals(HttpVersion.HTTP_1_1);
Expand Down Expand Up @@ -82,7 +80,6 @@ public static void serve(File localFile, HttpRequest nettyRequest, HttpResponse
}
} catch (Throwable exx) {
exx.printStackTrace();
closeQuietly(raf);
try {
if (ctx.getChannel().isOpen()) {
ctx.getChannel().close();
Expand Down
6 changes: 1 addition & 5 deletions framework/src/play/server/PlayHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package play.server;

import org.apache.commons.io.IOUtils;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBufferInputStream;
import org.jboss.netty.buffer.ChannelBuffers;
Expand Down Expand Up @@ -568,10 +567,7 @@ public Request parseRequest(ChannelHandlerContext ctx, HttpRequest nettyRequest,
}

} else {
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(new ChannelBufferInputStream(b), out);
byte[] n = out.toByteArray();
body = new ByteArrayInputStream(n);
body = new ByteArrayInputStream(new ChannelBufferInputStream(b).readAllBytes());
}

String host = nettyRequest.headers().get(HOST);
Expand Down
Loading

0 comments on commit 5ec27be

Please sign in to comment.