Skip to content

Commit

Permalink
Merge pull request #64 from huitseeker/resourceleaks
Browse files Browse the repository at this point in the history
Fix resource leaks in load/save
  • Loading branch information
mikiobraun committed Jun 22, 2015
2 parents bc20318 + 91b1292 commit 8213dba
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 40 deletions.
18 changes: 15 additions & 3 deletions src/main/java/org/jblas/ComplexDoubleMatrix.java
Expand Up @@ -1470,8 +1470,14 @@ public void in(DataInputStream dis) throws IOException {
* @throws IOException thrown on errors while writing the matrix to the file
*/
public void save(String filename) throws IOException {
DataOutputStream dos = new DataOutputStream(new FileOutputStream(filename, false));
this.out(dos);
FileOutputStream fos = new FileOutputStream(filename, false);
DataOutputStream dos = new DataOutputStream(fos);
try {
this.out(dos);
} finally {
dos.close();
fos.close();
}
}

/**
Expand All @@ -1481,8 +1487,14 @@ public void save(String filename) throws IOException {
* @throws IOException thrown on errors while reading the matrix
*/
public void load(String filename) throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream(filename));
FileInputStream fis = new FileInputStream(filename);
DataInputStream dis = new DataInputStream(fis);
try {
this.in(dis);
} finally {
dis.close();
fis.close();
}
}

/****************************************************************
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/org/jblas/ComplexFloatMatrix.java
Expand Up @@ -1470,8 +1470,14 @@ public void in(DataInputStream dis) throws IOException {
* @throws IOException thrown on errors while writing the matrix to the file
*/
public void save(String filename) throws IOException {
DataOutputStream dos = new DataOutputStream(new FileOutputStream(filename, false));
this.out(dos);
FileOutputStream fos = new FileOutputStream(filename, false);
DataOutputStream dos = new DataOutputStream(fos);
try {
this.out(dos);
} finally {
dos.close();
fos.close();
}
}

/**
Expand All @@ -1481,8 +1487,14 @@ public void save(String filename) throws IOException {
* @throws IOException thrown on errors while reading the matrix
*/
public void load(String filename) throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream(filename));
FileInputStream fis = new FileInputStream(filename);
DataInputStream dis = new DataInputStream(fis);
try {
this.in(dis);
} finally {
dis.close();
fis.close();
}
}

/****************************************************************
Expand Down
50 changes: 33 additions & 17 deletions src/main/java/org/jblas/DoubleMatrix.java
Expand Up @@ -2760,9 +2760,14 @@ public void in(DataInputStream dis) throws IOException {
* @throws IOException thrown on errors while writing the matrix to the file
*/
public void save(String filename) throws IOException {
DataOutputStream dos = new DataOutputStream(new FileOutputStream(filename, false));
this.out(dos);
dos.close();
FileOutputStream fos = new FileOutputStream(filename, false);
DataOutputStream dos = new DataOutputStream(fos);
try {
this.out(dos);
} finally {
dos.close();
fos.close();
}
}

/**
Expand All @@ -2772,9 +2777,15 @@ public void save(String filename) throws IOException {
* @throws IOException thrown on errors while reading the matrix
*/
public void load(String filename) throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream(filename));
this.in(dis);
dis.close();
FileInputStream fis = new FileInputStream(filename);
DataInputStream dis = new DataInputStream(fis);
try {
this.in(dis);
}
finally {
dis.close();
fis.close();
}
}

public static DoubleMatrix loadAsciiFile(String filename) throws IOException {
Expand Down Expand Up @@ -2808,19 +2819,24 @@ public static DoubleMatrix loadAsciiFile(String filename) throws IOException {
}
is.close();

// Go through file a second time process the actual data.
is = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
DoubleMatrix result = new DoubleMatrix(rows, columns);
int r = 0;
while ((line = is.readLine()) != null) {
String[] elements = WHITESPACES.split(line);
int firstElement = (elements[0].length() == 0) ? 1 : 0;
for (int c = 0, cc = firstElement; c < columns; c++, cc++) {
result.put(r, c, Double.valueOf(elements[cc]));
FileInputStream fis = new FileInputStream(filename);
try {
// Go through file a second time process the actual data.
is = new BufferedReader(new InputStreamReader(fis));
DoubleMatrix result = new DoubleMatrix(rows, columns);
int r = 0;
while ((line = is.readLine()) != null) {
String[] elements = WHITESPACES.split(line);
int firstElement = (elements[0].length() == 0) ? 1 : 0;
for (int c = 0, cc = firstElement; c < columns; c++, cc++) {
result.put(r, c, Double.valueOf(elements[cc]));
}
r++;
}
r++;
return result;
} finally {
fis.close();
}
return result;
}

public static DoubleMatrix loadCSVFile(String filename) throws IOException {
Expand Down
50 changes: 33 additions & 17 deletions src/main/java/org/jblas/FloatMatrix.java
Expand Up @@ -2760,9 +2760,14 @@ public void in(DataInputStream dis) throws IOException {
* @throws IOException thrown on errors while writing the matrix to the file
*/
public void save(String filename) throws IOException {
DataOutputStream dos = new DataOutputStream(new FileOutputStream(filename, false));
this.out(dos);
dos.close();
FileOutputStream fos = new FileOutputStream(filename, false);
DataOutputStream dos = new DataOutputStream(fos);
try {
this.out(dos);
} finally {
dos.close();
fos.close();
}
}

/**
Expand All @@ -2772,9 +2777,15 @@ public void save(String filename) throws IOException {
* @throws IOException thrown on errors while reading the matrix
*/
public void load(String filename) throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream(filename));
this.in(dis);
dis.close();
FileInputStream fis = new FileInputStream(filename);
DataInputStream dis = new DataInputStream(fis);
try {
this.in(dis);
}
finally {
dis.close();
fis.close();
}
}

public static FloatMatrix loadAsciiFile(String filename) throws IOException {
Expand Down Expand Up @@ -2808,19 +2819,24 @@ public static FloatMatrix loadAsciiFile(String filename) throws IOException {
}
is.close();

// Go through file a second time process the actual data.
is = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
FloatMatrix result = new FloatMatrix(rows, columns);
int r = 0;
while ((line = is.readLine()) != null) {
String[] elements = WHITESPACES.split(line);
int firstElement = (elements[0].length() == 0) ? 1 : 0;
for (int c = 0, cc = firstElement; c < columns; c++, cc++) {
result.put(r, c, Float.valueOf(elements[cc]));
FileInputStream fis = new FileInputStream(filename);
try {
// Go through file a second time process the actual data.
is = new BufferedReader(new InputStreamReader(fis));
FloatMatrix result = new FloatMatrix(rows, columns);
int r = 0;
while ((line = is.readLine()) != null) {
String[] elements = WHITESPACES.split(line);
int firstElement = (elements[0].length() == 0) ? 1 : 0;
for (int c = 0, cc = firstElement; c < columns; c++, cc++) {
result.put(r, c, Float.valueOf(elements[cc]));
}
r++;
}
r++;
return result;
} finally {
fis.close();
}
return result;
}

public static FloatMatrix loadCSVFile(String filename) throws IOException {
Expand Down

0 comments on commit 8213dba

Please sign in to comment.