import "Prelude" hiding (readInteger, readLong, readFloat, readDouble, read) import "Stream" import "Vector" import "ArrayList" as ArrayList class IO a where write :: OutputStream -> a -> () read :: InputStream -> a ioSize :: a -> Integer ioSize _ = fail "ioSize not implemented" instance IO Boolean where write = writeBoolean read = readBoolean ioSize _ = 1 instance IO Byte where write = writeByte read = readByte ioSize _ = 1 instance IO Character where write = writeCharacter read = readCharacter ioSize _ = 2 instance IO Short where write = writeShort read = readShort ioSize _ = 2 instance IO Integer where write = writeInteger read = readInteger ioSize _ = 4 instance IO Long where write = writeLong read = readLong ioSize _ = 8 instance IO Float where write = writeFloat read = readFloat ioSize _ = 4 instance IO Double where write = writeDouble read = readDouble ioSize _ = 8 instance IO String where write = writeString read = readString ioSize = ioSizeString instance (IO a) => IO (Maybe a) where read s = if read s then Just (read s) else Nothing write s (Just v) = do write s True write s v write s Nothing = do write s False ioSize Nothing = 1 ioSize (Just v) = 1 + ioSize v instance (IO a, IO b) => IO (Either a b) where read s = if read s then Right (read s) else Left (read s) write s (Left v) = do write s False write s v write s (Right v) = do write s True write s v ioSize (Left v) = 1 + ioSize v ioSize (Right v) = 1 + ioSize v instance IO () where read s = () write s _ = () ioSize _ = 0 instance (IO a, IO b) => IO (a, b) where read s = do a = read s b = read s (a, b) write s (a, b) = do write s a write s b ioSize (a, b) = ioSize a + ioSize b instance (IO a, IO b, IO c) => IO (a, b, c) where read s = do a = read s b = read s c = read s (a, b, c) write s (a, b, c) = do write s a write s b write s c ioSize (a, b, c) = ioSize a + ioSize b + ioSize c instance (IO a, IO b, IO c, IO d) => IO (a, b, c, d) where read s = do a = read s b = read s c = read s d = read s (a, b, c, d) write s (a, b, c, d) = do write s a write s b write s c write s d ioSize (a, b, c, d) = ioSize a + ioSize b + ioSize c + ioSize d instance (IO a, IO b, IO c, IO d, IO e) => IO (a, b, c, d, e) where read s = do a = read s b = read s c = read s d = read s e = read s (a, b, c, d, e) write s (a, b, c, d, e) = do write s a write s b write s c write s d write s e ioSize (a, b, c, d, e) = ioSize a + ioSize b + ioSize c + ioSize d + ioSize e instance (IO a) => IO [a] where write s v = do writeLength s (length v) for v (write s) read s = ArrayList.freeze (read s) ioSize vs = ioSizeLength (length vs) + sum [ioSize v | v <- vs] instance (IO a) => IO (ArrayList.T a) where write s v = do writeLength s (ArrayList.length v) ArrayList.for v (write s) read s = do l = readLength s result = ArrayList.newC l forN l (\_ -> ArrayList.add result (read s) ) result // TODO instance (IO a, VecComp a) => IO (Vector a) where @inline write s v = do l = length v writeLength s l forN l (\i -> write s (v ! i)) @inline read s = do l = readLength s result = createMVector l forN l (\i -> setMVector result i (read s)) freezeMVector result // TODO readByteArray :: IO a => ByteArray -> a readByteArray ar = runProc do s = openForReading ar v = read s close s v writeByteArray :: IO a => a -> ByteArray writeByteArray v = runProc do b = createBuffer () s = openForWriting b write s v close s toByteArray b importJava "org.simantics.scl.runtime.io.SclIO" where writeBoolean :: OutputStream -> Boolean -> () writeByte :: OutputStream -> Byte -> () writeCharacter :: OutputStream -> Character -> () writeShort :: OutputStream -> Short -> () writeInteger :: OutputStream -> Integer -> () writeLong :: OutputStream -> Long -> () writeFloat :: OutputStream -> Float -> () writeDouble :: OutputStream -> Double -> () writeDoubleArray :: OutputStream -> DoubleArray -> () writeString :: OutputStream -> String -> () writeLength :: OutputStream -> Integer -> () readBoolean :: InputStream -> Boolean readByte :: InputStream -> Byte readCharacter :: InputStream -> Character readShort :: InputStream -> Short readInteger :: InputStream -> Integer readLong :: InputStream -> Long readFloat :: InputStream -> Float readDouble :: InputStream -> Double readDoubleArray :: InputStream -> DoubleArray readString :: InputStream -> String readLength :: InputStream -> Integer ioSizeString :: String -> Integer ioSizeLength :: Integer -> Integer @JavaName readAllByteArrayAndClose readAllByteArray :: InputStream -> ByteArray @JavaName readAllStringAndClose readAllString :: InputStream -> String