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
2
votes
2 answers

Is try-with-resource not safe when declaring multiple effectively final resources?

Since Java 9 we can use effectively final variables in try-with-resources. The example below presents a situation where one of the resources initialization throws an exception. public static void main(String[] args) { Resource1 r1 = new…
RinaldoDev
  • 985
  • 6
  • 21
2
votes
1 answer

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. BufferedReader reader = createReader("my_resource_path"); Flux flux = Flux.fromIterable(()…
diziaq
  • 6,881
  • 16
  • 54
  • 96
2
votes
2 answers

Why is StandardOpenOption.DELETE_ON_CLOSE not deleting the source file of the FileChannel?

We have underneath method in Java which should delete the source file when its close method is called. private void appendFile(Path destination, Path source) {     try (FileChannel sourceChannel…
2
votes
1 answer

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 complain that the constructor call isn't the first…
AgentM
  • 406
  • 4
  • 18
2
votes
2 answers

Jersey: Close I/O resources after HTTP response

My Setup: I have created a REST service (Jersey/Dropwizard) that streams large content from a database. During a GET operation, the service taps into the database through a connection pool, wraps the data into a stream and performs some on-the-fly…
user5537999
2
votes
1 answer

Java: Force Client of Method to Execute Code Before Returning

I have a bunch of WebDriver profiles (let's call them x, y and z) and I need to select a random profile to perform certain actions. Let's call the actions A, B and C. In my class WebDriverManager I've defined three methods: doA(), doB() and doC().…
2
votes
0 answers

How resource close statement works in Java 7 internally?

Java 7 features has autoclose of resource and multi-catch statement, but how it works internally in both cases? For example // Try with resource public void testTryWithResourcesStatement() throws FileNotFoundException, IOException { try…
shiv
  • 477
  • 5
  • 17
2
votes
4 answers

Java Try-With-Resources Unknown Resource Count

I have a need to open N multicast sockets (where N comes from the size of an argument list). I will then send the same data to each of the N sockets within a loop, and finally, close each socket. My question is, how do I do this using the…
Jeff G
  • 4,470
  • 2
  • 41
  • 76
2
votes
1 answer

Java "Resource Leak" Warning Not Triggering When it Should

I have a class called JavaShellStream that extends the interfaces Closeable and Autocloseable. However, when I use the class, and do not call its close() method, no warning is triggered that there is a resource leak. I have another class called…
Mat Jones
  • 936
  • 1
  • 10
  • 27
2
votes
4 answers

Is it important to add AutoCloseable in java?

Is it important to implement AutoCloseable in java? Would it make sense if I create a class which implements AutoCloseable extends a other class which doesn't implement it? e.g public class IGetClosed extends ICannotBeClosed implements AutoCloseable…
user6302426
2
votes
1 answer

Autocloseable class doesn't invoke default close method

I have this piece of code: public class Resource implements AutoCloseable{ private String s = "I am resource."; private int NuberOfResource; public Resource(int NuberOfResource) { this.NuberOfResource = NuberOfResource; …
user5507755
  • 251
  • 1
  • 9
2
votes
3 answers

Call method on object in try-with-resources block

The Java tutorial state that you can do the following with try-with-resources: try (Statement stmt = con.createStatement()) { ResultSet rs = stmt.executeQuery(query); ... } catch (SQLException e) { ... } In this tutorial the ResultSet…
Ian2thedv
  • 2,691
  • 2
  • 26
  • 47
2
votes
2 answers

Is java.nio.file.Files.write(...) safe?

java.nio.file.Files.write(...) method throws IOException and I can't use it in try(java.nio.file.Files.write(...)) construction. Is it "AutoCloseable" and safe in case of exception?
Vitaly
  • 2,552
  • 2
  • 18
  • 21
2
votes
1 answer

Do I need to call finish() when using a GZIPOutputStream object inside try-wtth-resources block

When using a GZipOutputStream inside of a try-with-resources [autoclosable] block, do I need to explicitly call finish() after I'm done with my resource?
NathanChristie
  • 2,374
  • 21
  • 20
2
votes
1 answer

Do pre JAVA 7 JDBC APIs work with JAVA 7 try with resources?

Just converting an app to JAVA 7 and wondering if anyone has had issue with try with resources and c3p0 connection pool. I guess now JAVA automatically closes db, prepared statements and statements if you use the the try with resources. Does it mean…
user432024
  • 4,392
  • 8
  • 49
  • 85
1 2 3
8 9