Questions tagged [autocloseable]

java.lang.AutoCloseable is an interface which, when implemented by a class, can be used in the try-with-resources Statement. It is ensured that resource/class object is closed at the end of the statement.

The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

Resources:

127 questions
1
vote
1 answer

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 because it closes automatically when I return…
1
vote
3 answers

How to handle Autoclosable Resources which being passed as a Method arguments

I'm trying to migrate existing code to try with resource, but not sure how to deal when the closable resources are passed to other methods. My main concern is how to prevent the developers from touching the method additionalMethod() from closing the…
ir2pid
  • 5,604
  • 12
  • 63
  • 107
1
vote
1 answer

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 = getCommandChannel() // implements AutoCloseable …
Quuxplusone
  • 23,928
  • 8
  • 94
  • 159
1
vote
0 answers

Why Java StringWriter close method throws IOException?

I wonder why Java designers made close() method on StringWriter throwing the IOException, when it actually does nothing. E.g. in PrintWriter, the exceptions are suppressed even though the PrintWriter really can do something when it uses an…
Mi-La
  • 685
  • 10
  • 18
1
vote
1 answer

Converting an AutoCloseable, Iterable class into a Stream

StreamSupport.stream() can create a Stream from an Iterable, but what if the class implements Iterable and AutoCloseable? Is it possible to convert that class to a Stream and construct it within a try-with-resources block? public class…
hertzsprung
  • 9,445
  • 4
  • 42
  • 77
1
vote
1 answer

Why close method of class that implements Autoclosable interface is called first than Sql connection close?

I found solution to rollback transaction when commit fails and It works perfectly. But could you explain why Autorollback object close method is called first than connection close? Autorollback class : public class AutoRollback implements…
1
vote
2 answers

Closing resource coming as return from method

I have a helper class with a static method that returns a stream: public static InputStream getDocument(File file) throws IOException { ZipFile zipFile = new ZipFile(file); return zipFile.getInputStream(zipFile.getEntry("entry")); } Another…
Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181
1
vote
1 answer

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, ZipInputStream } import com.google.cloud.storage.{…
Etienne Neveu
  • 12,604
  • 9
  • 36
  • 59
1
vote
2 answers

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.IOException; public class TestRes { public…
jivko
  • 444
  • 2
  • 17
1
vote
0 answers

If an autocloseable is not stored in a variable, then do I have to try-with-resource it?

Saying I have an autocloseable resource (returned by a Sql.query call), then is it mandatory to put its return into a variable and try-with-resource it to avoid resource leaking? try (final Sql sql = ...) { // sql.query instanciates…
Xenos
  • 3,351
  • 2
  • 27
  • 50
1
vote
2 answers

Cast objects to an interface they don't implement but having the same method signature

Please take a look at the following DbUtil class example. Here are three its methods public static void closeResults(ResultSet rs) { if (rs != null) { try { if (!rs.isClosed()){ rs.close(); } …
humkins
  • 9,635
  • 11
  • 57
  • 75
1
vote
2 answers

Why is Java complaining about my try-with-resources block?

I've got an IncomingTrackHandlerFactory (ith) which hands out instances of IncomingTrackHandler. These instances implements AutoCloseable. The IncomingTrackHandler deals with the database, and is short lived. Every instance is just used for a couple…
sbrattla
  • 5,274
  • 3
  • 39
  • 63
1
vote
0 answers

Findbugs OBL_UNSATISFIED_OBLIGATION on constructor of class extending AutoClosable baseclass

I am trying to subclass a class that implements AutoClosable. Here is a brief example: public class AutoClosableBase implements AutoCloseable { private final int a; public AutoClosableBase(int a) { this.a = a; } @Override …
Pieter Müller
  • 4,573
  • 6
  • 38
  • 54
1
vote
2 answers

In Line interface, I must call close() for all instance, or only when Line is opened?

By the definition of AutoCloseable interface, I must call close() for ALL instances. i.e. I must write like this. try(A a = new A()){ //do something } In java.sound.sampled.SourceDataLine interface, or more commonly, in…
user4512098
1
vote
1 answer

How to cover close method in unit tests for autocloseable beans closed by spring

I have several Spring beans/components implementing AutoCloseable, and I expect the Spring Container to close them when the application context is destroyed. Anyway, my code coverage tool is complaining because the close() methods are "uncovered by…
alessiop86
  • 1,285
  • 2
  • 19
  • 38
1 2 3
8 9