132 questions
-2
votes
1
answer
196
views
How to create a file only once when it's needed?
I'm working on a Java utility. This utility should read some input files (one or several). Each input file contains three types of strings, representing an integer, a float, and a string. The utility ...
4
votes
2
answers
256
views
Does try-with-resources call dispose() method? [duplicate]
I have been using try-with-resources a lot. I use it for database resource types or File stuff all the time to close the resource.
Now, I am using POI for large Excel files, and I am just noticing ...
0
votes
1
answer
202
views
How to run parallel tasks in a for loop, for dealing with AutoCloseable files in Java
MOTIVATION
On a youtube video of Venkat Subramaniam, he tells about how not use AutoCloseable, but try to use a function like "use". Because, one may forget to implement try block on a class ...
0
votes
1
answer
414
views
Is there AutoCloseable like class that doesn't throw Exception [closed]
Does any major Java library offer a AutoCloseable like interface for which close method doesn't throw exception?
My close implementation is very simple and I'd like to avoid the boilerplate of ...
1
vote
1
answer
582
views
Return autocloseable object inside a CompletableFuture and use it in whenComplete
I want to return an autocloseable object inside a CompletableFuture and use it in whenComplete without going to close it manually later.
This is the code that I've tried, but of course it won't work ...
0
votes
1
answer
414
views
How to keep track if an instance has been closed with Java AutoCloseable?
I'm reading Effective Java by Joshua Bloch. In ITEM 8: AVOID FINALIZERS AND CLEANERS of CHAPTER 2, he highlights the problems with using finalizers and cleaners for memory management. This article by ...
-2
votes
1
answer
1k
views
Java Autocloseable Test [closed]
public class CloseableResource implements AutoCloseable {
private static boolean _closed = false;
int _n;
public CloseableResource(int n){
}
public void use() throws Exception{
...
1
vote
1
answer
920
views
Kotlin `use` with AutoCloseable type and a lambda returning Boolean
This must be simple, but I've been banging my head against it for half an hour now... Here's my old, exception-unsafe code:
fun isReady(): Boolean {
try {
val cc: CommandChannel = ...
0
votes
2
answers
278
views
Closing (Auto)Closeables that exist only in `Either`
I currently face the problem of correctly closing resources that never leave their containing Either.
The relevant code looks something like this:
object SomeError
class MyRes : AutoCloseable { [...] }...
0
votes
1
answer
95
views
Is allocating new instance of Closeable to previously created instance, closing previous?
For example Scanner class, which implements Closeable interface:
Scanner sc = new Scanner(...);
sc.//do smthing
sc = new Scanner(...); <- is previous Scanner closed? Does it create memory leak?
sc./...
0
votes
1
answer
1k
views
springboot return 200 when close Closeable even throw exception
How to return error in springboot and close a Closeable class?
When I got some error in springboot and close a closable class is returning 200 OK
don't I need close the Closeable? Is there some way to ...
0
votes
1
answer
740
views
JavaMail-Folder AutoClosable exception
Since JavaMail version 1.6.0 classes Store and Folder (amongst Transport) should implement AutoClosable interface.
I did not find any examples of someone using JavaMail API with auto-closable.
After a ...
0
votes
1
answer
402
views
How FileInputStream instance is automatically created when Closeable interface is implemented by some custom class when using try-with-resources
I have been working with try-with-resources statement.
try(FileReader rd = new FileReader("Test.txt");){}
catch (Exception e) {e.printStackTrace();}
The benefit of using try with resources ...
4
votes
1
answer
3k
views
How to close aws client in aws lambda
I am trying to correctly write an aws lambda using Java that will use aws sdk SqsClient and SnsClient. I see that these clients implement close() method, and it is generally a good practice to call ...
5
votes
1
answer
4k
views
Mark a method as returning an existing closeable to avoid spurious warning at usage site
Given a class MyClass using an internal closeable object, myCloseable and providing a method getCloseable() that returns it; Eclipse, if configured for this kind of closeable resource warnings, will ...
0
votes
1
answer
172
views
Does ActiveMQManagedConnection (EAP/AMQ) support Autoclosable?
I am using:
openshift
AMQ (seems to be a forked activemq-5.11.0.redhat...... version)
EAP 7.2.3
While local debugging I get as Connection some ~ConnectionProxy with a physical ...
3
votes
2
answers
553
views
Should closing a resource always be a responsibility of the function that opens it?
public ResultSet executeQuery(String query) {
return session.call(query)
}
public List<Record> findRecords(String name) {
String query = prepareQuery(name);
return mapResultToRecord(...
2
votes
1
answer
1k
views
How to run onClose operation after creating Flux.fromIterable?
Suppose we need to create a Flux based on contents of a Closeable resource.
For clarity say there is a BufferedReader to be converted to Flux<String>.
BufferedReader reader = createReader("...
7
votes
3
answers
1k
views
How should a closed resource behave in java?
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 ...
1
vote
1
answer
394
views
How can I compose resources in Scala while still closing them correctly with scala-arm?
I have a class that takes a local file, transforms it, and stores it in GCS:
import java.nio.channels.Channels
import java.nio.file.{ Files, Path }
import java.util.zip.{ GZIPOutputStream, ...
10
votes
1
answer
910
views
Is using a lambda a safe, correct, and equivalent workaround for classes that do not implement AutoCloseable?
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 ...
8
votes
3
answers
2k
views
Close a dynamic number of AutoCloseable objects in try-with-resources
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 to ...
1
vote
2
answers
1k
views
Order of execution of Closeable and AutoCloseable close() methods
Can someone please explain to me what is happening here and in which order?. The output doesn't make any sense to me.
The output is T 1 IOE F.
The code is:
import java.io.Closeable;
import java.io....
2
votes
1
answer
2k
views
How to properly pass InputStream to another constructor? (Java)
In the code below I try to access my other constructor that takes a InputStream...
However I need to somehow close this stream to avoid resource leaks. If I try to put a try catch around, it will ...
31
votes
5
answers
12k
views
In Java, how to check that AutoCloseable.close() has been called?
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 ...