Skip to content

Commit 0ece5da

Browse files
example about inputstream and file
1 parent 5d3661e commit 0ece5da

File tree

5 files changed

+59
-3
lines changed

5 files changed

+59
-3
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package testSuite.classes.stack_overflow.file_input_stream;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.util.Deque;
7+
import java.util.Iterator;
8+
import java.util.LinkedList;
9+
import java.util.Queue;
10+
import java.io.File;
11+
import java.io.FileInputStream;
12+
import java.io.IOException;
13+
14+
15+
16+
/**
17+
* Examples related to typestate refinements.
18+
* Idea from: https://stackoverflow.com/questions/5900435/blackberry-fileconnection-illegalstateexception
19+
* I am not sure we can handle this case since testFile does not need to know about FileInputStream, it is only used as a parameter.
20+
* Can we still relate them? Does it make sense?
21+
*/
22+
@SuppressWarnings("unused")
23+
public class Test {
24+
25+
public static void main(String[] args) {
26+
27+
// Create a test file
28+
File testFile = new File("liquidjava-example/src/main/java/testSuite/classes/stack_overflow/file_input_stream/testFile.txt");
29+
30+
// Simulate opening the file and forgetting to close it
31+
FileInputStream inputStream = null;
32+
try {
33+
// Open the file
34+
inputStream = new FileInputStream(testFile);
35+
System.out.println("Reading the file...");
36+
37+
// Simulate a long operation without closing the file
38+
// Note: Normally, you'd process the file here, but we're just mimicking the issue
39+
Thread.sleep(500); // Simulating a delay
40+
41+
// We forget to close the file here, mimicking the problem
42+
// inputStream.close(); // Error: Should be here to fix the issue
43+
44+
} catch (IOException | InterruptedException e) {
45+
e.printStackTrace();
46+
}
47+
48+
// Attempt to delete the file (this will fail if the file is still open)
49+
if (testFile.delete()) {
50+
System.out.println("File deleted successfully.");
51+
} else {
52+
System.out.println("Failed to delete the file. It might be open.");
53+
}
54+
}
55+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
qegigduaskxjnlm,

liquidjava-example/src/main/java/testSuite/classes/stack_overflow/iterator_not_tested/IteratorRefinements.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package test.currentlyTesting;
1+
package testSuite.classes.stack_overflow.iterator_not_tested;
22

33
import liquidjava.specification.ExternalRefinementsFor;
44
import liquidjava.specification.StateRefinement;

liquidjava-example/src/main/java/testSuite/classes/stack_overflow/iterator_not_tested/LinkedListRefinements.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package test.currentlyTesting;
1+
package testSuite.classes.stack_overflow.iterator_not_tested;
22

33
import java.util.Iterator;
44

liquidjava-example/src/main/java/testSuite/classes/stack_overflow/iterator_not_tested/TestIterator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package testSuite.classes.stack_overflow.iterator;
1+
package testSuite.classes.stack_overflow.iterator_not_tested;
22

33
import java.util.Deque;
44
import java.util.Iterator;

0 commit comments

Comments
 (0)