package ghcvm.runtime;

import java.io.PrintStream;
import java.nio.ByteBuffer;
import java.util.ListIterator;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import static java.nio.charset.StandardCharsets.US_ASCII;

public class Utils {
    public static <E> E peekPrevious(ListIterator<E> it) {
        E e = null;
        if (it.hasPrevious()) {
            e = it.previous();
            it.next();
        }
        return e;
    }

    public static <E> E peekNext(ListIterator<E> it) {
        E e = null;
        if (it.hasNext()) {
            e = it.next();
            it.previous();
        }
        return e;
    }

    public static int c_isatty(int tty) {
        return System.console() != null ? 1 : 0;
    }

    private static PrintStream getPrintStream(int file) {
        switch (file) {
        case 0:
            return System.out;
        case 1:
            return System.err;
        default:
            throw new IllegalArgumentException("Invalid file descriptor");
        }
    }

    public static long c_write(int file, ByteBuffer buffer, long count) {
        try {
            PrintStream stream = getPrintStream(file);
            byte[] dst = new byte[(int) count];
            buffer.get(dst, 0, (int) count);
            stream.print(new String(dst, US_ASCII));
            return 0;
        } catch (IllegalArgumentException ignored) {
            return -1;
        }
    }

    public static String byteBufferToStr(ByteBuffer buffer)
        throws UnsupportedEncodingException {
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);
        return new String(bytes, "UTF-8");
    }

    public static void printBuffer(ByteBuffer buffer) {
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);
        System.out.println(Arrays.toString(bytes));
    }
}

@@ -177,66 +177,8 @@
 ays;
-%0Aimport static java.nio.charset.StandardCharsets.US_ASCII;
 %0A%0Apu
@@ -1252,16 +1252,18 @@
 st, 
+%22
 US
-_
+-
 ASCII
+%22
 ));%0A
@@ -1281,17 +1281,101 @@
  return 
-0
+count;%0A        %7D catch (UnsupportedEncodingException ignored) %7B%0A            return -1
 ;%0A      

Fix bug where c_write said it wrote 0 bytes