int fetch_next_packet(Packet p) {
        int result;
        byte[] buffer;
        int index;
        int bytes;

        result = os.packetout(p);

        if (result > 0) {
            return 1;
        }

        while (oy.pageout(og) <= 0) {
            index = oy.buffer(CHUNKSIZE);
            buffer = oy.data;
            try {
                bytes = in.read(buffer, index, CHUNKSIZE);
            } catch (Exception e) {
                System.err.println(e);
                return 0;
            }
            if (bytes > 0) {
                oy.wrote(bytes);
            }
            if (bytes == 0 || bytes == -1) {
                return 0;
            }
        }
        os.pagein(og);

        return fetch_next_packet(p);
    }