Skip to content

Commit

Permalink
Removing/replacing all deprecated calls to IOUtils.closeQuietly
Browse files Browse the repository at this point in the history
  • Loading branch information
aaime committed Jun 1, 2019
1 parent fca7274 commit e357e37
Show file tree
Hide file tree
Showing 50 changed files with 220 additions and 444 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,8 @@ public void afterPropertiesSet() throws Exception {
JMSConfiguration.getConfigPathDir());
if (properties.getType() != Type.RESOURCE) {
// copy the defaults
InputStream inputStream = null;
try {
inputStream = defaults.getInputStream();
try (InputStream inputStream = defaults.getInputStream()) {
IOUtils.copy(inputStream, properties.out());
} finally {
if (inputStream != null) {
org.apache.commons.io.IOUtils.closeQuietly(inputStream);
}
}
}
super.setLocation(new SpringResourceAdaptor(properties));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,12 @@ public static void initHibernate() throws Exception {
p.put("driver", "org.h2.Driver");
p.put("url", "jdbc:h2:mem:monitoring");
File file = new File("./target/monitoring/db.properties");
FileOutputStream fos = null;
try {
if (!file.getParentFile().exists()) {
assertTrue(file.getParentFile().mkdirs());
}
fos = new FileOutputStream(file);

if (!file.getParentFile().exists()) {
assertTrue(file.getParentFile().mkdirs());
}
try (FileOutputStream fos = new FileOutputStream(file)) {
p.store(fos, null);
} finally {
IOUtils.closeQuietly(fos);
}

ctx =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,9 @@ private void testExpression(

// byte[] bytes = FileUtils.readFileToByteArray(URLs.urlToFile(zip));

InputStream is = null;
byte[] bytes;
try {
is = zip.openStream();
try (InputStream is = zip.openStream()) {
bytes = IOUtils.toByteArray(is);
} finally {
IOUtils.closeQuietly(is);
}

// creation of the workspace if not already present
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,14 @@ public Long handleUpload(String uploadId, Representation entity, Long startPosit
ResumableUploadResource resource = getResource(uploadId);
Long writtenBytes = 0L;
try {
final ReadableByteChannel source = entity.getChannel();
RandomAccessFile raf = null;
FileChannel outputChannel = null;
try {
raf = new RandomAccessFile(resource.getFile(), "rw");
outputChannel = raf.getChannel();


try (final ReadableByteChannel source = entity.getChannel(); RandomAccessFile raf = new RandomAccessFile(resource.getFile(), "rw"); FileChannel outputChannel = raf.getChannel() ) {
writtenBytes =
IOUtils.copyToFileChannel(256 * 1024, source, outputChannel, startPosition);
} finally {
try {
if (raf != null) {
raf.close();
}
} finally {
IOUtils.closeQuietly(source);
IOUtils.closeQuietly(outputChannel);
}
}
} catch (IOException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
} finally {

}

return resource.getFile().length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,9 @@ public void doPut(HttpServletRequest request, HttpServletResponse response) {

// copy over the contents
try {
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(script.out()));

try {
try (BufferedWriter w = new BufferedWriter(new OutputStreamWriter(script.out()))) {
IOUtils.copy(request.getInputStream(), w);
w.flush();
} finally {
IOUtils.closeQuietly(w);
}
} catch (IOException e) {
throw new RestException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,14 @@ private Resource findFile(String name, String type, String extension) {
}

private String readFile(Resource file) {
InputStream in = file.in();
try {

try (InputStream in = file.in()) {
String s = IOUtils.toString(in);
return s;
} catch (IOException ex) {
LOGGER.warning(
String.format(
"Error reading file '%s' because ", file.path(), ex.getMessage()));
} finally {
IOUtils.closeQuietly(in);
}
return "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,10 @@ protected void onSubmit() {

private void saveScript() {
Script script = (Script) scriptModel.getObject();
OutputStream out = script.getResource().out();
try {
try (OutputStream out = script.getResource().out()){
IOUtils.write(script.getContents(), out);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(out);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,10 @@ private String getModeFromExtension(String ext) {

private void save() {
Script s = (Script) form.getModelObject();
OutputStream out = s.getResource().out();
try {
try (OutputStream out = s.getResource().out()) {
IOUtils.write(s.getContents(), out);
} catch (IOException e) {
LOGGER.log(Level.WARNING, e.getMessage(), e);
} finally {
IOUtils.closeQuietly(out);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
*/
package org.geoserver.wps.gs.download;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
import org.geoserver.catalog.FeatureTypeInfo;
import org.geoserver.platform.resource.Resource;
import org.geoserver.wps.ppio.ComplexPPIO;
Expand All @@ -35,6 +29,12 @@
import org.opengis.util.ProgressListener;
import org.springframework.context.ApplicationContext;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* The class that does the real work of checking if we are exceeeding the download limits for vector
* data. Also this class writes the features in the output file.
Expand Down Expand Up @@ -273,30 +273,7 @@ private Resource writeVectorOutput(
final Resource output = resourceManager.getTemporaryResource(extension);

// write checking limits
OutputStream os = null;
try {

// If limits are configured we must create an OutputStream that checks limits
final BufferedOutputStream bufferedOutputStream =
new BufferedOutputStream(output.out());
if (limit > DownloadServiceConfiguration.NO_LIMIT) {
os =
new LimitedOutputStream(bufferedOutputStream, limit) {

@Override
protected void raiseError(long pSizeMax, long pCount)
throws IOException {
IOException ioe =
new IOException(
"Download Exceeded the maximum HARD allowed size!");
throw ioe;
}
};

} else {
os = bufferedOutputStream;
}

try (OutputStream os = getResourceOutputStream(output, limit)) {
// write with PPIO
if (ppio_ instanceof ComplexPPIO) {
((ComplexPPIO) ppio_).encode(features, os);
Expand All @@ -305,13 +282,33 @@ protected void raiseError(long pSizeMax, long pCount)
LOGGER.log(Level.FINE, "Flushing stream");
}
os.flush();
} finally {
if (os != null) {
IOUtils.closeQuietly(os);
}
}

// return
return output;
}

private OutputStream getResourceOutputStream(Resource output, long limit) {
OutputStream os = null;
// If limits are configured we must create an OutputStream that checks limits
final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(output.out());
if (limit > DownloadServiceConfiguration.NO_LIMIT) {
os =
new LimitedOutputStream(bufferedOutputStream, limit) {

@Override
protected void raiseError(long pSizeMax, long pCount) throws IOException {
IOException ioe =
new IOException(
"Download Exceeded the maximum HARD allowed size!");
throw ioe;
}
};

} else {
os = bufferedOutputStream;
}

return os;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,22 +185,16 @@ public static boolean isZpFile(File file) {
return false;
}
// Check on the first Integer
DataInputStream in = null;
try {
in = new DataInputStream(new FileInputStream(file));


try (DataInputStream in = new DataInputStream(new FileInputStream(file))) {
int test = in.readInt();
return test == 0x504b0304;
} catch (IOException e) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
return false;
} finally {
if (in != null) {
org.apache.commons.io.IOUtils.closeQuietly(in);
}
}
}
}

/**
Expand All @@ -227,19 +221,12 @@ private static void zipFileInternal(File file, ZipOutputStream zipout, byte[] bu
zipout.putNextEntry(entry);

// copy over the file
InputStream in = null;
try {
try (InputStream in = new FileInputStream(file)) {
int c;
in = new FileInputStream(file);
while (-1 != (c = in.read(buffer))) {
zipout.write(buffer, 0, c);
}
zipout.closeEntry();
} finally {
// close the input stream
if (in != null) {
org.apache.commons.io.IOUtils.closeQuietly(in);
}
}
zipout.flush();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ public class DownloadProcessTest extends WPSTestSupport {
public static File decode(InputStream input, File tempDirectory) throws Exception {

// unzip to the temporary directory
ZipInputStream zis = null;
try {
zis = new ZipInputStream(input);
try (ZipInputStream zis = new ZipInputStream(input)) {
ZipEntry entry = null;

// Copy the whole file in the new position
Expand All @@ -156,25 +154,16 @@ public static File decode(InputStream input, File tempDirectory) throws Exceptio
int count;
byte data[] = new byte[4096];
// write the files to the disk
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);

try (FileOutputStream fos = new FileOutputStream(file)) {
while ((count = zis.read(data)) != -1) {
fos.write(data, 0, count);
}
fos.flush();
} finally {
if (fos != null) {
org.apache.commons.io.IOUtils.closeQuietly(fos);
}
}
}
}
zis.closeEntry();
}
} finally {
if (zis != null) {
org.apache.commons.io.IOUtils.closeQuietly(zis);
}
}

return tempDirectory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ public String getMimeType(Object value, Operation operation) throws ServiceExcep
@Override
public void write(Object value, OutputStream output, Operation operation)
throws IOException, ServiceException {

InputStream input = null;
try {
input = ((RepositoryItem) value).getContents();
try (InputStream input = ((RepositoryItem) value).getContents()) {
if (null != input) {
IOUtils.copy(input, output);
} else {
Expand All @@ -62,7 +59,6 @@ public void write(Object value, OutputStream output, Operation operation)
} catch (IOException e) {
throw new ServiceException("Failed to encode the repository item onto the output", e);
} finally {
IOUtils.closeQuietly(input);
output.flush();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.io.Writer;
import java.nio.charset.Charset;
import net.opengis.cat.csw20.DescribeRecordType;
import org.apache.commons.io.IOUtils;
import org.geoserver.config.GeoServer;
import org.geoserver.csw.CSWInfo;
import org.geotools.feature.NameImpl;
Expand Down Expand Up @@ -68,21 +67,18 @@ public void writeSchemaComponent(
schemaLocationRoot = schemaLocationRoot.substring(0, schemaLocationRoot.length() - 1);
}

BufferedReader reader = null;
try {
reader =
new BufferedReader(
new InputStreamReader(
getClass().getResourceAsStream(schemaPath),
Charset.forName("UTF-8")));
try (BufferedReader reader =
new BufferedReader(
new InputStreamReader(
getClass().getResourceAsStream(schemaPath),
Charset.forName("UTF-8")))) {

String line;
while ((line = reader.readLine()) != null) {
line = line.replace("%SCHEMAS_ROOT%", schemaLocationRoot);
bw.write(line);
bw.write("\n");
}
} finally {
IOUtils.closeQuietly(reader);
}
}
}

0 comments on commit e357e37

Please sign in to comment.