78
78
import java .awt .Color ;
79
79
import java .awt .Image ;
80
80
import java .awt .image .BufferedImage ;
81
- import java .io .File ;
82
81
import java .io .IOException ;
82
+ import java .io .InputStream ;
83
+ import java .io .OutputStream ;
84
+ import java .nio .file .Files ;
85
+ import java .nio .file .Path ;
83
86
import java .util .ArrayList ;
84
87
import java .util .Collection ;
85
88
import java .util .Collections ;
@@ -392,19 +395,17 @@ private CommandResult loadMapFromFile(final CommandContext ctx) throws CommandEx
392
395
if (map .type () != ItemTypes .FILLED_MAP .get ()) {
393
396
throw new CommandException (Component .text ("You must hold a map in your hand" ));
394
397
}
395
- final File file = new File ("map.png" );
396
- if (!file . isFile ( )) {
398
+ final Path p = Path . of ("map.png" );
399
+ if (!Files . isRegularFile ( p )) {
397
400
try {
398
- if (!file .createNewFile ()) {
399
- throw new IOException ("failed to create new file :(" );
400
- }
401
+ Files .createFile (p );
401
402
} catch (final IOException e ) {
402
403
e .printStackTrace ();
403
404
}
404
405
}
405
406
final BufferedImage image ;
406
- try {
407
- image = ImageIO .read (file );
407
+ try ( final InputStream is = Files . newInputStream ( p )) {
408
+ image = ImageIO .read (is );
408
409
} catch (final IOException e ) {
409
410
throw new CommandException (Component .text (e .getMessage ()), e );
410
411
}
@@ -420,10 +421,10 @@ private CommandResult loadMapFromFile(final CommandContext ctx) throws CommandEx
420
421
}
421
422
422
423
private CommandResult savePallete (final CommandContext ctx ) {
423
- final File file = new File ("pallete.png" );
424
+ final Path p = Path . of ("pallete.png" );
424
425
try {
425
- if (!file . isFile ( )) {
426
- file . createNewFile ( );
426
+ if (!Files . isRegularFile ( p )) {
427
+ Files . createFile ( p );
427
428
}
428
429
final MapCanvas .Builder builder = MapCanvas .builder ();
429
430
@@ -451,7 +452,9 @@ private CommandResult savePallete(final CommandContext ctx) {
451
452
}
452
453
final MapCanvas canvas = builder .build ();
453
454
final Color color = new Color (0 ,0 ,0 ,0 );
454
- ImageIO .write ((BufferedImage ) canvas .toImage (color ),"png" , file );
455
+ try (final OutputStream os = Files .newOutputStream (p )) {
456
+ ImageIO .write ((BufferedImage ) canvas .toImage (color ), "png" , os );
457
+ }
455
458
} catch (final IOException e ) {
456
459
Sponge .server ().sendMessage (Component .text ("IOException" ));
457
460
}
@@ -465,18 +468,18 @@ private CommandResult saveToFile(final CommandContext ctx) throws CommandExcepti
465
468
throw new CommandException (Component .text ("You must hold a map in your hand" ));
466
469
}
467
470
final Image image = map .require (Keys .MAP_INFO ).require (Keys .MAP_CANVAS ).toImage ();
468
- final File file = new File ("map.png" );
469
- if (!file . isFile ( )) {
471
+ final Path p = Path . of ("map.png" );
472
+ if (!Files . isRegularFile ( p )) {
470
473
try {
471
- if (!file .createNewFile ()) {
472
- throw new IOException ("failed to create new file :(" );
473
- }
474
+ Files .createFile (p );
474
475
} catch (final IOException e ) {
475
476
e .printStackTrace ();
476
477
}
477
478
}
478
479
try {
479
- ImageIO .write ((BufferedImage )image , "png" , file );
480
+ try (final OutputStream os = Files .newOutputStream (p )) {
481
+ ImageIO .write ((BufferedImage ) image , "png" , os );
482
+ }
480
483
} catch (final IOException e ) {
481
484
e .printStackTrace ();
482
485
}
0 commit comments