# Read a compressed (deflate) block using Java

**URL:** <https://forum.hdfgroup.org/t/read-a-compressed-deflate-block-using-java/3428>\
**Category:** Hdf-forum archives\
**Created:** [March 11, 2015, 10:36pm UTC](https://forum.hdfgroup.org/t/read-a-compressed-deflate-block-using-java/3428 "2015-03-11T22:36:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Eldawy1](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/a/c0e974/32.png) [@Ahmed\_Eldawy1](https://forum.hdfgroup.org/u/Ahmed_Eldawy1)\
**Post date:** [March 11, 2015, 10:36pm UTC](https://forum.hdfgroup.org/t/read-a-compressed-deflate-block-using-java/3428/1 "2015-03-11T22:36:55Z")

</div>

Hi all,  
&nbsp;&nbsp;I'm working on a project that deals with HDF4 files provided by NASA.  
This is part of a big project in University of Minnesota (  
[http://spatialhadoop.cs.umn.edu/](http://spatialhadoop.cs.umn.edu/)) that uses Hadoop as a distributed  
environment to process large data including satellite data. I used to use  
HDFJava wrappers to deal with HDF files and it worked OK. However, since it  
depends on native libraries, I frequently have troubles distributing the  
code over a heterogeneous cluster of different architectures and systems.  
So, I decided to write my own Java-native code to open HDF files.  
Currently, I'm just writing the minimal code to support the files I'm  
working on.

I followed the specifications provided on the website and I was  
successfully able to get the structure of the HDF file and reach the data  
blocks that contain the main data (temperature in my case). These blocks  
are compressed using deflate with level 1 as indicated by the file.  
However, when I try to decompress these blocks, they do not give me the  
expected size. I feel that there's some kind of incompatible decompression  
parameters that I need to fix. Here's the code I wrote to decompress the  
file

&nbsp;&nbsp;&nbsp;&nbsp;byte[] compressedData = readRawData(in); // Reads the raw data from the  
block  
&nbsp;&nbsp;&nbsp;&nbsp;DeflaterInputStream dis = new DeflaterInputStream(new  
ByteArrayInputStream(compressedData), new Deflater(1, true)); // Creates a  
decompressor  
&nbsp;&nbsp;&nbsp;&nbsp;ByteArrayOutputStream baos = new ByteArrayOutputStream(); // A  
temporary place to store decompressed data  
&nbsp;&nbsp;&nbsp;&nbsp;// This loop reads the uncompressed data and caches it in memory  
&nbsp;&nbsp;&nbsp;&nbsp;byte[] buffer = new byte[4096];  
&nbsp;&nbsp;&nbsp;&nbsp;int bufferLength;  
&nbsp;&nbsp;&nbsp;&nbsp;while ((bufferLength = dis.read(buffer)) \> 0) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;baos.write(buffer, 0, bufferLength);  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;dis.close();  
&nbsp;&nbsp;&nbsp;&nbsp;baos.close();  
&nbsp;&nbsp;&nbsp;&nbsp;// Retrieve the decompressed data  
&nbsp;&nbsp;&nbsp;&nbsp;uncompressedData = baos.toByteArray();

The header of the compressed data indicate that the size after  
decompression should be 2880000 bytes, but my code finds that it is  
1613097, which is totally different. When creating the deflater, I tried to  
set the 'nowrap' parameter to true or false but both do not work. The same  
file works with HDFJava libraries so it should be a bug in my code.

Thanks in advance for your help. Please let me know if you need more  
details about this issue.

Best regards,  
Ahmed Eldawy

---

<div class="post-metadata">

**Author:** ![Ahmed\_Eldawy1](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/a/c0e974/32.png) [@Ahmed\_Eldawy1](https://forum.hdfgroup.org/u/Ahmed_Eldawy1)\
**Post date:** [March 11, 2015, 10:47pm UTC](https://forum.hdfgroup.org/t/read-a-compressed-deflate-block-using-java/3428/2 "2015-03-11T22:47:58Z")

</div>

Sorry. My bad. I had to use InflaterInputStream instead of  
DeflaterInputStream. What I was doing was simply recompressing the data  
that is already compressed. It works fine now.

Best regards,  
Ahmed Eldawy

> **···**
>
> On Wed, Mar 11, 2015 at 5:36 PM, Ahmed Eldawy \<aseldawy@gmail.com\> wrote:
> 
> > Hi all,  
> > &nbsp;&nbsp;I'm working on a project that deals with HDF4 files provided by NASA.  
> > This is part of a big project in University of Minnesota (  
> > [http://spatialhadoop.cs.umn.edu/\](http://spatialhadoop.cs.umn.edu/%5C)) that uses Hadoop as a distributed  
> > environment to process large data including satellite data. I used to use  
> > HDFJava wrappers to deal with HDF files and it worked OK. However, since it  
> > depends on native libraries, I frequently have troubles distributing the  
> > code over a heterogeneous cluster of different architectures and systems.  
> > So, I decided to write my own Java-native code to open HDF files.  
> > Currently, I'm just writing the minimal code to support the files I'm  
> > working on.
> > 
> > I followed the specifications provided on the website and I was  
> > successfully able to get the structure of the HDF file and reach the data  
> > blocks that contain the main data (temperature in my case). These blocks  
> > are compressed using deflate with level 1 as indicated by the file.  
> > However, when I try to decompress these blocks, they do not give me the  
> > expected size. I feel that there's some kind of incompatible decompression  
> > parameters that I need to fix. Here's the code I wrote to decompress the  
> > file
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;byte compressedData = readRawData(in); // Reads the raw data from  
> > the block  
> > &nbsp;&nbsp;&nbsp;&nbsp;DeflaterInputStream dis = new DeflaterInputStream(new  
> > ByteArrayInputStream(compressedData), new Deflater(1, true)); // Creates a  
> > decompressor  
> > &nbsp;&nbsp;&nbsp;&nbsp;ByteArrayOutputStream baos = new ByteArrayOutputStream(); // A  
> > temporary place to store decompressed data  
> > &nbsp;&nbsp;&nbsp;&nbsp;// This loop reads the uncompressed data and caches it in memory  
> > &nbsp;&nbsp;&nbsp;&nbsp;byte buffer = new byte[4096];  
> > &nbsp;&nbsp;&nbsp;&nbsp;int bufferLength;  
> > &nbsp;&nbsp;&nbsp;&nbsp;while ((bufferLength = dis.read(buffer)) \> 0) {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;baos.write(buffer, 0, bufferLength);  
> > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;&nbsp;&nbsp;dis.close();  
> > &nbsp;&nbsp;&nbsp;&nbsp;baos.close();  
> > &nbsp;&nbsp;&nbsp;&nbsp;// Retrieve the decompressed data  
> > &nbsp;&nbsp;&nbsp;&nbsp;uncompressedData = baos.toByteArray();
> > 
> > The header of the compressed data indicate that the size after  
> > decompression should be 2880000 bytes, but my code finds that it is  
> > 1613097, which is totally different. When creating the deflater, I tried to  
> > set the 'nowrap' parameter to true or false but both do not work. The same  
> > file works with HDFJava libraries so it should be a bug in my code.
> > 
> > Thanks in advance for your help. Please let me know if you need more  
> > details about this issue.
> > 
> > Best regards,  
> > Ahmed Eldawy

---

<div class="post-metadata">

**Author:** ![bmribler](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/b/a6a055/32.png) [@bmribler](https://forum.hdfgroup.org/u/bmribler)\
**Post date:** [May 13, 2015, 2:26pm UTC](https://forum.hdfgroup.org/t/read-a-compressed-deflate-block-using-java/3428/3 "2015-05-13T14:26:54Z")

</div>

Hi Ahmed,

I understand that you found the problem, but I just want to mention to you about the HDF4 Mapping tool: [http://www.hdfgroup.org/projects/h4map/](http://www.hdfgroup.org/projects/h4map/), which might be useful to your applications. This tool will give you a "map" that describes the structure of an HDF file.

Binh-Minh

> **···**
>
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> From: Hdf-forum \<[hdf-forum-bounces@lists.hdfgroup.org](mailto:hdf-forum-bounces@lists.hdfgroup.org)\> on behalf of Ahmed Eldawy \<[aseldawy@gmail.com](mailto:aseldawy@gmail.com)\>  
> Sent: Wednesday, March 11, 2015 6:47 PM  
> To: hdf-forum  
> Subject: Re: [Hdf-forum] Read a compressed (deflate) block using Java
> 
> Sorry. My bad. I had to use InflaterInputStream instead of DeflaterInputStream. What I was doing was simply recompressing the data that is already compressed. It works fine now.
> 
> Best regards,  
> Ahmed Eldawy
> 
> On Wed, Mar 11, 2015 at 5:36 PM, Ahmed Eldawy \<[aseldawy@gmail.com](mailto:aseldawy@gmail.com)\<[mailto:aseldawy@gmail.com](mailto:aseldawy@gmail.com)\>\> wrote:  
> Hi all,  
> &nbsp;&nbsp;I'm working on a project that deals with HDF4 files provided by NASA. This is part of a big project in University of Minnesota ([http://spatialhadoop.cs.umn.edu/](http://spatialhadoop.cs.umn.edu/)) that uses Hadoop as a distributed environment to process large data including satellite data. I used to use HDFJava wrappers to deal with HDF files and it worked OK. However, since it depends on native libraries, I frequently have troubles distributing the code over a heterogeneous cluster of different architectures and systems. So, I decided to write my own Java-native code to open HDF files. Currently, I'm just writing the minimal code to support the files I'm working on.
> 
> I followed the specifications provided on the website and I was successfully able to get the structure of the HDF file and reach the data blocks that contain the main data (temperature in my case). These blocks are compressed using deflate with level 1 as indicated by the file. However, when I try to decompress these blocks, they do not give me the expected size. I feel that there's some kind of incompatible decompression parameters that I need to fix. Here's the code I wrote to decompress the file
> 
> &nbsp;&nbsp;&nbsp;&nbsp;byte[] compressedData = readRawData(in); // Reads the raw data from the block  
> &nbsp;&nbsp;&nbsp;&nbsp;DeflaterInputStream dis = new DeflaterInputStream(new ByteArrayInputStream(compressedData), new Deflater(1, true)); // Creates a decompressor  
> &nbsp;&nbsp;&nbsp;&nbsp;ByteArrayOutputStream baos = new ByteArrayOutputStream(); // A temporary place to store decompressed data  
> &nbsp;&nbsp;&nbsp;&nbsp;// This loop reads the uncompressed data and caches it in memory  
> &nbsp;&nbsp;&nbsp;&nbsp;byte[] buffer = new byte[4096];  
> &nbsp;&nbsp;&nbsp;&nbsp;int bufferLength;  
> &nbsp;&nbsp;&nbsp;&nbsp;while ((bufferLength = dis.read(buffer)) \> 0) {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;baos.write(buffer, 0, bufferLength);  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;dis.close();  
> &nbsp;&nbsp;&nbsp;&nbsp;baos.close();  
> &nbsp;&nbsp;&nbsp;&nbsp;// Retrieve the decompressed data  
> &nbsp;&nbsp;&nbsp;&nbsp;uncompressedData = baos.toByteArray();
> 
> The header of the compressed data indicate that the size after decompression should be 2880000 bytes, but my code finds that it is 1613097, which is totally different. When creating the deflater, I tried to set the 'nowrap' parameter to true or false but both do not work. The same file works with HDFJava libraries so it should be a bug in my code.
> 
> Thanks in advance for your help. Please let me know if you need more details about this issue.
> 
> Best regards,  
> Ahmed Eldawy
