18
18
import java .io .IOException ;
19
19
import java .io .InputStream ;
20
20
import java .io .OutputStream ;
21
- import java .nio .ByteBuffer ;
22
21
import java .nio .charset .StandardCharsets ;
23
22
import java .nio .file .Files ;
24
23
import java .nio .file .NoSuchFileException ;
@@ -89,23 +88,12 @@ public static Map<String, String> getConfig(SettingsSecurity sec, String name) {
89
88
return null ;
90
89
}
91
90
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
-
104
91
private static final boolean IS_WINDOWS =
105
92
System .getProperty ("os.name" , "unknown" ).startsWith ("Windows" );
106
93
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" );
109
97
Path parent = requireNonNull (target .getParent (), "target must have parent" );
110
98
Files .createDirectories (parent );
111
99
Path tempFile = parent .resolve (target .getFileName () + "."
@@ -114,13 +102,19 @@ private static void writeFile(Path target, SettingsSecurity configuration, boole
114
102
configuration .setModelVersion (SecDispatcher .class .getPackage ().getSpecificationVersion ());
115
103
configuration .setModelEncoding (StandardCharsets .UTF_8 .name ());
116
104
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
+
119
110
if (doBackup && Files .isRegularFile (target )) {
120
111
Files .copy (target , parent .resolve (target .getFileName () + ".bak" ), StandardCopyOption .REPLACE_EXISTING );
121
112
}
122
113
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
+ }
124
118
} else {
125
119
Files .move (tempFile , target , StandardCopyOption .REPLACE_EXISTING );
126
120
}
@@ -130,22 +124,4 @@ private static void writeFile(Path target, SettingsSecurity configuration, boole
130
124
Files .deleteIfExists (tempFile );
131
125
}
132
126
}
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
- }
151
127
}
0 commit comments