Non symmetric java compression - java
I have a data sample:
byte[] b = new byte[]{120, 1, -67, -107, -51, 106, 20, 81, 16, -123, 107, 18, -51, -60, 31, -30, 117, -4, -53, -60, -123, 25, 70, 71, 23, -111, 89, 12, 8, -83, 49, 4, -14, -93, -63, 73, 32, 89, -55, -112, -123, 10, -30, 66, 69, -110, 69, -64, -107, -77, 8, -72, 21, 23, -82, 5, -97, -64, 55, -48, -73, -16, 97, 4, -3, 14, -23, -110, 75, 59, 125, 39, 8, -10, -123, 51, -73, -86, -85, -6, 84, -99, -22, -18, 59, 53, 51, 27, 2, 95, 7, 24, 95, -36, 97, 95, 9, 102, -17, 46, -101, -51, -81, 109, -82, -101, -43, 44, -100, 54, -5, -56, -11, 9, -128, 105, -81, -128, -42, -25, -109, 102, -121, -109, 102, 63, 107, 102, 75, 32, 94, 79, -66, -43, 109, -15, -57, 9, 91, -79, 55, -74, 111, -49, -71, 103, -51, 54, 13, 58, -42, 121, 112, 22, 76, 3, -15, 5, -32, -21, 20, 70, 0, -94, 19, -58, -59, -59, 19, -128, -81, 51, 24, 1, -8, -3, 23, -80, -107, 35, -1, 38, -104, -51, 109, 54, 123, -12, -6, -67, 54, -69, 1, 60, -57, 109, 122, 27, -34, -29, -70, 122, -68, 10, 2, 80, 111, -102, -45, 29, 16, -64, 1, 40, -6, 15, -71, -26, -15, 45, -20, 103, 5, -1, 65, -28, 95, -57, 126, 90, -16, -69, -111, -33, -64, 46, -6, 47, -93, -72, -6, -39, 3, -9, -127, -6, 92, -52, -9, 37, -10, 89, -96, -72, -18, -9, 62, 93, 91, 29, 109, 110, 19, 30, -34, -30, -57, -11, 23, 103, -12, -31, -21, 119, -94, -57, -97, 81, 32, -9, 54, 120, 12, -60, -21, -21, 87, -66, 120, 93, -84, 73, 70, -117, -89, -47, -78, -7, 100, 94, 11, 21, 77, 107, 48, -21, 70, 50, -81, 78, -4, 72, 113, -102, 79, 111, 64, -99, -38, 1, -108, -51, 76, -3, -118, -83, 5, -92, -61, -25, -12, 63, 103, -42, -50, -21, -92, 102, -74, 64, 39, 61, -69, 6, -82, 36, 103, -47, 67, -35, 2, 95, 77, 27, -92, -8, -26, -120, 31, 77, 54, -51, -41, -96, -26, 28, -75, -37, -96, 108, 102, -102, -105, -66, -45, 94, -82, -93, -118, -103, 101, -44, -46, -5, -107, -46, -72, 74, 70, -97, 39, -39, -25, 61, 74, -27, -11, -19, -100, -83, -14, 5, 101, 32, -107, -41, 37, -34, 33, -73, 51, -122, -81, 67, -51, 46, -75, 51, 80, 54, -77, 14, -67, 79, -125, 126, -82, -93, -118, -103, 109, 80, 75, 103, 89, 74, -29, 14, 25, 3, -69, 4, 102, -110, 121, 3, -101, -78, 29, -72, -60, -103, -30, 91, 38, -98, -111, -101, -115, -31, -53, -88, -71, 76, -19, 13, 80, 54, 51, 61, 115, -83, 1, -112, -114, 127, -103, 25, -57, -112, 40, -2, -36, 91, -117, 108, -98, -57, 95, 103, 126, -109, -8, 39, -32, 103, -70, -50, -20, -94, -1, 54, -118, 43, 95, 126, -103, 6, -113, 59, 79, 21, 26, -12, 31, 16, -9, 124, 119, -124, 31, 107, 80, 126, 74, -125, -57, -99, -89, 10, 13, -21, -123, -98, -73, 71, -8, -79, 6, -27, -89, 52, 120, -36, 121, -86, -48, -80, 91, -24, -7, -59, 8, 63, -42, -96, -4, -108, 6, -113, 59, -49, 113, 52, -52, -64, 121, 17, 104, 5, 32, -5, 55, -108, 74, -2, -44};
it's just a compressed EMF image. I try to decompress it by code:
InflaterInputStream iis = new InflaterInputStream(new ByteArrayInputStream(b));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
int length = 0;
while ((length = iis.read(buf)) > 0) {
baos.write(buf, 0, length);
}
and get a CORRECT answer
[1, 0, 0, 0, -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 118, 0, 0, 0, -93, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 16, 0, 0, 127, 22, 0, 0, 32, 69, 77, 70, 0, 0, 1, 0, 16, 10, 0, 0, -110, 0, 0, 0, 2, 0, 0, 0, 10, 0, 0, 0, 108, 0, 0, 0, 0, 0, 0, 0, -96, 5, 0, 0, -124, 3, 0, 0, -4, 1, 0, 0, 62, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, -64, 7, 0, 60, -40, 4, 0, 67, 0, 111, 0, 114, 0, 101, 0, 108, 0, 69, 0, 77, 0, 70, 0, 0, 0, 0, 0, 17, 0, 0, 0, 12, 0, 0, 0, 8, 0, 0, 0, 10, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 10, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 12, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 19, 0, 0, 0, 12, 0, 0, 0, 1, 0, 0, 0, 39, 0, 0, 0, 24, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 75, 109, -121, 0, 0, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 1, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 8, 0, 0, -128, 59, 0, 0, 0, 8, 0, 0, 0, 27, 0, 0, 0, 16, 0, 0, 0, 9, 0, 0, 0, -93, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 118, 0, 0, 0, -93, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 118, 0, 0, 0, 72, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 78, 0, 0, 0, 98, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 78, 0, 0, 0, 71, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 36, 0, 0, 0, 97, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 36, 0, 0, 0, 47, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 18, 0, 0, 0, 47, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 18, 0, 0, 0, 107, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 9, 0, 0, 0, 113, 0, 0, 0, 61, 0, 0, 0, 8, 0, 0, 0, 60, 0, 0, 0, 8, 0, 0, 0, 62, 0, 0, 0, 24, 0, 0, 0, 9, 0, 0, 0, 47, 0, 0, 0, 118, 0, 0, 0, -93, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 7, 0, 0, -128, 37, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, -128, 40, 0, 0, 0, 12, 0, 0, 0, 1, 0, 0, 0, 39, 0, 0, 0, 24, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -112, -79, -63, 0, 0, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 1, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 8, 0, 0, -128, 59, 0, 0, 0, 8, 0, 0, 0, 27, 0, 0, 0, 16, 0, 0, 0, 16, 0, 0, 0, 45, 0, 0, 0, 88, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 3, 0, 0, 0, 25, 0, 45, 0, 33, 0, 39, 0, 33, 0, 32, 0, 88, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 3, 0, 0, 0, 33, 0, 24, 0, 25, 0, 18, 0, 16, 0, 18, 0, 88, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 3, 0, 0, 0, 7, 0, 18, 0, 0, 0, 24, 0, 0, 0, 32, 0, 88, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 3, 0, 0, 0, 0, 0, 39, 0, 7, 0, 45, 0, 16, 0, 45, 0, 61, 0, 0, 0, 8, 0, 0, 0, 60, 0, 0, 0, 8, 0, 0, 0, 62, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 18, 0, 0, 0, 33, 0, 0, 0, 45, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 7, 0, 0, -128, 37, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, -128, 40, 0, 0, 0, 12, 0, 0, 0, 1, 0, 0, 0, 39, 0, 0, 0, 24, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -112, -79, -63, 0, 0, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 1, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 8, 0, 0, -128, 59, 0, 0, 0, 8, 0, 0, 0, 27, 0, 0, 0, 16, 0, 0, 0, 35, 0, 0, 0, 37, 0, 0, 0, 88, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 3, 0, 0, 0, 44, 0, 37, 0, 51, 0, 31, 0, 51, 0, 23, 0, 88, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 3, 0, 0, 0, 51, 0, 16, 0, 44, 0, 10, 0, 35, 0, 10, 0, 88, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 3, 0, 0, 0, 26, 0, 10, 0, 18, 0, 16, 0, 18, 0, 23, 0, 88, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 3, 0, 0, 0, 18, 0, 31, 0, 26, 0, 37, 0, 35, 0, 37, 0, 61, 0, 0, 0, 8, 0, 0, 0, 60, 0, 0, 0, 8, 0, 0, 0, 62, 0, 0, 0, 24, 0, 0, 0, 18, 0, 0, 0, 10, 0, 0, 0, 51, 0, 0, 0, 37, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 7, 0, 0, -128, 37, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, -128, 40, 0, 0, 0, 12, 0, 0, 0, 1, 0, 0, 0, 39, 0, 0, 0, 24, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -112, -79, -63, 0, 0, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 1, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 8, 0, 0, -128, 59, 0, 0, 0, 8, 0, 0, 0, 27, 0, 0, 0, 16, 0, 0, 0, 57, 0, 0, 0, 40, 0, 0, 0, 88, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 3, 0, 0, 0, 68, 0, 40, 0, 76, 0, 33, 0, 76, 0, 24, 0, 88, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 3, 0, 0, 0, 76, 0, 15, 0, 68, 0, 8, 0, 57, 0, 8, 0, 88, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 3, 0, 0, 0, 47, 0, 8, 0, 38, 0, 15, 0, 38, 0, 24, 0, 88, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 3, 0, 0, 0, 38, 0, 33, 0, 47, 0, 40, 0, 57, 0, 40, 0, 61, 0, 0, 0, 8, 0, 0, 0, 60, 0, 0, 0, 8, 0, 0, 0, 62, 0, 0, 0, 24, 0, 0, 0, 38, 0, 0, 0, 8, 0, 0, 0, 76, 0, 0, 0, 40, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 7, 0, 0, -128, 37, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, -128, 40, 0, 0, 0, 12, 0, 0, 0, 1, 0, 0, 0, 39, 0, 0, 0, 24, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -112, -79, -63, 0, 0, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 1, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 8, 0, 0, -128, 59, 0, 0, 0, 8, 0, 0, 0, 27, 0, 0, 0, 16, 0, 0, 0, 73, 0, 0, 0, 27, 0, 0, 0, 88, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 3, 0, 0, 0, 82, 0, 27, 0, 90, 0, 21, 0, 90, 0, 14, 0, 88, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 3, 0, 0, 0, 90, 0, 6, 0, 82, 0, 0, 0, 73, 0, 0, 0, 88, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 3, 0, 0, 0, 64, 0, 0, 0, 57, 0, 6, 0, 57, 0, 14, 0, 88, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 3, 0, 0, 0, 57, 0, 21, 0, 64, 0, 27, 0, 73, 0, 27, 0, 61, 0, 0, 0, 8, 0, 0, 0, 60, 0, 0, 0, 8, 0, 0, 0, 62, 0, 0, 0, 24, 0, 0, 0, 57, 0, 0, 0, 0, 0, 0, 0, 90, 0, 0, 0, 27, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 7, 0, 0, -128, 37, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, -128, 40, 0, 0, 0, 12, 0, 0, 0, 1, 0, 0, 0, 39, 0, 0, 0, 24, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 1, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 8, 0, 0, -128, 59, 0, 0, 0, 8, 0, 0, 0, 27, 0, 0, 0, 16, 0, 0, 0, 25, 0, 0, 0, -106, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 36, 0, 0, 0, -106, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 36, 0, 0, 0, 121, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 25, 0, 0, 0, 121, 0, 0, 0, 61, 0, 0, 0, 8, 0, 0, 0, 60, 0, 0, 0, 8, 0, 0, 0, 62, 0, 0, 0, 24, 0, 0, 0, 25, 0, 0, 0, 121, 0, 0, 0, 36, 0, 0, 0, -106, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 7, 0, 0, -128, 37, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, -128, 40, 0, 0, 0, 12, 0, 0, 0, 1, 0, 0, 0, 39, 0, 0, 0, 24, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 1, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 8, 0, 0, -128, 59, 0, 0, 0, 8, 0, 0, 0, 27, 0, 0, 0, 16, 0, 0, 0, 47, 0, 0, 0, -106, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 58, 0, 0, 0, -106, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 58, 0, 0, 0, 121, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 47, 0, 0, 0, 121, 0, 0, 0, 61, 0, 0, 0, 8, 0, 0, 0, 60, 0, 0, 0, 8, 0, 0, 0, 62, 0, 0, 0, 24, 0, 0, 0, 47, 0, 0, 0, 121, 0, 0, 0, 58, 0, 0, 0, -106, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 7, 0, 0, -128, 37, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, -128, 40, 0, 0, 0, 12, 0, 0, 0, 1, 0, 0, 0, 39, 0, 0, 0, 24, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 1, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 8, 0, 0, -128, 59, 0, 0, 0, 8, 0, 0, 0, 27, 0, 0, 0, 16, 0, 0, 0, 70, 0, 0, 0, -106, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 81, 0, 0, 0, -106, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 81, 0, 0, 0, 121, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 70, 0, 0, 0, 121, 0, 0, 0, 61, 0, 0, 0, 8, 0, 0, 0, 60, 0, 0, 0, 8, 0, 0, 0, 62, 0, 0, 0, 24, 0, 0, 0, 70, 0, 0, 0, 121, 0, 0, 0, 81, 0, 0, 0, -106, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 7, 0, 0, -128, 37, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, -128, 40, 0, 0, 0, 12, 0, 0, 0, 1, 0, 0, 0, 39, 0, 0, 0, 24, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 1, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 8, 0, 0, -128, 59, 0, 0, 0, 8, 0, 0, 0, 27, 0, 0, 0, 16, 0, 0, 0, 92, 0, 0, 0, -106, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 103, 0, 0, 0, -106, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 103, 0, 0, 0, 121, 0, 0, 0, 54, 0, 0, 0, 16, 0, 0, 0, 92, 0, 0, 0, 121, 0, 0, 0, 61, 0, 0, 0, 8, 0, 0, 0, 60, 0, 0, 0, 8, 0, 0, 0, 62, 0, 0, 0, 24, 0, 0, 0, 92, 0, 0, 0, 121, 0, 0, 0, 103, 0, 0, 0, -106, 0, 0, 0, 37, 0, 0, 0, 12, 0, 0, 0, 7, 0, 0, -128, 37, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, -128, 40, 0, 0, 0, 12, 0, 0, 0, 1, 0, 0, 0, 14, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 20, 0, 0, 0]
After that i'm try to compress it back by code:
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
//Deflater defalter = new Deflater(1);
DeflaterOutputStream dos = new DeflaterOutputStream(baos2);//deflater, false);
dos.write(baos.toByteArray(), 0 , baos.toByteArray().length);
dos.finish();
And get a result:
[120, -100, -67, 84, 77, 47, 3, 81, 20, 61, -29, -85, 45, 13, -29, 91, 89, -48, -108, -78, 32, -77, -112, 72, -22, -93, 105, -126, -46, 40, 9, 43, 105, 44, -112, -120, 5, 34, 44, 36, 86, -70, -112, -40, -118, -123, -75, -60, 47, -16, 15, -8, 23, 126, -116, -124, 51, -45, -5, -30, -103, -52, -68, -63, 98, 94, 114, -46, 119, 122, -49, -36, 123, -49, 25, 106, 1, -88, -29, -5, 92, 17, -49, 26, 95, -74, -127, -101, 1, 96, 108, 117, -77, 12, 88, -80, -37, -127, 7, 126, -33, 68, -16, -118, 83, -47, 61, -75, 2, -73, -51, -64, -121, 5, 20, 45, -4, 56, -5, -81, 9, 44, -66, -73, 96, 25, -25, -72, -60, 17, -97, 89, -59, 38, -54, 94, -83, -101, 72, 19, 73, -23, 103, 107, -49, -91, -124, 91, -126, -88, 122, -38, 87, -17, -16, -43, 123, 69, -29, -34, 39, -119, 33, -71, -69, 103, -29, -20, -50, -5, -100, -48, 52, -22, -50, -35, -22, 11, -78, -29, -120, -12, 76, 73, 78, 115, -62, -81, 2, -8, -70, -58, -73, -120, 67, 31, 95, -45, -8, 56, 113, -32, -29, -114, -58, 123, 2, -8, -119, -58, -35, 125, 46, -120, 37, -39, 115, 81, 62, -117, -30, 51, 37, -49, -85, 61, -107, -73, 4, -67, -87, 59, 79, 125, -54, -112, -47, -3, -53, -37, -97, 50, 114, 49, 67, -20, 18, 83, -38, 123, -7, -108, -61, 63, 23, 100, -88, -56, 114, 82, 22, 99, 70, 93, -106, -101, 100, -24, -38, 38, 76, -70, -124, -105, -116, -69, -75, -71, -97, -21, 46, -63, -39, 54, 17, -106, 25, 36, -25, -84, -8, -120, 35, -77, -100, -44, 77, -69, 79, 83, 49, -117, 81, 98, -48, -88, -101, 101, -57, 105, -2, -41, -28, 8, -109, 110, -104, -11, 70, -78, -26, 126, 61, -100, 57, -52, -39, 57, 34, 44, 51, 55, -81, 118, 111, 118, -61, 71, 28, -103, 21, 100, 103, -45, -18, 43, -84, 84, -7, 38, -85, -100, 100, -46, 85, -47, 69, 109, -110, 61, -109, 70, -99, -61, 122, -98, -38, 124, 68, -65, 60, 103, 58, -84, 22, -120, -80, -52, -14, -62, -85, -46, 39, -114, -52, 42, 114, 55, -19, -66, 67, 69, 13, -3, 68, -89, 81, 87, 67, 27, -75, -115, -98, 38, 93, -55, 123, 87, 109, -124, -71, 95, -127, 51, 75, -100, 93, 33, -62, 50, 43, -56, 115, 53, -15, -15, -97, -52, 56, -18, 79, -103, 101, -120, 71, -4, -4, -115, -10, -13, 107, -115, 103, -124, -121, 121, 80, 117, -43, 39, 14, 15, -114, 111, -25, -7, 0, -82, 123, 112, 34, 60, -88, -70, -22, 19, -121, -121, -78, 111, -25, -19, 0, -82, 123, 40, 71, 120, 80, 117, -43, 39, 14, 15, 123, -66, -99, -113, 3, -72, -18, 97, 47, -62, -125, -86, -85, 62, -65, -15, -48, 73, -12, -55, -2, -74, -36, -65, 0, -108, 74, -2, -44]
the more similar result was achieved when i uncomment deflater initialization and using in DeflateOutputStream constructor.
As for me it's very strange behaviour or i don't use inflate/deflate mechanism properly.
Any idea?
Lossless compressors, to be called such, assure that compression followed by decompression produces exactly the same thing as what you started with.
On the other hand, there is no assurance nor any need to assure that decompression followed by compression should always produce the same result. Compressors can comply with the compressed data format, but produce very different results on the same input data depending the algorithm used and parameters of the algorithm that are chosen. Compressors often have different compression "levels" that allow the user to spend more or less CPU compressing better or worse, depending on the application. So you will get different output with different settings.
Related
How can I convert image to arrays using Java code?
I'm trying to set up an MLKit detection project on Android using Tensorflow library, I have got false values on the output using Tensorflow lite ( different values than Frozen model inference ). I doubt that the problem is with the input ( image ), So I want to compare the two same images that I have. To do this task, I have used PIL image and numpy libraries on python to get bytes arrays, and I have converted the drawable image to bitmap and from bitmap to bytes arrays. I don't know if the function np.asarray should give the same value as those two functions below: Java code: private float[][][][] bitmapToInputArray() { // [START mlkit_bitmap_input] Bitmap bitmap= getYourInputImage(); int batchNum = 0; float[][][][] input = new float[1][112][112][3]; for (int x = 0; x < 112; x++) { for (int y = 0; y < 112; y++) { int pixel = bitmap.getPixel(x, y); // Normalize channel values to [-1.0, 1.0]. This requirement varies by // model. For example, some models might require values to be normalized // to the range [0.0, 1.0] instead. input[batchNum][x][y][0] = (Color.red(pixel))/ 255.0f; input[batchNum][x][y][1] = (Color.green(pixel)) / 255.0f; input[batchNum][x][y][2] = (Color.blue(pixel))/ 255.0f; Log.i("Input","input"+input[batchNum][x][y][0]); Log.i("input","input"+input[batchNum][x][y][1]); } } // [END mlkit_bitmap_input] return input; } public byte[] convertBitmapToByteArray(Bitmap bitmap) { ByteArrayOutputStream stream = null; try { stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); return stream.toByteArray(); }finally { if (stream != null) { try { stream.close(); } catch (IOException e) { Log.e(ThemedSpinnerAdapter.Helper.class.getSimpleName(), "ByteArrayOutputStream was not closed"); } } } } private Bitmap getYourInputImage() { // This method is just for show BitmapDrawable drawable = (BitmapDrawable) image2.getDrawable(); Bitmap bitmap = drawable.getBitmap(); Bitmap bitmapp=Bitmap.createScaledBitmap(bitmap,112,112,true); Bitmap bitmap2= bitmapp.copy(Bitmap.Config.ARGB_8888, true); return bitmap2; } byte[] bytes=convertBitmapToByteArray(bitmap1); Log.i("byte",""+ Arrays.toString(bytes)); float[][][][] inp = new float[1][112][112][3]; inp=bitmapToInputArray(); Log.i("byte2",""+Arrays.toString(inp[0])); Python script: img = Image.open("irisdata-300VW_Dataset_2015_12_14-017-000880.jpg") img.load() img = img.resize((112, 112), PIL.Image.ANTIALIAS) print(str(image_to_byte_array(img))) # Normalize to [0, 1] data = np.asarray( img, dtype="float32") print(data) Output of java code: 2019-08-20 19:01:13.589 1513-1513/com.example.irisdetection I/byte: [-1, -40, -1, -32, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, -1, -37, 0, 67, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -37, 0, 67, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -64, 0, 17, 8, 0, 112, 0, 112, 3, 1, 34, 0, 2, 17, 1, 3, 17, 1, -1, -60, 0, 29, 0, 0, 2, 2, 3, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 8, 6, 9, 4, 5, 10, 3, 2, 1, -1, -60, 0, 69, 16, 0, 1, 3, 3, 1, 5, 5, 4, 7, 5, 6, 5, 5, 0, 0, 0, 3, 1, 2, 4, 5, 6, 17, 33, 0, 7, 18, 19, 49, 8, 34, 65, 81, 97, 20, 35, 113, -127, 9, 21, 50, 51, -111, -95, -79, 36, 66, -63, -47, -16, 22, 23, 67, 82, 83, -31, 37, 52, 98, 114, -15, 99, 115, -126, -125, -94, -1, -60, 0, 28, 1, 0, 2, 2, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7, 4, 5, 2, 3, 8, 0, 1, -1, -60, 0, 55, 17, 0, 0, 4, 3, 6, 3, 8, 1, 3, 4, 3, 1, 0, 0, 0, 0, 1, 2, 3, 4, 5, 17, 33, 0, 6, 49, 65, 81, 97, 18, 19, 113, 7, 20, 35, -127, -111, -95, -79, -16, -47, 34, -63, -31, 21, 36, 67, -15, 8, 22, 50, 51, -1, -38, 0, 12, 3, 1, 0, 2, 17, 3, 17, 0, 63, 0, -28, -105, 115, 116, 26, 45, 98, -97, 117, 74, -71, -31, -57, -88, 40, -73, 93, 88, -83, 83, 89, 39, 41, -20, -43, 40, -7, 28, 115, 12, -100, -52, -116, -88, 78, 53, -45, 84, -49, -53, 102, 114, 124, 34, -52, -20, -3, -72, 91, -115, 6, -58, -120, -76, -5, -102, -126, 71, 36, -107, 43, -97, -11, 61, 93, -17, 78, 96, 63, 115, -35, -52, 8, -3, -30, -89, -40, -26, 117, -40, 29, -70, 107, 106, 21, 112, 86, -36, 7, 73, 36, 120, -9, 30, -19, -18, -48, -56, 86, -79, -88, -31, -106, -105, 36, -53, -53, 103, 30, -92, 121, 24, -120, 76, 47, -7, -15, -99, 54, 103, 44, -88, -107, 42, -57, 98, 107, 90, 83, -98, 105, 20, -21, 43, 126, 53, -117, 106, 58, -71, -116, 84, -114, 58, -11, 22, 53, 77, -84, 119, 47, -34, -14, -52, 72, 102, 127, -68, -57, -61, 77, -78, 126, -35, 18, -79, 112, 9, 38, 41, 40, 50, 3, 104, 63, -7, -64, 66, 97, -100, -66, -115, -115, 59, 43, -119, 21, 43, -50, -111, 79, 62, 82, -55, -72, 64, 70, -76, -92, -6, 5, 119, -54, -42, 15, -40, -126, -36, -81, 86, 109, 8, 55, 69, -62, 114, 31, -39, -30, 125, 65, 65, 9, 120, -47, -111, -24, -48, 36, -55, 56, -58, 49, -12, 103, 52, -14, 77, -24, -68, 12, -41, 24, -38, -49, 45, -6, 122, -80, -104, -106, -88, 32, -76, 92, 72, -67, 51, -13, 95, 92, -2, 105, -90, 112, -85, -1, 0, 101, -21, 110, -105, 72, -35, 117, -93, 21, -22, -48, 56, 84, 56, 50, 9, -62, -33, -74, 105, 35, -25, -99, 126, 42, 71, -89, -126, -86, -2, 91, 77, 119, -59, -67, 56, 86, 101, 29, -50, -116, -41, 28, -83, 78, 1, 2, 59, 85, 78, 103, -82, -120, -120, -119, -16, 84, -8, -81, -82, -43, -119, 2, 73, -74, -32, 34, 124, -95, -91, 103, -120, -56, 48, -108, -3, 55, 10, -128, 72, 44, 54, -11, 68, -43, -117, 63, 85, 41, 75, -67, -81, -115, 104, 11, -128, 98, 30, 84, -99, -90, 117, -103, -47, 13, 32, -111, 99, 59, 8, -30, 120, -85, 59, -38, -8, 42, 116, -50, -67, 51, -25, -116, 109, -17, 6, -128, 85, 123, 72, 34, 46, 60, 28, -72, -8, 39, 69, -49, -97, -13, -38, -77, -28, -17, 107, 125, -75, -103, -113, -97, 108, 110, -34, -19, -88, 0, 69, -53, 80, 84, -87, 74, -114, 69, 34, -82, -124, 81, -94, 97, 124, -4, -70, 109, 40, -94, 118, -112, -19, 67, 109, 76, -114, -54, -2, -25, 36, -118, -104, -30, 35, -122, -39, -111, -90, 10, 79, 39, 61, 73, -53, 69, -41, 25, 93, 116, -12, -38, -87, 70, -4, -59, 84, -83, 100, 30, 67, 47, -65, 103, 51, 120, 107, -94, 36, 68, -124, -63, -31, -56, 39, 44, -58, -127, -112, -53, 110, -125, -106, 118, 97, 30, -117, 46, 58, 100, -68, 46, 110, 123, -51, -41, -89, 84, -4, -4, -68, 126, 91, 123, -44, -30, 3, -39, -107, 80, 92, 92, 89, -18, -89, -34, 105, -29, -4, 83, -11, -38, 47, -70, -19, -17, 10, -4, -89, 8, -43, 90, 49, 40, -14, -36, 1, -72, -111, -56, -113, 86, 101, 90, -103, 31, 125, 62, -13, -29, -32, -102, -90, -60, -55, -43, 90, 28, 126, 23, 56, 106, -118, -115, -30, 114, 47, -106, 5 2019-08-20 19:01:13.590 1513-1513/com.example.irisdetection I/byte2: [[[F#8bc81c1, [[F#4123d66, [[F#d5d68a7, [[F#ed2a154, [[F#7ce78fd, [[F#c74c9f2, [[F#66de843, [[F#627ec0, [[F#50ea7f9, [[F#201933e, [[F#57cc59f, [[F#250c6ec, [[F#f88cab5, [[F#7ffa54a, [[F#d721cbb, [[F#1f965d8, Output of python code: How can I get the same output of the python code with Java code using bitmap as an input?
ArrayList changing when receiving info from socket Java
I am getting this bug which I just can't imagine the cause for. I have a receiver class which receives packets, within which is an ArrayList. No, the packets don't have an ArrayList; the class has one. When I receive a new packet, the ArrayList is altered, but before I alter it. In fact, before I even read the data from the packet into the object I am using the data. Not only that, but the ArrayList is changing all the objects within the array to the equivalent of the object which is created from the data in a later instruction... Method which is producing the weird behaviour: public void processPackets() { byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); try { while(true) { System.out.println("Start: " + outBuffer); socket.receive(packet); System.out.println("Before: " + outBuffer); // Find out where the packets are coming from if (sendHost == null) { sendHost = packet.getAddress(); sendPort = packet.getPort(); } // Grab the data from the packet PacketInfo data = new PacketInfo(packet.getData()); System.out.println("Found: " + data); if (this.packetWithinWindow(data.getSequenceNumber())) { outBuffer.add(data); System.out.println("After: " + outBuffer); } ackThePacket(data); if (processData()) {break;}; // Returns true after last packet processed System.out.println("End: " + outBuffer); } } catch (IOException ioe) { System.err.println(ioe); System.exit(1); } } Given that outBuffer is an ArrayList<PacketInfo> and PacketInfo.toString() returns a sequence number and a list of the first 20 bytes, this is a sample of the weird behaviour that is then printed to the console (after several cycles through the code where outBuffer starts empty): Start: [] Before: [] Found: 2 [-120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120] After: [ 2 [-120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120]] End: [ 2 [-120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120]] Start: [ 2 [-120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120, 8, 0, 2, -120]] Before: [ 1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 65, 0, 76, 0, 71, 68, 70, 68, 48]] Found: 1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 65, 0, 76, 0, 71, 68, 70, 68, 48] After: [ 1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 65, 0, 76, 0, 71, 68, 70, 68, 48], 1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 65, 0, 76, 0, 71, 68, 70, 68, 48]] End: [ 1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 65, 0, 76, 0, 71, 68, 70, 68, 48], 1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 65, 0, 76, 0, 71, 68, 70, 68, 48]] Start: [ 1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 65, 0, 76, 0, 71, 68, 70, 68, 48], 1 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 65, 0, 76, 0, 71, 68, 70, 68, 48]] Before: [ 4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] Found: 4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] After: [ 4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] End: [ 4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 4 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] This is perplexing me so any help is appreciated. If it's any help, this is compiled using Java 6.
Why canvas Draw slowly when real time plot values graph?
I want to draw line using sample values from array or real time plot values from external device. i almost got ans. But there is canvas not draw even speed from start to end point. after executing some data canvas almost dead slow. please suggest me what is the problem inside the ondraw method... public class WaveDrawing extends View { public final Paint backgroundColor = new Paint(Paint.ANTI_ALIAS_FLAG); public final Paint patientData = new Paint(Paint.ANTI_ALIAS_FLAG); public final Paint patientDataRePaint = new Paint(Paint.ANTI_ALIAS_FLAG); int i = 0; //k = 36; //graph point int[] a = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 0, 0, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 26, 40, 44, 48, 50, -25, -20, -15, -10, -5, -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 54, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, -50, -48, -46, -44, -42, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6, -7, -6, -6, -5, -5, -4, -4, -3, -3, -2, -2, -1, -1, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 0, 0, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 26, 40, 44, 48, 50, -25, -20, -15, -10, -5, -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 54, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, -50, -48, -46, -44, -42, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6, -7, -6, -6, -5, -5, -4, -4, -3, -3, -2, -2, -1, -1, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 0, 0, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 26, 40, 44, 48, 50, -25, -20, -15, -10, -5, -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 54, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, -50, -48, -46, -44, -42, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6, -7, -6, -6, -5, -5, -4, -4, -3, -3, -2, -2, -1, -1, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 0, 0, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 26, 40, 44, 48, 50, -25, -20, -15, -10, -5, -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 54, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, -50, -48, -46, -44, -42, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6, -7, -6, -6, -5, -5, -4, -4, -3, -3, -2, -2, -1, -1, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 0, 0, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 26, 40, 44, 48, 50, -25, -20, -15, -10, -5, -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 54, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, -50, -48, -46, -44, -42, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6, -7, -6, -6, -5, -5, -4, -4, -3, -3, -2, -2, -1, -1, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 0, 0, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 26, 40, 44, 48, 50, -25, -20, -15, -10, -5, -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 54, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, -50, -48, -46, -44, -42, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6, -7, -6, -6, -5, -5, -4, -4, -3, -3, -2, -2, -1, -1, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 0, 0, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 26, 40, 44, 48, 50, -25, -20, -15, -10, -5, -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 54, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, -50, -48, -46, -44, -42, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6, -7, -6, -6, -5, -5, -4, -4, -3, -3, -2, -2, -1, -1, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 0, 0, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 26, 40, 44, 48, 50, -25, -20, -15, -10, -5, -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 54, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, -50, -48, -46, -44, -42, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6, -7, -6, -6, -5, -5, -4, -4, -3, -3, -2, -2, -1, -1, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 0, 0, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 26, 40, 44, 48, 50, -25, -20, -15, -10, -5, -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 54, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, -50, -48, -46, -44, -42, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6, -7, -6, -6, -5, -5, -4, -4, -3, -3, -2, -2, -1, -1, 0, 0, 0, 0 ,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 0, 0, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 26, 40, 44, 48, 50, -25, -20, -15, -10, -5, -0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 54, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, -50, -48, -46, -44, -42, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -20, -18, -16, -14, -12, -10, -8, -6, -4, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6, -7, -6, -6, -5, -5, -4, -4, -3, -3, -2, -2, -1, -1, 0, 0, 0, 0 }; //int pre, next; public WaveDrawing(Context context) { super(context); init(); } public WaveDrawing(Context context, AttributeSet attrs) { super(context, attrs); init(); } public WaveDrawing(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } public void init() { // backgroundColor.setColor(Color.BLACK); patientData.setColor(Color.GREEN); patientDataRePaint.setColor(Color.WHITE); patientData.setStrokeWidth(2); } #Override public synchronized void onDraw(Canvas canvas) { super.onDraw(canvas); int widthIterator = 0; //canvas.drawRect(0, 0, getWidth(), getHeight(), backgroundColor); if (i != 0) { for (int drawPulseIterator = 1; drawPulseIterator <= i; drawPulseIterator++) { if (widthIterator > getWidth() ) { widthIterator = 0; // k = widthIterator; } else { widthIterator += 1; } canvas.drawRect(widthIterator, 0, widthIterator + 7, getHeight(), patientDataRePaint); canvas.drawLine(widthIterator, (getHeight() / 2) - a[drawPulseIterator], widthIterator + 1, (getHeight() / 2) - a[drawPulseIterator - 1], patientData); } } i++; if (i == a.length) { i = 1; } invalidate(); } } XML <com.test.sample.wave.WaveDrawing android:layout_width="wrap_content" android:layout_height="wrap_content" />
In the onDraw method, there is an increase of loop iterations. This leads to the fact that it is necessary to do more work per pass rendering cycle. Also your animation depends on the device performance. So for more powerful devices, the animation will go faster.In general, using onDraw() and invalidate() to draw dynamic graphics - bad idea. For these purposes it is better to use the OpenGL ES. For the same performance on different devices, the animation is better to make time-dependent rendering between frames
There is another way. You can draw your pulse line in Bitmap. then draw bitmap on main Canvas: #Override public synchronized void onDraw(Canvas canvas) { super.onDraw(canvas); if (!mIsInited) { int w = getWidth(), h = getHeight(); Bitmap.Config conf = Bitmap.Config.ARGB_8888; mBitmap = Bitmap.createBitmap(w, h, conf); mCanvas = new Canvas(mBitmap); mCanvas.drawColor(Color.WHITE); mIsInited = true; } if (i != 0) { if (mWidthIterator > getWidth()) { mWidthIterator = 0; } else { mWidthIterator += 1; } mCanvas.drawRect(mWidthIterator, 0, mWidthIterator + 7, getHeight(), patientDataRePaint); mCanvas.drawLine(mWidthIterator, (getHeight() / 2) - a[i], mWidthIterator + 1, (getHeight() / 2) - a[i - 1], patientData); } canvas.drawBitmap(mBitmap, 0, 0, null); i++; if (i == a.length) { i = 1; mWidthIterator = 0; mCanvas.drawColor(Color.WHITE); } invalidate(); } Also, do not forget to make mWidthIterator, mCanvas and mBitmap as an instance variables.
Why do I keep an exception message when ever I run my code on Eclipse?
When ever I run this code on eclipse, it gives me this exception message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1000 at HW2.howManyTimesDoNumbersIncrease(HW2.java:221) at HW2.main(HW2.java:190) Please run on your programs tell me what's wrong with my program. PLEASE HELP! code public class HW2 { // Class variables // DO NOT CHANGE THE FOLLOWING ARRAY!! public static int[] first = { 3391, 3391, 3391, 0, -1036, -3676, -4840, 3798, 0, 0, -1350, -1350, 4320, -265, -265, 3222, 3222, -77, 0, 0, -3344, -3018, 1479, -2085, -2085, -2085, -2085, -2085, 0, 3205, -3775, 1165, 1165, 0, 0, 747, -1584, -3320, 3301, 0, -194, 1595, 1595, 0, 440, 722, 722, 722, 722, 3130, -2978, 4215, 4215, 4215, 766, 766, -157, -3206, 0, 0, -2630, 3348, 3348, 0, 0, -3942, -1545, 326, 326, 4901, -4048, 0, -1388, -1388, 0, 0, 0, 0, 4604, -2998, 0, -963, 0, 2555, -3578, 875, 410, 410, 2610, -4244, -4244, -4244, 3474, 4073, -3865, -1618, -467, 4630, -2874, -2874, 1660, 1660, 4236, 4236, 0, -1532, 2544, -1869, -1869, 0, -4064, 3383, -1648, -4642, -4090, -4090, -4090, 1611, -587, 0, 0, 0, 551, 551, 551, 551, 0, 1418, 2953, 3892, -547, -547, 1847, 1719, -3701, 2335, 3193, -4723, -3818, -3818, 1140, 1140, 4154, 3947, 0, 0, -2992, -2992, 4351, 4351, 213, 213, 213, -2177, 3029, 1275, -1086, -4651, -4651, 3380, 669, -3962, -3962, -3962, 0, 0, 0, 1764, 496, -4707, 1554, 4758, 0, 280, 3979, -4812, 1678, 43, -979, -2610, 725, 725, 4653, -2571, -2571, -4435, -4799, 4383, -2089, -2089, -2089, -2089, -2089, 4735, -3959, 3860, -3702, 2334, -3329, -3329, -3329, 3786, 3786, -2057, 4172, 4172, -787, 31, 31, 1702, 3410, 3410, 2216, -1739, 770, 770, -861, 2107, 2107, -2089, 2432, 2432, 2543, 3422, 2728, 2728, -1292, 3601, 0, 0, 1218, -1979, -2632, -2632, -214, -214, 0, -770, 4007, -4358, -1776, -867, -3334, -4020, 997, 3607, -794, 3828, 1740, 0, -38, -38, 870, 870, 870, 1691, 0, -3390, -290, -290, 4400, -2152, -1433, -1433, -265, -265, -4259, -2589, 4895, 735, 735, 3246, -3913, -4163, -4163, 2084, -4086, -4086, 4486, 2632, 2614, 3069, -3298, -4401, -4881, 2646, 2646, -1323, 4204, 0, 0, -4828, 2662, 470, -3909, -3909, -787, -3675, -3675, -3861, -3861, 0, 0, -3093, -1750, -59, -3406, -1558, 2399, 4791, 2491, 1287, 1287, 0, -3019, -1008, 0, 797, 1185, -439, -439, 0, 2470, 3553, -4848, 2519, -3162, -2911, -2556, 2229, 1412, 0, 0, -350, 0, 0, -3403, -3403, 3270, 3270, 2898, 2898, 2898, -1620, 2507, 1180, 1180, 384, -4952, 2221, 3447, 3447, 3447, 3447, 1725, -2329, -2329, -902, 4760, 1092, 1092, 0, 420, 4324, 2048, 99, -3813, -1376, -1376, -1376, -1376, 564, 3107, -852, 3953, 0, -364, 3388, -2440, -2440, 1077, 0, 4340, -2646, 4644, -266, -409, 492, -3000, 1073, -1851, 1950, 1950, -1583, 4937, 3840, 3840, 0, 0, 0, 0, -2505, -1990, 1159, 1797, 0, 0, -146, 4521, -1095, -2776, 0, 2606, -3931, -3931, -3931, 2557, 2557, 2557, 2579, 1438, -980, 498, 0, -3139, 4407, 4407, -9, 2287, 1633, -3942, -3942, -872, 2193, 0, 3894, -1069, -1069, -3094, 3376, 3376, 0, 0, -3017, -3017, -4278, -445, -4408, 2724, 0, 2678, 2398, -1388, 4676, 2958, 2958, 971, 3491, 3491, 3793, 531, 0, -1278, -3552, -4599, -3125, 2763, -2599, 4505, -2964, -2964, 0, -1208, 0, 0, 3727, 3727, 0, 0, -562, 0, 1377, -239, -2957, -4129, -4129, -4129, -3265, 3275, -4140, -4140, -4140, 2421, 2421, 2421, -1462, 1507, 1507, 1507, -4975, 2055, 341, 341, -2183, -2183, -2183, 348, -2964, -2964, 967, 3650, 3650, 0, 545, 0, -3443, 3970, -1816, 4905, 4905, -621, -621, -2103, 33, 1260, 1260, -3546, -3546, -922, -3519, 0, 0, 2382, 3425, -3021, -922, 4183, 989, -1425, -1425, 0, 0, 0, 1784, 1784, 1784, 841, -616, -616, 4488, 3820, 3820, 3820, 3768, 1050, -680, 3757, 2482, -2635, 1408, 1408, -3100, -4242, -4242, -50, -50, -2013, -2013, -2013, -484, -4434, -4434, -4434, 1562, 1562, 1100, -763, -2597, 3203, 3203, 3203, 2512, 2512, -1100, -1100, -1100, -1100, -1100, -1100, -3462, -3859, -1716, 4833, -1843, -1843, 421, 867, 4661, 566, 566, 2885, 2885, 2176, -639, 4285, 4285, 4285, 3229, 3229, 0, 0, -3298, -3298, 0, -1452, -3456, -1922, -1370, 4485, 4485, 422, 422, 322, 2044, 3156, 1588, -2406, -2406, -2406, 2136, 4283, 2991, -969, 0, 0, -369, -369, 0, 4202, 85, 85, 41, -1917, -1917, -773, -3183, 1245, -1991, 2160, -2860, -2480, 2657, 2657, 2657, 2657, 4975, 760, 2750, -3876, 3738, 1255, -3385, -1946, 0, -1511, -4376, -3181, -3830, 1862, 1862, -3265, -3265, -3265, 3379, 956, 4833, 4833, -969, 911, 421, 421, -4156, 4045, 2478, -4151, -2899, -2721, -2721, -2721, -2399, -4976, 2447, 2447, 3534, 0, 0, -4856, -3308, 4592, 4592, 2969, 2969, -4851, 201, 47, -1960, -1960, -1960, -1960, 0, -1181, -4296, 0, 0, 0, -87, 0, -4796, -1630, -3903, 720, 720, 0, 0, 4341, 4341, -911, -911, -2980, -2222, -2222, -2222, 0, 0, 0, -1198, 1609, 2435, 0, 1623, 1623, 0, 1256, -3594, 3234, -316, -2103, 3289, 0, 3765, 2156, 2156, 2158, 2158, 2158, -1958, -526, -2735, -2735, 98, 3157, 4056, 3613, -3940, -3940, 1548, 2735, -4805, -475, -4285, -4115, 0, -806, 813, -2120, -2120, 2910, -1645, -1645, 926, 926, -1873, -1873, 556, 556, -1857, 2339, 0, 4214, 3887, 2540, -4717, 3958, 3958, 3958, -963, -963, 0, 4882, -4490, -3035, -4224, -263, 3646, 894, 0, 0, 0, 2349, 2493, -1124, 1358, -4463, 3136, 1304, -4621, -4621, -4621, 3845, 3845, -368, -4793, 1983, -1186, -1200, 4909, 4909, 4909, 3178, -3670, -42, -1968, 3403, 812, -38, -1570, -2693, -674, -4884, 2048, 2276, 1820, 1820, 1820, 79, -1316, -834, -477, 0, -1873, 3307, 4222, -234, 961, -2372, 4199, 4199, -1714, 4668, 4668, -1556, -1556, -4089, 4389, 4026, 0, -3158, -1678, -1678, -2960, -2960, -2960, -3427, -3427, 4947, 0, 0, -138, -4103, -3391, -3391, 0, 0, -3016, -3990, 4396, -1099, 3406, -3632, -2860, 69, 4315, 4690, -891, -48, 0, 1470, -3638, 0, 4735, 4735, 631, 631, -4769, 2231, 2231, 2231, 0, 0, 0, 0, 1903, 0, 0, 0, 2195, 4142, -1396, -1756, 2954, -4427, 178, 4857, 4813, 4813, 547, 547, 547, 2923, -4627, 1836, -3243, -3243, 3002, 3002, 2600, 2600, 2600, 971, 0, 0, -1, -1, 4351, 4351, 4351, 2351, -2209, -1771, 3061, -3381, -3381, -4978, 923, -2470, 512, 512, 2468, 3987, 3987, 3987, 1993, -3338, -3338, 0, -1696, 3087, 1165, -4491, -4146, -4146, -2412, -4969, 938, 1351, -40, 0, 4035, -3848, 4345, 0, 1176, 2887, 4333, 4333, 4281, 4281, -354, -354, -371, 4692, 415, -990, -975, 3098, 3098, 0, 0, 0, 4626, }; public static int[] second = { 0, 3113, 3352, 3352, 340, -2872, -2872, 4976, 4976, 2541, 4330, -2917, 2139, 2139, 3853, 1393, 4730, 4730, -1734, -1734, 4932, 4663, 0, 0, -4982, 1489, 1489, 1489, -65, -65, -883, 203, 203, 0, 2072, 2072, 2072, -3012, -4878, -225, -225, 0, 0, -3344, -4995, 2638, 2638, -1350, -1350, -2767, 0, 0, -4759, -4759, 1377, 2545, 2439, 2439, 794, 794, 1342, -2773, -2026, 4404, -4974, -4974, 3022, 3022, -159, 2154, -3037, -3383, -3383, 0, -2319, 1905, 0, 2325, -708, -1766, 1205, 1205, -152, 0, 3335, 0, 0, -303, 1567, 1567, -2042, 0, -4032, 658, -299, 3411, 693, 1213, 0, -3541, 2366, 2366, 2366, 249, -2454, -2984, -3969, -3869, -3869, -2230, -1132, 1913, 0, -3508, -3508, 1406, -3917, -3917, -703, -4040, 3070, 3070, 3504, 331, -3142, 3014, 0, 0, -3759, -3977, 4875, -2916, -2916, 1003, 1547, -501, 0, -881, -2926, -2810, 712, 712, 712, 4570, 1726, 0, -2736, 4358, -3164, 2856, 2592, 0, 0, 0, 3972, 3972, -3143, -3143, -1463, -3342, -3342, -3342, -4867, -4867, 4776, -2911, -2911, -2911, -55, 2425, 2425, 2425, 2685, 2685, -4424, 0, 0, 4933, -402, 0, -4278, -3043, 2496, 2496, 2496, 3768, 1678, 1795, 3979, 0, 4131, 4131, 0, -1521, 2763, 2763, -3518, -3518, -3995, 0, 0, -4799, 559, 275, -4180, 0, -3733, -3733, -3704, -3704, 1890, 168, 2795, 2795, -1831, -1831, 1596, -893, -71, -512, -1825, -1825, -1825, -2983, -4707, -4652, 806, 4826, 4959, -2488, 652, 652, -1725, -3984, 0, 696, 4408, -3788, 3764, 0, 1316, -2776, 0, 1849, -2470, -4063, 0, 2248, -2183, -2183, 0, -3837, -3837, -339, 1792, -2659, 358, 0, -4600, 2123, 2123, 3256, -1720, -1720, -1720, 4045, 4045, 3011, -3275, 0, 0, 4615, -4657, 4560, 4560, -3570, 0, 0, 0, -3438, 1677, 3475, 3475, 3475, -3965, 3509, 3070, 3167, 442, -630, 2551, 0, 3260, -2432, -2432, -1347, 4358, -85, -85, 485, 3740, 1675, 1124, 1124, 2627, 4994, -2404, 1638, 4453, -1065, -609, 4294, -1169, 4593, -225, -225, -225, 1077, -60, -4331, 1608, 1876, 3035, -3045, -3045, 4418, -3622, -3622, 0, 0, 0, 4207, 4207, -430, 0, -669, 0, 0, 0, 630, 630, 4581, -1534, 635, -109, -109, -109, -2013, -2208, 3685, 3685, 585, 585, 585, 4999, 4395, 4395, -3385, -2789, 2014, 0, -1382, -2869, -2869, 0, 0, -1884, -1782, 3576, 3576, -3926, 0, 4259, -4770, -4770, -4770, 609, 0, 4005, 4005, 2970, -883, -4679, -12, -12, -890, -2431, 2076, 3011, -1787, 237, -2781, -2917, -558, 1163, -4513, 3420, 3420, 4370, 4370, 1024, 1024, 672, 672, 0, 4570, 4570, 4570, -2277, 882, 3456, 0, 0, 0, 3594, -3209, -3133, -3852, 3045, 3045, -4142, -4142, 2054, -4471, 1997, 0, 1852, 3386, -1709, -2972, 0, -4104, 178, -1491, 3984, 3984, 3984, 3984, 3984, 3984, 3984, 0, 0, 3692, 0, 2964, 1548, -1106, 4484, 0, 0, 2600, -2594, 3632, 3632, 2253, 532, 210, -81, 0, 0, 0, -74, 280, -2884, 1715, -1045, -431, -431, 3357, 3695, 3695, 3695, 0, 385, 385, -307, -4858, -2792, 874, -465, -833, 74, 74, 74, -742, -742, -742, 441, 0, -4041, -4041, -4106, 360, 0, 620, 620, 620, 620, 2498, 4531, -1598, -4643, 3714, 3714, 1309, 3206, 3206, 3542, -2609, -2609, -1165, 0, 0, 0, 0, 0, -2333, 3554, 3554, 3554, 3554, 1828, -3542, -3542, -629, 1589, 2508, -1528, -799, -799, 0, -733, -4520, 2242, 2242, 2226, 1972, 4800, 4800, 4471, -1618, 4229, 4229, 4459, 0, 3, 3, 3171, 4739, -279, 3049, -1220, 0, 113, -2272, 2675, -4903, -2498, -3219, -3903, -2275, -2040, 0, -1973, 0, -4041, 3277, 3277, 3277, -3240, -669, -669, -669, -4814, -3918, 2542, 4695, 4695, 2873, 2926, 2789, 2789, -2080, -4957, -4957, -4603, 4290, 2043, 3375, -827, -827, 4175, 185, 185, 1932, -2172, -2172, -3174, -4826, -4813, 3740, -4327, -2278, 0, -1875, -1875, -3950, -4829, -261, 3432, -1274, -1274, -2502, -1351, -4286, -4898, 1371, -988, 416, -1065, 76, 4318, 0, 0, 0, 0, -2579, -2579, 0, 0, 0, 1918, 2781, 2781, -2986, -4446, 1261, 1261, 1261, 1261, 1261, 4286, -2951, 4707, 4707, 4466, 4466, -772, 4691, -4913, -1642, -3092, -3092, 0, 565, 565, -2026, -2026, 0, 742, 742, -3122, -4087, -4087, -4087, -4119, -4097, -175, -175, -175, 1776, 1776, 1776, 561, 0, 0, -838, 3071, 3071, 3654, 3654, 708, 708, 4532, 0, 3213, 4535, 4535, 1969, -2980, 2485, -4969, -4969, -1097, -39, 3932, 3116, -1807, 0, 0, -2869, 1274, 1274, 1700, 1700, 1700, 3985, -2590, 2896, 0, 3012, -2029, -102, 631, 631, -1159, -1159, 394, 2800, -4835, 1446, -1185, -1185, -4091, -4380, -1061, 463, -20, 2527, -4468, 1062, 4188, 33, -4370, -4370, 0, -4230, 4405, 0, 0, 0, 0, -1644, 178, 0, -369, -369, -4085, -3883, -3883, 2150, 633, -4599, -4599, 0, -3318, -4545, 4759, 62, -865, 1716, 1716, -3735, -1204, -1204, -435, -779, 0, 3438, 0, 0, -567, 0, -927, -3647, 0, -3959, -3959, 0, 1074, 2355, 2887, 4760, 0, 0, -1100, -1100, -4905, -4905, -2193, -2193, -2193, -2193, 1046, -2771, -2832, -831, -3156, -3156, 4648, -4179, 1680, 435, 649, 1309, 1309, -1893, 1823, 1823, 1823, 1823, 0, 0, -414, -4631, 1388, -70, 4216, 1789, 1789, -3121, 0, 3960, 2665, -727, 1551, 0, 0, 1544, -4000, -567, 212, 212, -186, -1243, 0, 0, -3411, -3411, -3411, 4965, -4064, -456, 4987, 4987, 2007, 2007, -190, -1634, -1634, 1537, 689, -3040, -844, -3000, 367, -1076, -1076, 4581, -2278, -1899, -3602, -1449, -4000, -894, 985, -957, 4384, -3149, -3747, 2391, 3940, 0, 0, 1487, -201, 3961, 3961, -4910, 0, 0, 0, -2486, -2486, -3592, 4360, 2815, 2815, -1664, -1664, 3135, -4170, 0, 0, 504, -2385, 0, 0, -354, -1862, -1035, 231, 231, 231, 0, -1034, 4996, 336, 2447, 0, 2563, -1115, -1115, -357, -143, -3451, -3451, 0, -857, -857, -2057, -134, -1162, -4728, -4728, -4728, 0, 3608, 3608, 3608, 0, 0, 0, 2559, 1068, -2393, -563, 4619, 4619, -174, -174, -2133, -1469, 2501, -2875, 0, 910, -235, 0, 0, 0, -4155, 0, 3526, 2167, -1872, 4703, 3555, 3555, 0, -1864, 0, -2865, -2765, 1606, -543, 3742, 3742, -932, 4973, 1684, 0, 4239, 3537, 787, 3399, 3399, 3399, 3399, 2149, -1940, -478, 2748, 4713, 2538, 3542, 2925, 3257, -2152, -2152, -3643, -3643, 1381, -4229, -1835, 32, 32, 396, 0, }; public static void main(String[] args) { System.out.println("howManyTimesIsFirstGreaterThanSecond() = " + howManyTimesIsFirstGreaterThanSecond(first, second)); System.out.println("howManyTimesDoNumbersIncrease() = " + howManyTimesDoNumbersIncrease(first)); System.out.println("averageOfAllEvenNumbers() = " + averageOfAllEvenNumbers(second)); System.out.println("firstIndexOfLongestRepeatingInteger() = " + firstIndexOfLongestRepeatingInteger(first, 0)); int[] subsequence = new int[] { 2398, -1388, 4676, 2958, 2958, 971, 3491, 3491, 3793, 531, 0, -1278, -3552 }; System.out.println("subsequenceStart() = " + subsequenceStart(first, subsequence)); } // easy // returns the number of times first[i] > second [i] // you can assume first.length == second.length public static int howManyTimesIsFirstGreaterThanSecond(int[] first, int[] second){ // implement int count=0; for(int i=0;i<first.length;i++){ if(first[i]>second[i]){ count++; } } return count; } // easy // returns the number of times is input[i + 1] > input[i] public static int howManyTimesDoNumbersIncrease(int[] input) { // implement int times=0; for(int i=0;i<input.length;i++){ if(input[i+1]>input[i]){ times++; } } return times; } // medium // returns the average of all even numbers public static double averageOfAllEvenNumbers(int[] input) { // implement int sum=0; for (int i=0; i<input.length;i++){ if(input[i]%2==0){ sum+=input[i]; } } return (double)sum/input.length; } // hard // returns the first index of the longest repeating sequence of x in input // for example, if input = {0,0,1,1,1,0,1} and x = 1, then output = 2 public static int firstIndexOfLongestRepeatingInteger(int[] input, int x) { // implement return 1; } // hard // returns the first index where the subsequence is found in sequence // you can assume subsequence.length < sequence.length // you can assume subsequence is in sequence public static int subsequenceStart(int[] sequence, int[] subsequence) { // implement return 1; } }
for(int i=0;i<input.length;i++){ if(input[i+1]>input[i]){ times++; } } You have to add a test here because input[i+1] is out of bound of course. For example : input.length = 1000; input[999 + 1] is out of bound because the last index of your array is 999 Here is the code you'll have to add for(int i=0;i<input.length;i++){ if (i == input.length - 1) break; if(input[i+1]>input[i]){ times++; } } You break from the loop once all the indexes have been tested. EDIT : For the clarity, I put this example when I could've just changed the condition in the loop. I choose this one because it speaks more. Here's the other solution for (int i = 0 ; i < input.length() - 1 ; i++){ (...) }
Your arrays are of equal lengths. However, because array indeces start at ZERO, the last index in the array should be 999. if(input[i+1]>input[i]){ times++; } 999 is less than the array's length, but 999 + 1 is 1000, which is out of bounds.
JAVA url = new URL () malformedURLexception
I was trying to build a crawler that collects HTML sourcecodes from websites, which I have in a .csv file. Everything seems to be working fine whenever I place the link in url = new URL ("http://example.com") but whenever I try to place the link in a variable ("text" in this example) I get an error, telling me that there has been a malformedURLException. Here is my code: String text ="http://stackoverflow.com/questions/9827143/continuing-execution-after-an-exception-is-thrown-in-java"; // get the sourcecode of the link you just grabbed url = new URL(text); PrintWriter writer = new PrintWriter("sourcecode.txt", "UTF-8");
You have hidden characters in your string. You probably copied the URL from a Word file or a text file that was converted in Windows. There is a BOM marker in its beginning. When I do this: System.out.println( Arrays.toString(text.getBytes(StandardCharsets.UTF_16BE))); This is the output I get: [-2, -1, 0, 104, 0, 116, 0, 116, 0, 112, 0, 58, 0, 47, 0, 47, 0, 115, 0, 116, 0, 97, 0, 99, 0, 107, 0, 111, 0, 118, 0, 101, 0, 114, 0, 102, 0, 108, 0, 111, 0, 119, 0, 46, 0, 99, 0, 111, 0, 109, 0, 47, 0, 113, 0, 117, 0, 101, 0, 115, 0, 116, 0, 105, 0, 111, 0, 110, 0, 115, 0, 47, 0, 57, 0, 56, 0, 50, 0, 55, 0, 49, 0, 52, 0, 51, 0, 47, 0, 99, 0, 111, 0, 110, 0, 116, 0, 105, 0, 110, 0, 117, 0, 105, 0, 110, 0, 103, 0, 45, 0, 101, 0, 120, 0, 101, 0, 99, 0, 117, 0, 116, 0, 105, 0, 111, 0, 110, 0, 45, 0, 97, 0, 102, 0, 116, 0, 101, 0, 114, 0, 45, 0, 97, 0, 110, 0, 45, 0, 101, 0, 120, 0, 99, 0, 101, 0, 112, 0, 116, 0, 105, 0, 111, 0, 110, 0, 45, 0, 105, 0, 115, 0, 45, 0, 116, 0, 104, 0, 114, 0, 111, 0, 119, 0, 110, 0, 45, 0, 105, 0, 110, 0, 45, 0, 106, 0, 97, 0, 118, 0, 97] The first two bytes are the unicode BOM character. Be careful where you get your strings from. If you export your CSV from Excel, and the file contains only URLs, try to export it as ASCII only.
There's a problem with your double quote. I pasted your "text" line into Eclipse and tried to save, and it showed me that there was an invalid character at the start of your "text" string because there was a Cp1252 encoded character. I deleted the first double quote you had, and retyped it. Then I ran String text = "http://stackoverflow.com/questions/9827143/continuing-execution-after-an-exception-is-thrown-in-java"; try { URL url = new URL(text); PrintWriter writer = new PrintWriter("sourcecode.txt", "UTF-8"); System.out.println("all good"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } And it worked.
You have a special char in your text variable. Just tried your link in a Browser and it did not work because of this. Copy the following and try again: ​String text ="http://stackoverflow.com/questions/9827143/continuing-execution-after-an-exception-is-thrown-in-java";