--- /dev/null
+import "Prelude" hiding (readInteger, readLong, readFloat, readDouble, read)
+import "Stream"
+import "Vector"
+import "ArrayList" as ArrayList
+
+class IO a where
+ write :: OutputStream -> a -> <Proc> ()
+ read :: InputStream -> <Proc> 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 DoubleArray where
+ write = writeDoubleArray
+ read = readDoubleArray
+ //ioSize v = do
+ // l = length v
+ // ioSizeLength l + 8 * l
+
+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 -> <Proc> ()
+ writeByte :: OutputStream -> Byte -> <Proc> ()
+ writeCharacter :: OutputStream -> Character -> <Proc> ()
+ writeShort :: OutputStream -> Short -> <Proc> ()
+ writeInteger :: OutputStream -> Integer -> <Proc> ()
+ writeLong :: OutputStream -> Long -> <Proc> ()
+ writeFloat :: OutputStream -> Float -> <Proc> ()
+ writeDouble :: OutputStream -> Double -> <Proc> ()
+ writeDoubleArray :: OutputStream -> DoubleArray -> <Proc> ()
+ writeString :: OutputStream -> String -> <Proc> ()
+ writeLength :: OutputStream -> Integer -> <Proc> ()
+
+ readBoolean :: InputStream -> <Proc> Boolean
+ readByte :: InputStream -> <Proc> Byte
+ readCharacter :: InputStream -> <Proc> Character
+ readShort :: InputStream -> <Proc> Short
+ readInteger :: InputStream -> <Proc> Integer
+ readLong :: InputStream -> <Proc> Long
+ readFloat :: InputStream -> <Proc> Float
+ readDouble :: InputStream -> <Proc> Double
+ readDoubleArray :: InputStream -> <Proc> DoubleArray
+ readString :: InputStream -> <Proc> String
+ readLength :: InputStream -> <Proc> Integer
+
+ ioSizeString :: String -> Integer
+ ioSizeLength :: Integer -> Integer
+
+ @JavaName readAllByteArrayAndClose
+ readAllByteArray :: InputStream -> <Proc> ByteArray
+ @JavaName readAllStringAndClose
+ readAllString :: InputStream -> <Proc> String
\ No newline at end of file