Loading...
Logo
Processing Forum
I would like to get the example code from java to work:
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/zip/Inflater.html

here it is:
              
Copy code
  1. // Encode a String into bytes
  2. String inputString = "blahblahblah??";
  3. byte[] input = inputString.getBytes("UTF-8");

  4. // Compress the bytes
  5. byte[] output = new byte[100];
  6. Deflater compresser = new Deflater();
  7. compresser.setInput(input);
  8. compresser.finish();
  9. int compressedDataLength = compresser.deflate(output);

  10. // Decompress the bytes
  11. Inflater decompresser = new Inflater();
  12. decompresser.setInput(output, 0, compressedDataLength);
  13. byte[] result = new byte[100];
  14. int resultLength = decompresser.inflate(result);
  15. decompresser.end();

  16. // Decode the bytes into a String
  17. String outputString = new String(result, 0, resultLength, "UTF-8");
I get:
 Unhandled exception type UnsupportedEncodingException
 
So i use:

import java.util.zip.*; import java.io.*;

and put everything in a try block but i can get multiple things thrown at me.

How can i do this in a clean way?
And if i need to try catch, why doesnt the javadoc show that?

Replies(1)

"i can get multiple things thrown at me"
This is imprecise at best...

"
if i need to try catch, why doesnt the javadoc show that? "
Because they suppose you know how to code in Java?
The JavaDoc always specifies when a method can throw an exception.

For example:
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#getBytes(java.lang.String)

getBytes

public byte[] getBytes(String charsetName)
                throws UnsupportedEncodingException
Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.

The behavior of this method when this string cannot be encoded in the given charset is unspecified. The CharsetEncoder class should be used when more control over the encoding process is required.


Parameters:
charsetName - The name of a supported charset
Returns:
The resultant byte array
Throws:
UnsupportedEncodingException - If the named charset is not supported
Since:
JDK1.1