
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.sql.*;
import java.util.Arrays;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class Main {
    public static void main(String [] args) {
        int[] sizes = new int[] {512, 2048, 16000, 100000, 250000, 500000, 1000000};

        for (int b = 0; b < 256; b ++) {
            for (int size : sizes) {
                byte[] buf = new byte[size];
                Arrays.fill(buf, (byte) b);
                InputStream is = new ByteArrayInputStream(buf);

                try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:4007/testj?user=root&enablePacketDebug=true")) {
                    Statement stmt = connection.createStatement();
                    stmt.execute("DROP TABLE IF EXISTS bintest4");
                    stmt.execute("CREATE TABLE bintest4(bin1 longblob)");
                    try (PreparedStatement ps = connection.prepareStatement("insert into bintest4 (bin1) values (?)")) {
                        ps.setBinaryStream(1, is);
                        ps.execute();
                    }

                    ResultSet rs = stmt.executeQuery("select bin1 from bintest4");
                    assertTrue(rs.next());
                    byte[] buf2 = rs.getBytes(1);
                    assertEquals(size, buf2.length);
                    for (int i = 0; i < size; i++) {
                        assertEquals(buf[i], buf2[i]);
                    }
                } catch (Exception e) {
                    System.err.println("ERROR with byte " + b + " size " + size);
                    e.printStackTrace();
                }
            }
        }
        //will throw errors (connection reset)
        //ERROR with byte 1 size 1000000
        //ERROR with byte 17 size 100000


    }

}
