@@ -9,29 +9,21 @@ import Java.Nio
9
9
import Java.IO
10
10
import Java.Util
11
11
import Java.Util.Concurrent
12
-
13
- %language TypeProviders
14
- %language ElabReflection
12
+ import IdrisJvm.ClientServerSocket
13
+ import IdrisJvm.FileChannelIo
14
+ import IdrisJvm.Files
15
+ import IdrisJvm.System
15
16
16
17
%hide Prelude . File . File
17
18
%hide Prelude . File . FileError
18
19
%hide Java . IO . File . File
19
20
20
- jdkimport [
21
- (stringClass, [" getBytes" ]),
22
- (pathsClass, [" get" ]),
23
- (fileChannelClass, [" open" ]),
24
- (filesClass, [" lines" , " write" , " readAttributes" , " createDirectory" ]),
25
- (fileTimeClass, [" to" ]),
26
- (streamClass, [" collect" ]),
27
- (collectorsClass, [" joining" ])
28
- ]
29
-
30
21
public export
31
22
data File = MkFileStdin
32
23
| MkFileStdout
33
24
| MkFileStderr
34
- | MkFile FileChannel Path
25
+ | MkFile FileChannelIo
26
+ | MkFileClientServerSocket ClientServerSocket
35
27
36
28
public export
37
29
FileError : Type
@@ -68,14 +60,13 @@ export
68
60
total
69
61
openFile : (f : String) -> (m : Mode ) -> JVM_IO (Either FileError File )
70
62
openFile f m = assert_total $ do
71
- path <- invokeStatic RuntimeClass " createPath" ( String -> JVM_IO Path ) f
63
+ path <- Files . createPath f
72
64
ensureParentDir path
73
65
openOptions <- listToArray (modeToOpenOption m)
74
- exceptionOrFile <- (fileChannelClass <.> " open(java/nio/file/Path,Array 1 i java/nio/file/OpenOption) " ) path openOptions
66
+ exceptionOrFile <- FileChannelIo . open path openOptions
75
67
case exceptionOrFile of
76
68
Left exception => pure $ Left $ believe_me exception
77
- Right Nothing => pure $ Left . believe_me $ ! (unableToOpenFile f)
78
- Right (Just file) => pure $ Right (MkFile file path)
69
+ Right file => pure $ Right (MkFile file)
79
70
where
80
71
hasWriteMode : Bool
81
72
hasWriteMode = case m of
@@ -91,54 +82,75 @@ openFile f m = assert_total $ do
91
82
case optParent of
92
83
Nothing => pure ()
93
84
Just pathDir => do
94
- _ <- invokeStatic FilesClass " createDirectories" (Path -> JVM_Array FileAttribute -> JVM_IO Path ) pathDir
95
- ! (newArray FileAttribute 0 )
85
+ Files . createDirectories pathDir
96
86
pure ()
97
87
else pure ()
88
+ export
89
+ getChar : File -> JVM_IO Char
90
+ getChar (MkFile fileChannelIo) = getChar fileChannelIo
91
+ getChar (MkFileClientServerSocket clientServerSocket) = getChar clientServerSocket
92
+ getChar MkFileStdin = IO . getChar
93
+ getChar _ = do
94
+ putStrLn " Unable to read a character"
95
+ exit 1
96
+
97
+ export
98
+ getLine : File -> JVM_IO String
99
+ getLine (MkFile fileChannelIo) = getLine fileChannelIo
100
+ getLine (MkFileClientServerSocket clientServerSocket) = ClientServerSocket . getLine clientServerSocket
101
+ getLine MkFileStdin = IO . getLine
102
+ getLine _ = do
103
+ putStrLn " Unable to read a line"
104
+ exit 1
105
+
106
+ export
107
+ writeString : File -> String -> JVM_IO ()
108
+ writeString (MkFile fileChannelIo) str = writeString fileChannelIo str
109
+ writeString (MkFileClientServerSocket socket) str = writeString socket str
110
+ writeString MkFileStdout str = print str
111
+ writeString _ _ = pure ()
98
112
99
113
export
100
114
closeFile : File -> JVM_IO ()
101
- closeFile (MkFile file _ ) = Channel . close file
115
+ closeFile (MkFile file) = FileChannelIo . close file
116
+ closeFile (MkFileClientServerSocket clientServerSocket) = ClientServerSocket . close clientServerSocket
102
117
closeFile _ = pure ()
103
118
104
119
export
105
120
changeDir : String -> JVM_IO Bool
106
- changeDir = invokeStatic RuntimeClass " changeDir" ( String -> JVM_IO Bool )
121
+ changeDir = Files . changeDir
107
122
108
123
export
109
124
getTemporaryFileName : JVM_IO String
110
- getTemporaryFileName = invokeStatic RuntimeClass " getTemporaryFileName" ( JVM_IO String )
125
+ getTemporaryFileName = Files . getTemporaryFileName
111
126
112
127
export
113
128
chmod : String -> Int -> JVM_IO ()
114
- chmod f m = invokeStatic RuntimeClass " chmod" ( String -> Int -> JVM_IO () ) f m
129
+ chmod = Files . chmod
115
130
116
131
export
117
132
total
118
133
createDir : String -> JVM_IO (Either FileError () )
119
- createDir d = assert_total $ do
120
- path <- invokeStatic RuntimeClass " createPath" (String -> JVM_IO Path ) d
121
- exceptionOrPath <- (filesClass <.> " createDirectory" ) path ! (newArray FileAttribute 0 )
122
- pure $ const () <$> exceptionOrPath
134
+ createDir d = assert_total $ Files . createDirectory d
123
135
124
136
export
125
137
currentDir : JVM_IO String
126
- currentDir = invokeStatic RuntimeClass " getWorkingDir" ( JVM_IO String )
138
+ currentDir = Files . getWorkingDir
127
139
128
140
-- This is returning Int to conform to Idris fileSize function type
129
141
-- even though Java's FileChannel returns long
130
142
export
131
143
fileSize : File -> JVM_IO (Either FileError Int)
132
- fileSize (MkFile f _ ) = do
133
- exceptionOrSize <- invokeInstance " size" ( FileChannel -> JVM_IO ( JVM_Throwable Bits64 )) f
144
+ fileSize (MkFile f) = do
145
+ exceptionOrSize <- FileChannelIo . size f
134
146
case exceptionOrSize of
135
147
Left exception => pure $ Left $ believe_me exception
136
148
Right size => pure $ Right $ Long . intValue size
137
- fileSize _ = pure . Left . believe_me $ ! (IOException . new " Cannot determine size for stdin/stdout/stderr " )
149
+ fileSize _ = pure . Left . believe_me $ ! (IOException . new " Cannot determine size" )
138
150
139
151
export
140
152
fflush : File -> JVM_IO ()
141
- fflush (MkFile file _ ) = invokeInstance " force " ( FileChannel -> Bool -> JVM_IO () ) file True
153
+ fflush (MkFile file) = FileChannelIo . flush file
142
154
fflush MkFileStdout = invokeStatic RuntimeClass " flushStdout" (JVM_IO () )
143
155
fflush MkFileStderr = invokeStatic RuntimeClass " flushStderr" (JVM_IO () )
144
156
fflush _ = pure ()
@@ -161,61 +173,38 @@ stderr = MkFileStderr
161
173
export
162
174
total
163
175
readFile : String -> JVM_IO (Either FileError String)
164
- readFile pathStr = assert_total $ do
165
- path <- invokeStatic RuntimeClass " createPath" (String -> JVM_IO Path ) pathStr
166
- exceptionOrlines <- (filesClass <.> " lines(java/nio/file/Path)" ) path
167
- case exceptionOrlines of
168
- Left exception => pure $ Left exception
169
- Right Nothing => pure $ Left . believe_me $ ! (unableToReadFile pathStr)
170
- Right (Just lines ) => do
171
- joiningCollector <- (collectorsClass <.!> " joining(java/lang/CharSequence)" ) $ believe_me ! (lineSeparator)
172
- pure . Right $ believe_me ! ((streamClass <.!> " collect(java/util/stream/Collector)" ) lines joiningCollector)
176
+ readFile pathStr = assert_total $ Files . readFile pathStr
173
177
174
178
export
175
179
writeFile : String -> String -> JVM_IO (Either FileError () )
176
- writeFile file content = do
177
- path <- invokeStatic RuntimeClass " createPath" (String -> JVM_IO Path ) file
178
- bytes <- (stringClass <.!> " getBytes(java/lang/String)" ) content " UTF-8"
179
- exceptionOrPath <- (filesClass <.> " write(java/nio/file/Path,Array 1 byte,Array 1 i java/nio/file/OpenOption)" ) path
180
- bytes ! (newArray OpenOption 0 )
181
- pure $ either (const . Left . believe_me $ ! (unableToWriteFile file)) (const $ Right () ) exceptionOrPath
180
+ writeFile file content = Files . writeFile file content
182
181
183
182
export
184
183
fileModifiedTime : File -> JVM_IO (Either FileError Integer)
185
- fileModifiedTime (MkFile _ path) = do
186
- attrs <- (filesClass <.!> " readAttributes(java/nio/file/Path,java/lang/Class,Array 1 java/nio/file/LinkOption)" )
187
- path (classLit basicFileAttributesClass) ! (newArray LinkOption 0 )
188
- fileTime <- invokeInstance " lastModifiedTime" (BasicFileAttributes -> JVM_IO FileTime ) attrs
189
- seconds <- (fileTimeClass <.!> " to" ) fileTime TimeUnit . seconds
190
- pure . Right $ believe_me $ BigInteger . valueOf seconds
191
- fileModifiedTime _ = pure . Left $ believe_me ! (IOException . new " Cannot get file modified time for stdin, stdout or stderr" )
184
+ fileModifiedTime (MkFile file) = FileChannelIo . getFileModifiedTime file
185
+ fileModifiedTime _ = pure . Left $ believe_me ! (IOException . new " Cannot get file modified time" )
192
186
193
187
export
194
188
fileAccessTime : File -> JVM_IO (Either FileError Integer)
195
- fileAccessTime (MkFile _ path) = do
196
- attrs <- (filesClass <.!> " readAttributes(java/nio/file/Path,java/lang/Class,Array 1 java/nio/file/LinkOption)" )
197
- path (classLit basicFileAttributesClass) ! (newArray LinkOption 0 )
198
- fileTime <- invokeInstance " lastAccessTime" (BasicFileAttributes -> JVM_IO FileTime ) attrs
199
- seconds <- (fileTimeClass <.!> " to" ) fileTime TimeUnit . seconds
200
- pure . Right $ believe_me $ BigInteger . valueOf seconds
201
- fileAccessTime _ = pure . Left $ believe_me ! (IOException . new " Cannot get file access time for stdin, stdout or stderr" )
189
+ fileAccessTime (MkFile file) = FileChannelIo . getFileAccessTime file
190
+ fileAccessTime _ = pure . Left $ believe_me ! (IOException . new " Cannot get file access time" )
202
191
203
192
export
204
193
fileStatusTime : File -> JVM_IO (Either FileError Integer)
205
- fileStatusTime (MkFile _ path) = do
206
- attrs <- (filesClass <.!> " readAttributes(java/nio/file/Path,java/lang/Class,Array 1 java/nio/file/LinkOption)" )
207
- path (classLit basicFileAttributesClass) ! (newArray LinkOption 0 )
208
- fileTime <- invokeInstance " creationTime" (BasicFileAttributes -> JVM_IO FileTime ) attrs
209
- seconds <- (fileTimeClass <.!> " to" ) fileTime TimeUnit . seconds
210
- pure . Right $ believe_me $ BigInteger . valueOf seconds
211
- fileStatusTime _ = pure . Left $ believe_me ! (IOException . new " Cannot get file status time for stdin, stdout or stderr" )
194
+ fileStatusTime (MkFile file) = FileChannelIo . getFileStatusTime file
195
+ fileStatusTime _ = pure . Left $ believe_me ! (IOException . new " Cannot get file status time" )
212
196
213
197
||| Check if a file handle has reached the end
214
198
export
215
199
fEOF : File -> JVM_IO Bool
216
- fEOF (MkFile file _ ) = do
217
- position <- invokeInstance " position" (FileChannel -> JVM_IO Bits64 ) file
218
- size <- invokeInstance " size" (FileChannel -> JVM_IO Bits64 ) file
219
- pure (position == size)
200
+ fEOF (MkFile file) = FileChannelIo . isEof file
220
201
fEOF MkFileStdin = invokeStatic RuntimeClass " isStdinEof" (JVM_IO Bool )
221
202
fEOF _ = pure False
203
+
204
+ export
205
+ socketListenAndAccept : Int -> JVM_IO (Either String File )
206
+ socketListenAndAccept port = do
207
+ byteBufferIoOrError <- ClientServerSocket . listenAndAccept port
208
+ case byteBufferIoOrError of
209
+ Left throwable => Left <$> Objects . toString throwable
210
+ Right byteBufferIo => pure . Right $ MkFileClientServerSocket byteBufferIo
0 commit comments