Skip to content

Instantly share code, notes, and snippets.

@jstrachan
Created January 7, 2011 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jstrachan/769594 to your computer and use it in GitHub Desktop.
Save jstrachan/769594 to your computer and use it in GitHub Desktop.
helper class for working with closeable resources like streams
/**
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.camel.util;
import java.io.Closeable;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* A helper class to help folks write some code using a closable resource
*/
public abstract class CloseableTemplate<T extends Closeable, R> {
private static final transient Log LOG = LogFactory.getLog(CloseableTemplate.class);
public R use(T stream) throws Exception {
R answer;
try {
answer = doUse(stream);
} catch (Exception e) {
// lets close the stream catching and ignoring any exceptions as we are about to
// rethrow the actual exception
try {
stream.close();
} catch (IOException closeException) {
// ignore this exception
LOG.debug("Caught exception closing stream: " + e, e);
}
throw e;
}
// we processed correctly so lets let any close exceptions throw
if (stream != null) {
stream.close();
}
return answer;
}
/**
* Actually process the stream
*/
protected abstract R doUse(T stream) throws Exception;
}
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.camel.util;
import junit.framework.Assert;
import junit.framework.TestCase;
import java.io.BufferedReader;
import java.io.File;
import java.io.StringReader;
/**
* @version $Revision: 1039088 $
*/
public class CloseableTemplateTest extends TestCase {
public void testCloseable() throws Exception {
Boolean answer = new CloseableTemplate<BufferedReader, Boolean>() {
@Override
protected Boolean doUse(BufferedReader stream) throws Exception {
return stream.readLine().contains("world");
}
}.use(new BufferedReader(new StringReader("hello world")));
Assert.assertEquals("Should have found the world text!", Boolean.TRUE, answer);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment