@Override
    public String readString(String encoding, int length) throws IOException
    {
        Charset charset = Charset.forName(encoding);
        CharsetDecoder dec = charset.newDecoder();
        ByteBuffer bin = ByteBuffer.wrap(buffer);
        CharBuffer bout = CharBuffer.allocate(1);
        String ret = "";
        
        bin.position(curPosition);
        for (int i = 0; length == 0 || i < length; i++)
        {
            CoderResult res = dec.decode(bin, bout, false);
            if (res == CoderResult.UNDERFLOW)
                break;
            else if (res != CoderResult.OVERFLOW)
                break;  // this is an ugly hack. We shouldn't be ignoring errors like that.
                        // but it seems that the ASCII charset decoder is too dumb to not read more
                        // than one byte when meeting the NULL terminator, making it occasionally
                        // throw UNMAPPABLE/MALFORMED errors depending on what follows the string
                        // the same issue occured with Shift-JIS but perhaps that is normal, I don't
                        // know about that encoding's internals
            //    throw new IOException(String.format("Error while reading string @ %1$X", curPosition));
            
            char ch = bout.get(0);
            if (ch == '\0') break;
            
            bout.clear();
            ret += ch;
        }
        
        return ret;
    }