autocloseable Questions
4
Is there any helper method in the JDK or common libraries that does this:
if (resource instanceof AutoCloseable) {
((AutoCloseable) resource).close();
}
Just a one-liner to call an object's clo...
Vierno asked 23/3, 2014 at 20:0
4
Solved
Autocloseable should always be used with try-with-resources. At least Intellij inspection suggests it.
So, if I have a code that produces Foo that implements Autocloseable I should do:
try (final ...
Disillusionize asked 7/12, 2016 at 11:56
3
Below is the sample code snippet to create SXSSFWorkbook:
try(SXSSFWorkbook wb = new SXSSFWorkbook()) {
//...
} finally {
wb.dispose(); //wb not accessible over here, so can't use try with resou...
Rival asked 15/5, 2018 at 13:32
3
Solved
Java docs of close() method of java.lang.AutoCloseable says
Note that unlike the close() method of Closeable, this close() method is
not required to be idempotent. In other words, calling this ...
Fact asked 11/10, 2013 at 14:1
2
The reader should be closed when a Stream is used in a try-with-resources.
Given this:
try(Stream<String> lines = new BufferedReader(reader).lines()) {
return lines.map(it -> trim ? it....
Reinsure asked 2/12, 2013 at 1:18
1
Solved
Background: I use the Java class InitialDirContext to access LDAP directories. Unfortunately, it does not implement interface AutoCloseable, so it cannot be used in try-with-resources blocks.
Here...
Pennywise asked 12/10, 2019 at 15:35
5
Solved
Consider the following situation :
try (ResultSet resultSet = DriverManager.getConnection("jdbc:...", "user", "pass")
.createStatement().executeQuery(sql)) {
.
.
...
Retroflexion asked 29/11, 2020 at 12:11
3
Solved
I have a java class that holds a Closeable resource internally. This means my class also implements the Closeable interface and closes the internal resource in its own close() method.
What is the ...
Vancevancleave asked 15/4, 2020 at 13:9
5
Solved
I read that the catch block in try-with-resources is optional.
I've tried creating a Connection object in a try-with-resources block, with no subsequent catch block, only to get compiler error fro...
Olmstead asked 26/8, 2014 at 2:18
2
Solved
I'm using CloseableHttpResponse (from apache-httpclient-4.5.3) and I'm not sure I'm using it right, I saw an answer with no votes to use EntityUtils.consume on finally:
CloseableHttpResponse...
Ewold asked 22/2, 2018 at 13:42
3
I am creating a variable amount of AutoCloseable objects in a try-with-resources block. At any point of exit, I want all of the allocated resources closed.
I can imagine writing something myself ...
Perceptual asked 26/4, 2019 at 16:40
0
This is a follow-up question from this one.
Specifically I'm trying to verify correct use of an API which involves calling close() on Java objects holding heavy native resources over JNI. To re-it...
Moonstone asked 12/2, 2019 at 14:1
5
I am authoring a java library. Some of the classes that are meant to be used by library users, hold native system resources (over JNI). I'd like to ensure that the user "disposes" these objects, as...
Antisyphilitic asked 29/1, 2019 at 19:44
3
Solved
It can be handy to time code execution so you know how long things take. However, I find the common way this is done sloppy since it's supposed to have the same indentation, which makes it harder t...
Astringent asked 23/10, 2018 at 10:23
2
Solved
I am looking at examples of try-with-resources in Java and I understand the following one:
try (Connection conn = DriverManager.getConnection(url, user, pwd);
Statement stmt = conn.createStatemen...
Austrasia asked 8/10, 2018 at 9:45
3
Solved
If I want to automatically close a resource passed as an argument, is there a more elegant solution than this?
void doSomething(OutputStream out) {
try (OutputStream closeable = out) {
// do so...
Singlehandedly asked 14/8, 2018 at 9:17
4
I know that the resource you pass with a try, will be closed automatically if the resource has AutoCloseable implemented. So far so good. But what do I do when i have several resources that I want ...
Frantz asked 31/5, 2015 at 2:7
1
These "resource leak" warnings I'm getting in Eclipse for AutoCloseables seem to be a life-saver.
However, how do I get them to work for factory created instances?
For example (a works, but b doe...
Heighttopaper asked 24/4, 2014 at 2:31
1
Solved
I am new in Java8, and I want to know if, for the AutoCloseable resource, I have to add a try for each resource, or it will work with the code above
try (Connection conn = getConnection();) ...
Cory asked 8/11, 2017 at 9:6
1
Looking at the quick start guide it gives the following code example:
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://targethost/homepage");
Clo...
Cran asked 10/6, 2017 at 5:18
3
Solved
Kotlin provides the use function for Closeable objects, but it seems they forgot to consider AutoCloseable (e.g. DB prepared statements) for the try-with-resources full Java equivalent.
I've imple...
Longship asked 28/3, 2016 at 10:35
1
When I compile my project, I am getting this error:
trouble processing java/lang/AutoCloseable.class
Please help me sort this out.
Curule asked 8/5, 2017 at 8:10
3
Solved
Today I tried to refactor this code, that reads ids from files in a directory,
Set<Long> ids = new HashSet<>();
for (String fileName : fileSystem.list("my-directory")) {
InputSt...
Mcgruter asked 25/4, 2017 at 11:10
1
Solved
My team has quite a huge amount of code. Recently I have found some objects that weren't closed properly.
How can i find all the instances that are not closed or not inside a try-with-resources blo...
Mayfair asked 14/2, 2017 at 8:15
2
Solved
When implementing an AutoCloseable to work with the Java 7 try-with-resources statement, I would like to know if there had been an exception within the try block. E.g.:
class C implements AutoClos...
Tran asked 25/1, 2014 at 17:10
1 Next >
© 2022 - 2024 — McMap. All rights reserved.