-2

I have a map in java (Version 1.7)

public void doTest() {
    try {
         Map<String, String> data = new HashMap<String, String>();
         data.put("Product", "someProduct");
         data.put("CreatedOn", "Fri May 08 02:25:03 IST 2015");
         data.put("Module", "someService");
         data.put("Type", "ERROR");
         data.put("Message", "LogId:635666739033286524 Message:WSClient.FetchReservation::WS FetchReservation calledSystem.Net.WebException: The operation has timed out at System.Web.Services.Protocols.WebClientProtocol.GetWebResponse(WebRequest request) at System.Web.Services.Protocols.HttpWebClientProtocol.GetWebResponse(WebRequest request) at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters) at .BOTWSClient.ChannelDirectWS.YieldGain.fFetchReservation(String strReservationXML) at BOTWSClient.WSClient.FetchReservation(String RequestXML) ");
         data.put("IP", "");
         data.put("Name", "WriteLog");

         byte[] byteArray = convert(data);
         int response = port.writelogcollection(byteArray);

    } catch(Exception e) {
        e.printStackTrace();
    }
}

  public byte[] convert(Map obj) throws IOException {
   ObjectOutputStream os = null;

  ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
  os = new ObjectOutputStream(new GZIPOutputStream(byteStream));
  os.writeObject(obj);
  os.flush();
  byte[] sendBuf = byteStream.toByteArray();
  os.close();
 return sendBuf;

}

 public static String writelogcollection(byte[] bytes) throws IOException {
BufferedReader bf;
    String outStr;
    try (GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(bytes))) {
        bf = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
        outStr = "";
        String line;
        while ((line = bf.readLine()) != null) {
            outStr += line;
        }
    }
    bf.close();
    return outStr;
}

I am trying to GZIPInputStream but getting following exception

Exception occurred in target VM: Not in GZIP format 
    java.util.zip.ZipException: Not in GZIP format
    at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:164)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:78)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:90)

I am sure its not java version issue, Its I am missing in code.

Sergei Tachenov
  • 24,345
  • 8
  • 57
  • 73
sangita
  • 191
  • 5
  • 14
  • You're trying to use GZIPInputStream for reading data that you have just created, but not in GZIP format. Of course it breaks saying that it's... well, not in GZIP format. Perhaps you meant to use GZIPOutputStream in your `convert()`? – Sergei Tachenov May 09 '15 at 10:40
  • Nope I need to convert my map into byte Array first then need to GZIP it. So in convert() I am converting map in array of byte and passing it to writelogcollection(). – sangita May 09 '15 at 10:51
  • But what you're actually doing is `convert()` then gUNzip it. Of course it doesn't work. – Sergei Tachenov May 09 '15 at 10:59
  • So what should be changed in Convert(), that's what my query is? – sangita May 09 '15 at 11:02
  • I don't get it. If you need to GZIP your data, why are you trying to GUNZIP it? – Sergei Tachenov May 09 '15 at 11:27
  • Ok, sorry for not clearing from my end. I first need to convert map in byteArray, GZIP it and GUNZIP it in writelogcollection(). I have modified convert() but now getting Exception occurred in target VM: Unexpected end of ZLIB input stream java.io.EOFException: Unexpected end of ZLIB input stream at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240) at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158) at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:116) – sangita May 09 '15 at 11:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77382/discussion-between-sangita-and-sergey-tachenov). – sangita May 09 '15 at 11:44

1 Answers1

1

Your error is here:

os.flush();
byte[] sendBuf = byteStream.toByteArray();
os.close();

It should be

os.flush();
os.close();
byte[] sendBuf = byteStream.toByteArray();

because while flush() only writes the data that has been written so far, but close() also writes some important meta-data that is needed to properly form the gzipped object stream.

There are also different minor issues with your code, like writelogcollection should be writeLogCollection, you should use try-with-resources to close your BufferedReader and InputStreamReader (which you don't close at all), and other code cleanness issues. Keeping your code clean makes it easier for you to understand what's wrong.

Oh, and last but not least, it doesn't make sense to first write objects using ObjectOutputStream and then reading them with BufferedReader. ObjectOutputStream produces binary data which will turn into garbage if you try to interpret it as text.

Sergei Tachenov
  • 24,345
  • 8
  • 57
  • 73