JAVA url = new URL () malformedURLexception - java

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";

Related

Take screenshot in native java and display this screenshot in flutter

Want to take a screenshot of the mobile screen with native Java. Store this screenshot Bitmap in a byte[] array, pass this byte[] array in flutter through platform channel and finally want to display this screenshot in a flutter app.
Native Java Code:
public Bitmap takeScreenshot(){
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
public byte[] bitmapToByte(){
Bitmap bitmap = takeScreenshot();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] array = stream.toByteArray();
Log.i("MyAndroidClass", Arrays.toString(array));
return array;
}
public File saveBitmap(Bitmap bitmap){
Bitmap bitmap = takeScreenshot();
File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshoot.png");
FileOutputStream fos;
try{
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
return imagePath;
}
Dart Platform Channel:
Future<Null> _takeScreenShot() async {
Uint8List screenShot;
Uint8List ssView = await platform2.invokeMethod("takeScreenshot");
screenShot = ssView;
print(screenShot);
setState(() {
byteView = screenShot;
});
}
Flutter UI to display screenshot:
Container(
child: byteView != null
? Image.memory(
byteView,
width: 100,
height: 150,
)
: Container(),
),
print(screenshot) shows a byte[] array:
[137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 4, 56, 0, 0, 7, 128, 8, 2, 0, 0, 0, 164, 3, 112, 93, 0, 0, 0, 3, 115, 66, 73, 84, 8, 8, 8, 219, 225, 79, 224, 0, 0, 32, 0, 73, 68, 65, 84, 120, 156, 236, 221, 187, 138, 20, 81, 20, 64, 209, 115, 155, 198, 119, 160, 129, 162, 160, 32, 38, 130, 255, 228, 159, 248, 165, 134, 26, 136, 10, 130, 207, 81, 25, 186, 12, 156, 25, 17, 140, 237, 13, 179, 86, 80, 20, 197, 13, 78, 186, 57, 69, 213, 122, 254, 98, 46, 172, 53, 107, 247, 251, 110, 0, 0, 0, 254, 135, 109, 102, 102, 14, 115, 216, 254, 60, 219, 255, 227, 208, 58, 63, 10, 0, 0, 112, 12, 103, 161, 178, 109, 51, 219, 28, 182, 57, 28, 102, 89, 167, 0, 0, 0, 255, 215, 90, 179, 214, 204, 58, 235, 145, 243, 141, 202, 54, 223, 79, 230, 219, 167, 249, 246, 249, 252, 220, 113, 198, 3, 0, 0, 46, 163, 171, 55, 230, 230, 237, 185, 118, 99, 214, 110, 102, 155, 253, 110, 205, 54, 115, 216, 230, 235, 199, 121, 251, 106, 222, 191, 58, 246, 128, 0, 0, 192, 229, 115, 231, 254, 60, 120, 50, 87, 174, 206, 126, 63,
Complete Byte[] array didn't pass to flutter from native through the channel.
But it displays a black Image container
An easy way to achieve this would be to save the screenshot as a file on the disk and simply pass the file path to flutter.
Then you load it using Image.file

Spring Framework 3.2.5 - Issue decoding the response body of a POST API output

I just spent 5 hours trying to solve this issue but I've made zero progress. I've tried all the solutions I could find but I'm stuck.
Here's my basic setup and the issue that I'm facing:
Basic Setup
I have 2 functions -
1) callGetApi(String url)
2) callPostApi(String url, String requestBody)
The code is more or less the same:
callGetApi:
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "application/json");
headers.add("Authorization", "Basic " + apiAuthorizationString);
ResponseEntity<String> entity = null;
try {
entity = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<Object>(headers), String.class);
apiOutput = gson.fromJson(entity.getBody().toString(), ApiOutput.class);
}
....
callPostApi:
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Content-Type", "application/json");
headers.add("Authorization", "Basic " + apiAuthorizationString);
HttpEntity<Object> request = new HttpEntity<Object>(request_body, headers);
ResponseEntity<String> entity = null;
try {
entity = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
apiOutput = gson.fromJson(entity.getBody().toString(), ApiOutput.class);
}
.....
Issue
The issue that I'm facing is that in the callGetApi() function, I'm receiving proper response which is getting decoded properly, but I'm receiving differently-encoded response in the CallPostApi() function which I'm not able to decode. Both of these are working perfectly fine in PostMan with the same input.
Output I'm receiving in callPostApi() function call:
Debugger Output
Output Received in Postman for same input and headers:
{"SuccessData":"Ticket not saved as no changes madeINC024","ErrorData":null,"AppData":null}
In the callGetApi() function I always receive these headers:
{Transfer-Encoding=[chunked], Content-Type=[text/plain; charset=utf-8], Server=[Kestrel], X-Powered-By=[ASP.NET], Date=[Mon, 15 Oct 2018 13:05:27 GMT]}
In the callPostApi() function, I always receive these headers:
{Transfer-Encoding=[chunked], Content-Type=[text/plain; charset=utf-16], Server=[Kestrel], X-Powered-By=[ASP.NET], Date=[Mon, 15 Oct 2018 13:06:58 GMT]}
And at the same time, the same API endpoint for callPostApi() function is sending the proper headers in Postman(same headers as the one for callGetApi() above)
Solutions I've tried:
I've tried all PnCs of the following:
1) Added StringHttpMessageConverter for UTF-16 and UTF-8 both separately and together:
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-16")));
2) Removed all other converters:
restTemplate.getMessageConverters().clear();
3) Added the following headers in callPostApi():
headers.add("Accept-Encoding", "identity");
headers.add("Cache-Control", "no-cache");
headers.add("Accept-Charset", "utf-8");
headers.add("Accept", "application/json, charset=utf-8");
headers.set("Accept-Language", "en");
4) Used HttpHeaders instead of MultiValueMap:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization", "Basic " + apiAuthorizationString);
headers.add("Accept-Encoding", "identity");
headers.add("Cache-Control", "no-cache");
headers.add("Accept-Charset", "utf-8");
Something that I tried that showed some progress:
entity = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
//entity has the same data as the debugger screenshot I've attached above
byte[] utf8 = entity.getBody().toString().getBytes("UTF-16");
String string = new String(utf8, "UTF-8");
On running the above code, the string array contains the correct output BUT with spaces between all characters:
[{,,",,S,,u,,c,,c,,e,,s,,s,,D,,a,,t,,a,,",,:,,",,T,,i,,c,,k,,e,,t,,
,,n,,o,,t,, ,,s,,a,,v,,e,,d,, ,,a,,s,, ,,n,,o,,
,,c,,h,,a,,n,,g,,e,,s,,
,,m,,a,,d,,e,,I,,N,,C,,0,,2,,4,,",,,,,",,E,,r,,r,,o,,r,,D,,a,,t,,a,,",,:,,n,,u,,l,,l,,,,,",,A,,p,,p,,D,,a,,t,,a,,",,:,,n,,u,,l,,l,,},]
the byte array contains the following data:
[123, 0, 34, 0, 83, 0, 117, 0, 99, 0, 99, 0, 101, 0, 115, 0, 115, 0,
68, 0, 97, 0, 116, 0, 97, 0, 34, 0, 58, 0, 34, 0, 84, 0, 105, 0, 99,
0, 107, 0, 101, 0, 116, 0, 32, 0, 110, 0, 111, 0, 116, 0, 32, 0, 115,
0, 97, 0, 118, 0, 101, 0, 100, 0, 32, 0, 97, 0, 115, 0, 32, 0, 110, 0,
111, 0, 32, 0, 99, 0, 104, 0, 97, 0, 110, 0, 103, 0, 101, 0, 115, 0,
32, 0, 109, 0, 97, 0, 100, 0, 101, 0, 73, 0, 78, 0, 67, 0, 48, 0, 50,
0, 52, 0, 34, 0, 44, 0, 34, 0, 69, 0, 114, 0, 114, 0, 111, 0, 114, 0,
68, 0, 97, 0, 116, 0, 97, 0, 34, 0, 58, 0, 110, 0, 117, 0, 108, 0,
108, 0, 44, 0, 34, 0, 65, 0, 112, 0, 112, 0, 68, 0, 97, 0, 116, 0, 97,
0, 34, 0, 58, 0, 110, 0, 117, 0, 108, 0, 108, 0, 125, 0]
Any idea how I can fix this? All help is appreciated
Changing ResponseEntity's type from String to Object worked. The code is:
ResponseEntity<Object> entity = restTemplate.exchange(url, HttpMethod.POST, request, Object.class);
Thanks to #HadiJ for pointing me in the right direction.

not getting the original data after decompression with Inflater. inflate

Here's my test code, the problem is resultLength != original.length, and result array is same as original array, shouldn't it be after compression and decompression?
public static void main(String[] args) {
// generateTyreAlarmEvent()
byte[] original = {10, 17, 97, 98, 99, 100, 101, 102, 103, 104, 105, 103, 107, 108, 109, 110, 111, 112, 113, 16, 2, 24, -50, -1, -113, -59, 5, 34, 20, 8, -35, 1, 16, 40, 24, -34, 1, 32, 60, 40, -33, 1, 48, 80, 56, -32, 1, 64, 100, 42, 16, 8, 2, 16, 1, 24, 3, 32, 0, 40, 1, 48, 1, 56, 1, 64, 0};
byte[] buffer = new byte[original.length];
Deflater compresser = new Deflater();
compresser.setInput(original);
compresser.finish();
int compressedLen = compresser.deflate(buffer);
compresser.end();
Inflater decompressor = new Inflater();
decompressor.setInput(buffer, 0, compressedLen);
byte[] result = new byte[original.length];
int resultLength = 0;
try {
resultLength = decompressor.inflate(result);
} catch (DataFormatException e) {
}
decompressor.end();
// the problem is resultLength(63) != original.length(67), and result array is same as original array
}
The original byte array is (len is 67) :
{10, 17, 97, 98, 99, 100, 101, 102, 103, 104, 105, 103, 107, 108, 109, 110, 111, 112, 113, 16, 2, 24, -50, -1, -113, -59, 5, 34, 20, 8, -35, 1, 16, 40, 24, -34, 1, 32, 60, 40, -33, 1, 48, 80, 56, -32, 1, 64, 100, 42, 16, 8, 2, 16, 1, 24, 3, 32, 0, 40, 1, 48, 1, 56, 1, 64, 0}
and the result is (len is 63) :
{10, 17, 97, 98, 99, 100, 101, 102, 103, 104, 105, 103, 107, 108, 109, 110, 111, 112, 113, 16, 2, 24, -50, -1, -113, -59, 5, 34, 20, 8, -35, 1, 16, 40, 24, -34, 1, 32, 60, 40, -33, 1, 48, 80, 56, -32, 1, 64, 100, 42, 16, 8, 2, 16, 1, 24, 3, 32, 0, 40, 1, 48, 1}
Thanks!
You did not make the output buffer big enough. Just to test, I replaced byte[] buffer = new byte[original.length]; with byte[] buffer = new byte[1000];, and it worked fine.
If the data is not compressible, as this is, then the output will be larger than the input.

Non symmetric java compression

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,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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, -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-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.

Convert byte array (byte[]) to Image in Java

I have a byte[] that I want to convert to an Image and display the image in a label.
The byte[] is of a jpeg 2000 format.
I have tried the code below but it returns null:
InputStream in = new ByteArrayInputStream(bytearray);
BufferedImage image = ImageIO.read(in);
The image value comes back as null.
I want to be able to display the image in a label like below:
jLabel.setIcon(new ImageIcon(image));
Thanks
To convert an array of bytes, i.e. byte[] into an Image, use getImage(). Probably the easiest way to do this is to instantiate an ImageIcon using the ImageIcon(byte[]) constructor, and then call getImage(). This is illustrated in the method below, particularly the last line:
public Image createImage(){
//ccurve.png
byte[] b = {-119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82,
0, 0, 0, 15, 0, 0, 0, 15, 8, 6, 0, 0, 0, 59, -42, -107,
74, 0, 0, 0, 64, 73, 68, 65, 84, 120, -38, 99, 96, -64, 14, -2,
99, -63, 68, 1, 100, -59, -1, -79, -120, 17, -44, -8, 31, -121, 28, 81,
26, -1, -29, 113, 13, 78, -51, 100, -125, -1, -108, 24, 64, 86, -24, -30,
11, 101, -6, -37, 76, -106, -97, 25, 104, 17, 96, -76, 77, 97, 20, -89,
109, -110, 114, 21, 0, -82, -127, 56, -56, 56, 76, -17, -42, 0, 0, 0,
0, 73, 69, 78, 68, -82, 66, 96, -126};
return new ImageIcon(b).getImage();
}
I think this can by used for png, gif, bmp, and jpg images. Also the byte array does not have to be hard-coded, as in this example.
If you want an ImageIcon instead of an Image, don't call getImage():
public ImageIcon createImageIcon(){
//ccurve.png
byte[] b = {-119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82,
0, 0, 0, 15, 0, 0, 0, 15, 8, 6, 0, 0, 0, 59, -42, -107,
74, 0, 0, 0, 64, 73, 68, 65, 84, 120, -38, 99, 96, -64, 14, -2,
99, -63, 68, 1, 100, -59, -1, -79, -120, 17, -44, -8, 31, -121, 28, 81,
26, -1, -29, 113, 13, 78, -51, 100, -125, -1, -108, 24, 64, 86, -24, -30,
11, 101, -6, -37, 76, -106, -97, 25, 104, 17, 96, -76, 77, 97, 20, -89,
109, -110, 114, 21, 0, -82, -127, 56, -56, 56, 76, -17, -42, 0, 0, 0,
0, 73, 69, 78, 68, -82, 66, 96, -126};
return new ImageIcon(b);
}
Then you can call jlabel.setIcon(createIconImage());.
ServletOutputStream out = response.getOutputStream();
out.write(user.getBytes());
The above is how its worked for me in the past where user has a profile picture simply stored in a byte array. The servlet realizes this and outputs the image.

Categories