1818import java .io .IOException ;
1919import java .io .InputStream ;
2020import java .io .OutputStream ;
21- import java .nio .ByteBuffer ;
2221import java .nio .charset .StandardCharsets ;
2322import java .nio .file .Files ;
2423import java .nio .file .NoSuchFileException ;
@@ -89,23 +88,12 @@ public static Map<String, String> getConfig(SettingsSecurity sec, String name) {
8988 return null ;
9089 }
9190
92- public static void write (Path target , SettingsSecurity configuration ) throws IOException {
93- requireNonNull (target , "file must not be null" );
94- requireNonNull (configuration , "sec must not be null" );
95- writeFile (target , configuration , false );
96- }
97-
98- public static void writeWithBackup (Path target , SettingsSecurity configuration ) throws IOException {
99- requireNonNull (target , "file must not be null" );
100- requireNonNull (configuration , "sec must not be null" );
101- writeFile (target , configuration , true );
102- }
103-
10491 private static final boolean IS_WINDOWS =
10592 System .getProperty ("os.name" , "unknown" ).startsWith ("Windows" );
10693
107- private static void writeFile (Path target , SettingsSecurity configuration , boolean doBackup ) throws IOException {
108- requireNonNull (target , "target is null" );
94+ public static void write (Path target , SettingsSecurity configuration , boolean doBackup ) throws IOException {
95+ requireNonNull (target , "file must not be null" );
96+ requireNonNull (configuration , "configuration must not be null" );
10997 Path parent = requireNonNull (target .getParent (), "target must have parent" );
11098 Files .createDirectories (parent );
11199 Path tempFile = parent .resolve (target .getFileName () + "."
@@ -114,13 +102,19 @@ private static void writeFile(Path target, SettingsSecurity configuration, boole
114102 configuration .setModelVersion (SecDispatcher .class .getPackage ().getSpecificationVersion ());
115103 configuration .setModelEncoding (StandardCharsets .UTF_8 .name ());
116104
117- try (OutputStream out = Files .newOutputStream (tempFile )) {
118- new SecurityConfigurationStaxWriter ().write (out , configuration );
105+ try {
106+ try (OutputStream tempOut = Files .newOutputStream (tempFile )) {
107+ new SecurityConfigurationStaxWriter ().write (tempOut , configuration );
108+ }
109+
119110 if (doBackup && Files .isRegularFile (target )) {
120111 Files .copy (target , parent .resolve (target .getFileName () + ".bak" ), StandardCopyOption .REPLACE_EXISTING );
121112 }
122113 if (IS_WINDOWS ) {
123- copy (tempFile , target );
114+ try (InputStream is = Files .newInputStream (tempFile );
115+ OutputStream os = Files .newOutputStream (target )) {
116+ is .transferTo (os );
117+ }
124118 } else {
125119 Files .move (tempFile , target , StandardCopyOption .REPLACE_EXISTING );
126120 }
@@ -130,22 +124,4 @@ private static void writeFile(Path target, SettingsSecurity configuration, boole
130124 Files .deleteIfExists (tempFile );
131125 }
132126 }
133-
134- /**
135- * On Windows we use pre-NIO2 way to copy files, as for some reason it works. Beat me why.
136- */
137- private static void copy (Path source , Path target ) throws IOException {
138- ByteBuffer buffer = ByteBuffer .allocate (1024 * 32 );
139- byte [] array = buffer .array ();
140- try (InputStream is = Files .newInputStream (source );
141- OutputStream os = Files .newOutputStream (target )) {
142- while (true ) {
143- int bytes = is .read (array );
144- if (bytes < 0 ) {
145- break ;
146- }
147- os .write (array , 0 , bytes );
148- }
149- }
150- }
151127}
0 commit comments