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 Original file line Diff line number Diff line change
Expand Up @@ -48,14 +48,8 @@ public void afterPropertiesSet() throws Exception {
JMSConfiguration.getConfigPathDir()); JMSConfiguration.getConfigPathDir());
if (properties.getType() != Type.RESOURCE) { if (properties.getType() != Type.RESOURCE) {
// copy the defaults // copy the defaults
InputStream inputStream = null; try (InputStream inputStream = defaults.getInputStream()) {
try {
inputStream = defaults.getInputStream();
IOUtils.copy(inputStream, properties.out()); IOUtils.copy(inputStream, properties.out());
} finally {
if (inputStream != null) {
org.apache.commons.io.IOUtils.closeQuietly(inputStream);
}
} }
} }
super.setLocation(new SpringResourceAdaptor(properties)); super.setLocation(new SpringResourceAdaptor(properties));
Expand Down
Original file line number Original file line 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("driver", "org.h2.Driver");
p.put("url", "jdbc:h2:mem:monitoring"); p.put("url", "jdbc:h2:mem:monitoring");
File file = new File("./target/monitoring/db.properties"); File file = new File("./target/monitoring/db.properties");
FileOutputStream fos = null;
try { if (!file.getParentFile().exists()) {
if (!file.getParentFile().exists()) { assertTrue(file.getParentFile().mkdirs());
assertTrue(file.getParentFile().mkdirs()); }
} try (FileOutputStream fos = new FileOutputStream(file)) {
fos = new FileOutputStream(file);
p.store(fos, null); p.store(fos, null);
} finally {
IOUtils.closeQuietly(fos);
} }


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


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


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


// creation of the workspace if not already present // creation of the workspace if not already present
Expand Down
Original file line number Original file line 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); ResumableUploadResource resource = getResource(uploadId);
Long writtenBytes = 0L; Long writtenBytes = 0L;
try { try {
final ReadableByteChannel source = entity.getChannel();
RandomAccessFile raf = null;
FileChannel outputChannel = null; try (final ReadableByteChannel source = entity.getChannel(); RandomAccessFile raf = new RandomAccessFile(resource.getFile(), "rw"); FileChannel outputChannel = raf.getChannel() ) {
try {
raf = new RandomAccessFile(resource.getFile(), "rw");
outputChannel = raf.getChannel();
writtenBytes = writtenBytes =
IOUtils.copyToFileChannel(256 * 1024, source, outputChannel, startPosition); IOUtils.copyToFileChannel(256 * 1024, source, outputChannel, startPosition);
} finally {
try {
if (raf != null) {
raf.close();
}
} finally {
IOUtils.closeQuietly(source);
IOUtils.closeQuietly(outputChannel);
}
} }
} catch (IOException e) { } catch (IOException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e); LOGGER.log(Level.SEVERE, e.getMessage(), e);
} finally {

} }


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


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

try {
IOUtils.copy(request.getInputStream(), w); IOUtils.copy(request.getInputStream(), w);
w.flush(); w.flush();
} finally {
IOUtils.closeQuietly(w);
} }
} catch (IOException e) { } catch (IOException e) {
throw new RestException( throw new RestException(
Expand Down
Original file line number Original file line 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) { private String readFile(Resource file) {
InputStream in = file.in();
try { try (InputStream in = file.in()) {
String s = IOUtils.toString(in); String s = IOUtils.toString(in);
return s; return s;
} catch (IOException ex) { } catch (IOException ex) {
LOGGER.warning( LOGGER.warning(
String.format( String.format(
"Error reading file '%s' because ", file.path(), ex.getMessage())); "Error reading file '%s' because ", file.path(), ex.getMessage()));
} finally {
IOUtils.closeQuietly(in);
} }
return ""; return "";
} }
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -119,13 +119,10 @@ protected void onSubmit() {


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


private void save() { private void save() {
Script s = (Script) form.getModelObject(); Script s = (Script) form.getModelObject();
OutputStream out = s.getResource().out(); try (OutputStream out = s.getResource().out()) {
try {
IOUtils.write(s.getContents(), out); IOUtils.write(s.getContents(), out);
} catch (IOException e) { } catch (IOException e) {
LOGGER.log(Level.WARNING, e.getMessage(), e); LOGGER.log(Level.WARNING, e.getMessage(), e);
} finally {
IOUtils.closeQuietly(out);
} }
} }
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
*/ */
package org.geoserver.wps.gs.download; 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.catalog.FeatureTypeInfo;
import org.geoserver.platform.resource.Resource; import org.geoserver.platform.resource.Resource;
import org.geoserver.wps.ppio.ComplexPPIO; import org.geoserver.wps.ppio.ComplexPPIO;
Expand All @@ -35,6 +29,12 @@
import org.opengis.util.ProgressListener; import org.opengis.util.ProgressListener;
import org.springframework.context.ApplicationContext; 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 * 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. * 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); final Resource output = resourceManager.getTemporaryResource(extension);


// write checking limits // write checking limits
OutputStream os = null; try (OutputStream os = getResourceOutputStream(output, limit)) {
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;
}

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


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

int test = in.readInt(); int test = in.readInt();
return test == 0x504b0304; return test == 0x504b0304;
} catch (IOException e) { } catch (IOException e) {
if (LOGGER.isLoggable(Level.SEVERE)) { if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e); LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
} }
return false; 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); zipout.putNextEntry(entry);


// copy over the file // copy over the file
InputStream in = null; try (InputStream in = new FileInputStream(file)) {
try {
int c; int c;
in = new FileInputStream(file);
while (-1 != (c = in.read(buffer))) { while (-1 != (c = in.read(buffer))) {
zipout.write(buffer, 0, c); zipout.write(buffer, 0, c);
} }
zipout.closeEntry(); zipout.closeEntry();
} finally {
// close the input stream
if (in != null) {
org.apache.commons.io.IOUtils.closeQuietly(in);
}
} }
zipout.flush(); zipout.flush();
} }
Expand Down
Original file line number Original file line 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 { public static File decode(InputStream input, File tempDirectory) throws Exception {


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


// Copy the whole file in the new position // 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; int count;
byte data[] = new byte[4096]; byte data[] = new byte[4096];
// write the files to the disk // write the files to the disk
FileOutputStream fos = null;
try { try (FileOutputStream fos = new FileOutputStream(file)) {
fos = new FileOutputStream(file);
while ((count = zis.read(data)) != -1) { while ((count = zis.read(data)) != -1) {
fos.write(data, 0, count); fos.write(data, 0, count);
} }
fos.flush(); fos.flush();
} finally { }
if (fos != null) {
org.apache.commons.io.IOUtils.closeQuietly(fos);
}
}
} }
zis.closeEntry(); zis.closeEntry();
} }
} finally {
if (zis != null) {
org.apache.commons.io.IOUtils.closeQuietly(zis);
}
} }


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

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


BufferedReader reader = null; try (BufferedReader reader =
try { new BufferedReader(
reader = new InputStreamReader(
new BufferedReader( getClass().getResourceAsStream(schemaPath),
new InputStreamReader( Charset.forName("UTF-8")))) {
getClass().getResourceAsStream(schemaPath),
Charset.forName("UTF-8")));
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
line = line.replace("%SCHEMAS_ROOT%", schemaLocationRoot); line = line.replace("%SCHEMAS_ROOT%", schemaLocationRoot);
bw.write(line); bw.write(line);
bw.write("\n"); bw.write("\n");
} }
} finally {
IOUtils.closeQuietly(reader);
} }
} }
} }

0 comments on commit e357e37

Please sign in to comment.