Skip to content

Commit e184e8d

Browse files
committed
code cleanup - use try-with-resources were possible
1 parent 11ecc99 commit e184e8d

File tree

29 files changed

+169
-257
lines changed

29 files changed

+169
-257
lines changed

visualvm/applicationviews/src/org/graalvm/visualvm/application/views/threads/PersistenceSupport.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,7 @@ private synchronized static void saveDataManager(VisualVMThreadsDataManager dm,
133133
}
134134

135135
private static VisualVMThreadsDataManager loadDataManager(InputStream is) throws IOException {
136-
SavedThreadsDataManager dm = null;
137-
DataInputStream dis = null;
138-
try {
139-
dis = new DataInputStream(is);
136+
try (DataInputStream dis = new DataInputStream(is)) {
140137

141138
if (!THREADS_SNAPSHOT_HEADER.equals(dis.readUTF()))
142139
throw new IOException("Unknown snapshot format"); // NOI18N
@@ -157,12 +154,8 @@ private static VisualVMThreadsDataManager loadDataManager(InputStream is) throws
157154
tdata[tidx] = td;
158155
}
159156

160-
dm = new SavedThreadsDataManager(stime, etime, dtcount, tdata);
161-
} finally {
162-
if (dis != null) dis.close();
157+
return new SavedThreadsDataManager(stime, etime, dtcount, tdata);
163158
}
164-
165-
return dm;
166159
}
167160

168161

visualvm/appui/src/org/graalvm/visualvm/modules/appui/about/AboutDialogControls.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,7 @@ public void run() {
208208
}
209209

210210
private void showLogfileInBrowser(final File logfile) throws Exception {
211-
RandomAccessFile raf = null;
212-
try {
213-
raf = new RandomAccessFile(logfile, "r"); // NOI18N
211+
try (RandomAccessFile raf = new RandomAccessFile(logfile, "r")) { // NOI18N
214212
byte[] buffer = new byte[(int)raf.length()];
215213
raf.readFully(buffer);
216214
final String logfileContents = new String(buffer);
@@ -231,8 +229,6 @@ protected void fireActionPerformed(ActionEvent event) {
231229
tb.showCodeText(string);
232230
}
233231
});
234-
} finally {
235-
if (raf != null) raf.close();
236232
}
237233
}
238234

visualvm/attach/src/org/graalvm/visualvm/attach/AttachModelImpl.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,8 @@ public synchronized void setFlag(String name, String value) {
151151
}
152152

153153
public synchronized HeapHistogramImpl takeHeapHistogram() {
154-
try {
155-
InputStream in = getVirtualMachine().heapHisto(ALL_OBJECTS_OPTION);
156-
HeapHistogramImpl h = new HeapHistogramImpl(in);
157-
in.close();
158-
return h;
154+
try (InputStream in = getVirtualMachine().heapHisto(ALL_OBJECTS_OPTION)) {
155+
return new HeapHistogramImpl(in);
159156
} catch (IOException ex) {
160157
LOGGER.log(Level.INFO,"takeHeapHistogram",ex); // NOI18N
161158
}

visualvm/attach/src/org/graalvm/visualvm/attach/OracleJRockitAttachModelImpl.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,8 @@ class OracleJRockitAttachModelImpl extends AttachModelImpl {
4141
}
4242

4343
public synchronized HeapHistogramImpl takeHeapHistogram() {
44-
try {
45-
InputStream in = getVirtualMachine().heapHisto(ALL_OBJECTS_OPTION);
46-
HeapHistogramImpl h = new JRockitHeapHistogramImpl(in);
47-
in.close();
48-
return h;
44+
try (InputStream in = getVirtualMachine().heapHisto(ALL_OBJECTS_OPTION)) {
45+
return new JRockitHeapHistogramImpl(in);
4946
} catch (IOException ex) {
5047
LOGGER.log(Level.INFO,"takeHeapHistogram",ex); // NOI18N
5148
}

visualvm/charts/src/org/graalvm/visualvm/charts/xy/XYStorage.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,7 @@ public synchronized void saveValues(OutputStream os) throws IOException {
106106
}
107107

108108
public synchronized void loadValues(InputStream is) throws IOException {
109-
DataInputStream dis = null;
110-
try {
111-
dis = new DataInputStream(is);
109+
try (DataInputStream dis = new DataInputStream(is)) {
112110

113111
if (!SNAPSHOT_HEADER.equals(dis.readUTF()))
114112
throw new IOException("Unknown snapshot format"); // NOI18N
@@ -126,8 +124,6 @@ public synchronized void loadValues(InputStream is) throws IOException {
126124
vals[iidx] = dis.readLong();
127125
addValues(timestamp, vals);
128126
}
129-
} finally {
130-
if (dis != null) dis.close();
131127
}
132128
}
133129

visualvm/heapdump/src/org/graalvm/visualvm/heapdump/impl/HeapDumpCategory.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.graalvm.visualvm.core.ui.DataSourceWindowManager;
3030
import org.graalvm.visualvm.heapdump.HeapDump;
3131
import java.io.File;
32-
import java.io.FileNotFoundException;
3332
import java.io.IOException;
3433
import java.io.RandomAccessFile;
3534
import javax.swing.filechooser.FileFilter;
@@ -68,20 +67,16 @@ protected boolean isSnapshot(File file) {
6867
}
6968

7069
private boolean checkHprofFile(File file) {
71-
try {
72-
if (file.isFile() && file.canRead() && file.length()>MIN_HPROF_SIZE) { // heap dump must be 1M and bigger
70+
if (file.isFile() && file.canRead() && file.length()>MIN_HPROF_SIZE) { // heap dump must be 1M and bigger
71+
try (RandomAccessFile raf = new RandomAccessFile(file,"r")) { // NOI18N
7372
byte[] prefix = new byte[HPROF_HEADER.length()+4];
74-
RandomAccessFile raf = new RandomAccessFile(file,"r"); // NOI18H
7573
raf.readFully(prefix);
76-
raf.close();
7774
if (new String(prefix).startsWith(HPROF_HEADER)) {
7875
return true;
7976
}
77+
} catch (IOException ex) {
78+
return false;
8079
}
81-
} catch (FileNotFoundException ex) {
82-
return false;
83-
} catch (IOException ex) {
84-
return false;
8580
}
8681
return false;
8782
}

visualvm/heapviewer/src/org/graalvm/visualvm/heapviewer/java/impl/JavaDiffDumpSelector.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import java.awt.event.MouseAdapter;
4646
import java.awt.event.MouseEvent;
4747
import java.io.File;
48-
import java.io.FileNotFoundException;
4948
import java.io.IOException;
5049
import java.io.RandomAccessFile;
5150
import java.util.ArrayList;
@@ -601,20 +600,16 @@ private SelectSecondSnapshotPanel getSecondSnapshotSelector() {
601600
private static final String HPROF_HEADER = "JAVA PROFILE 1.0"; // NOI18H
602601
private static final long MIN_HPROF_SIZE = 1024*1024L;
603602
static boolean checkHprofFile(File file) {
604-
try {
605-
if (file.isFile() && file.canRead() && file.length()>MIN_HPROF_SIZE) { // heap dump must be 1M and bigger
606-
byte[] prefix = new byte[HPROF_HEADER.length()+4];
607-
RandomAccessFile raf = new RandomAccessFile(file,"r"); // NOI18H
603+
if (file.isFile() && file.canRead() && file.length()>MIN_HPROF_SIZE) { // heap dump must be 1M and bigger
604+
byte[] prefix = new byte[HPROF_HEADER.length()+4];
605+
try (RandomAccessFile raf = new RandomAccessFile(file,"r")) { // NOI18N
608606
raf.readFully(prefix);
609-
raf.close();
610607
if (new String(prefix).startsWith(HPROF_HEADER)) {
611608
return true;
612609
}
610+
} catch (IOException ex) {
611+
return false;
613612
}
614-
} catch (FileNotFoundException ex) {
615-
return false;
616-
} catch (IOException ex) {
617-
return false;
618613
}
619614
return false;
620615
}

visualvm/libs.profiler/lib.profiler.heap/src/org/graalvm/visualvm/lib/jfluid/heap/AbstractLongMap.java

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,9 @@ interface Data {
213213
//~ Methods --------------------------------------------------------------------------------------------------------------
214214
static Data readFromStream(DataInputStream dis, CacheDirectory cacheDir, int entrySize) throws IOException {
215215
File tempFile = cacheDir.getCacheFile(dis.readUTF());
216-
RandomAccessFile file = new RandomAccessFile(tempFile, "rw"); // NOI18N
217-
Data dumpBuffer = getDumpBuffer(tempFile, file, entrySize);
218-
file.close();
219-
return dumpBuffer;
216+
try (RandomAccessFile file = new RandomAccessFile(tempFile, "rw")) { // NOI18N
217+
return getDumpBuffer(tempFile, file, entrySize);
218+
}
220219
}
221220

222221
byte getByte(long index);
@@ -436,10 +435,9 @@ public void force() throws IOException {
436435
}
437436

438437
private static MappedByteBuffer createBuffer(RandomAccessFile file, long length) throws IOException {
439-
FileChannel channel = file.getChannel();
440-
MappedByteBuffer buf = channel.map(MAP_MODE, 0, length);
441-
channel.close();
442-
return buf;
438+
try (FileChannel channel = file.getChannel()) {
439+
return channel.map(MAP_MODE, 0, length);
440+
}
443441
}
444442
}
445443

@@ -504,22 +502,22 @@ public void force() throws IOException{
504502
if (MemoryMappedData.MAP_MODE == FileChannel.MapMode.PRIVATE) {
505503
File newBufferFile = new File(bufferFile.getAbsolutePath()+".new"); // NOI18N
506504
long length = bufferFile.length();
507-
FileChannel channel = new FileOutputStream(newBufferFile).getChannel();
508-
int offset_start = 0;
509-
510-
for (int i = 0; i < dumpBuffer.length; i++) {
511-
MappedByteBuffer buf = dumpBuffer[i];
512-
long offset_end = (((i+1)*BUFFER_SIZE)/entrySize)*entrySize + entrySize;
513-
514-
if (offset_end > length) {
515-
offset_end = length;
505+
try (FileChannel channel = new FileOutputStream(newBufferFile).getChannel()) {
506+
int offset_start = 0;
507+
508+
for (int i = 0; i < dumpBuffer.length; i++) {
509+
MappedByteBuffer buf = dumpBuffer[i];
510+
long offset_end = (((i+1)*BUFFER_SIZE)/entrySize)*entrySize + entrySize;
511+
512+
if (offset_end > length) {
513+
offset_end = length;
514+
}
515+
buf.limit((int)(offset_end - i*BUFFER_SIZE));
516+
buf.position(offset_start);
517+
channel.write(buf);
518+
offset_start = (int)(offset_end - (i+1)*BUFFER_SIZE);
516519
}
517-
buf.limit((int)(offset_end - i*BUFFER_SIZE));
518-
buf.position(offset_start);
519-
channel.write(buf);
520-
offset_start = (int)(offset_end - (i+1)*BUFFER_SIZE);
521520
}
522-
channel.close();
523521
dumpBuffer = null;
524522
bufferFile.delete();
525523
newBufferFile.renameTo(bufferFile);
@@ -532,15 +530,15 @@ public void force() throws IOException{
532530
}
533531

534532
private static MappedByteBuffer[] createBuffers(RandomAccessFile file, long length) throws IOException {
535-
FileChannel channel = file.getChannel();
536-
MappedByteBuffer[] dumpBuffer = new MappedByteBuffer[(int) (((length + BUFFER_SIZE) - 1) / BUFFER_SIZE)];
537-
538-
for (int i = 0; i < dumpBuffer.length; i++) {
539-
long position = i * BUFFER_SIZE;
540-
long size = Math.min(BUFFER_SIZE + BUFFER_EXT, length - position);
541-
dumpBuffer[i] = channel.map(MemoryMappedData.MAP_MODE, position, size);
533+
MappedByteBuffer[] dumpBuffer;
534+
try (FileChannel channel = file.getChannel()) {
535+
dumpBuffer = new MappedByteBuffer[(int) (((length + BUFFER_SIZE) - 1) / BUFFER_SIZE)];
536+
for (int i = 0; i < dumpBuffer.length; i++) {
537+
long position = i * BUFFER_SIZE;
538+
long size = Math.min(BUFFER_SIZE + BUFFER_EXT, length - position);
539+
dumpBuffer[i] = channel.map(MemoryMappedData.MAP_MODE, position, size);
540+
}
542541
}
543-
channel.close();
544542
file.close();
545543
return dumpBuffer;
546544
}

visualvm/libs.profiler/lib.profiler.heap/src/org/graalvm/visualvm/lib/jfluid/heap/CacheDirectory.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,18 @@ HprofByteBuffer createHprofByteBuffer(File dumpFile) throws IOException{
145145
}
146146

147147
AbstractLongMap.Data createDumpBuffer(long fileSize, int entrySize) throws IOException {
148-
AbstractLongMap.Data dumpBuffer;
149148
File tempFile = createTempFile("NBProfiler", ".map"); // NOI18N
150-
151-
RandomAccessFile file = new RandomAccessFile(tempFile, "rw"); // NOI18N
152-
if (Boolean.getBoolean("org.graalvm.visualvm.lib.jfluid.heap.zerofile")) { // NOI18N
153-
byte[] zeros = new byte[512*1024];
154-
while(file.length()<fileSize) {
155-
file.write(zeros);
149+
try (RandomAccessFile file = new RandomAccessFile(tempFile, "rw")) { // NOI18N
150+
if (Boolean.getBoolean("org.graalvm.visualvm.lib.jfluid.heap.zerofile")) { // NOI18N
151+
byte[] zeros = new byte[512*1024];
152+
while(file.length()<fileSize) {
153+
file.write(zeros);
154+
}
155+
file.write(zeros,0,(int)(fileSize-file.length()));
156156
}
157-
file.write(zeros,0,(int)(fileSize-file.length()));
157+
file.setLength(fileSize);
158+
return AbstractLongMap.getDumpBuffer(tempFile, file, entrySize);
158159
}
159-
file.setLength(fileSize);
160-
dumpBuffer = AbstractLongMap.getDumpBuffer(tempFile, file, entrySize);
161-
file.close();
162-
return dumpBuffer;
163160
}
164161

165162
NumberList createNumberList(int idSize) throws IOException {

visualvm/libs.profiler/lib.profiler.heap/src/org/graalvm/visualvm/lib/jfluid/heap/HeapFactory.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,9 @@ static Heap loadHeap(CacheDirectory cacheDir)
9292
throws FileNotFoundException, IOException {
9393
File savedDump = cacheDir.getHeapDumpAuxFile();
9494
InputStream is = new BufferedInputStream(new FileInputStream(savedDump), 64*1024);
95-
DataInputStream dis = new DataInputStream(is);
96-
Heap heap = new HprofHeap(dis, cacheDir);
97-
dis.close();
98-
return heap;
95+
try (DataInputStream dis = new DataInputStream(is)) {
96+
return new HprofHeap(dis, cacheDir);
97+
}
9998
}
10099

101100
}

0 commit comments

Comments
 (0)