2
2
3
3
import java .io .Closeable ;
4
4
import java .io .EOFException ;
5
+ import java .io .File ;
5
6
import java .io .FileNotFoundException ;
6
7
import java .io .IOException ;
7
8
import java .nio .ByteBuffer ;
8
9
import java .nio .channels .Channel ;
10
+ import java .nio .channels .Channels ;
9
11
import java .nio .channels .FileChannel ;
10
12
import java .nio .channels .ReadableByteChannel ;
11
13
import java .nio .channels .SeekableByteChannel ;
27
29
import java .util .Set ;
28
30
import java .util .function .Function ;
29
31
32
+ import static io .github .mmhelloworld .idrisjvm .runtime .Directories .workingDir ;
30
33
import static java .nio .charset .StandardCharsets .UTF_8 ;
31
34
import static java .nio .file .attribute .PosixFilePermission .GROUP_EXECUTE ;
32
35
import static java .nio .file .attribute .PosixFilePermission .GROUP_READ ;
@@ -120,6 +123,14 @@ public static ChannelIo open(String name, String mode) {
120
123
}
121
124
}
122
125
126
+ public static ChannelIo popen (String command , String mode ) throws IOException {
127
+ Process process = new ProcessBuilder (IdrisSystem .getCommand (command ))
128
+ .directory (new File (workingDir ))
129
+ .start ();
130
+ return new ChannelIo (null , new ReadableWritableChannel (Channels .newChannel (process .getInputStream ()),
131
+ Channels .newChannel (process .getOutputStream ())));
132
+ }
133
+
123
134
public static void close (ChannelIo file ) {
124
135
file .close ();
125
136
}
@@ -129,17 +140,17 @@ public static int chmod(String file, int mode) {
129
140
.chmod (mode );
130
141
}
131
142
132
- public static void createDirectories (Path dirPath ) throws IOException {
133
- if (dirPath != null ) {
134
- java . nio . file . Files .createDirectories (dirPath );
143
+ public static void createDirectories (Path path ) throws IOException {
144
+ if (path != null && (! Files . isSymbolicLink ( path ) || ! Files . exists ( path )) ) {
145
+ Files .createDirectories (path );
135
146
}
136
147
}
137
148
138
149
public static void writeFile (String pathString , String content ) throws IOException {
139
150
Path path = Paths .createPath (pathString );
140
151
byte [] bytes = content .getBytes (UTF_8 );
141
152
createDirectories (path .getParent ());
142
- java . nio . file . Files .write (path , bytes );
153
+ Files .write (path , bytes );
143
154
}
144
155
145
156
public static int flush (ChannelIo file ) {
@@ -334,9 +345,7 @@ public int write(ByteBuffer src) throws IOException {
334
345
}
335
346
336
347
private static ChannelIo open (Path path , OpenOption ... openOptions ) throws IOException {
337
- if (path .getParent () != null ) {
338
- java .nio .file .Files .createDirectories (path .getParent ());
339
- }
348
+ ensureParentDirectory (path );
340
349
return new ChannelIo (path , FileChannel .open (path , openOptions ));
341
350
}
342
351
0 commit comments