• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

HELP - GZIPInputStream

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
In my code I write to a file using GZIPInputStream. Now I want to read the contents of this back into a byte array. When I use myGZip.available() to specify the size of my array, it only reads in the first char in the file - does anyone know why?
or does anyone know how I can specify the size of my byte array without using available()?
Thanks in advance,
D
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/D
GZIPInputStream::available() overrides InputStream::available(). In fact GZIPInputStream::available() returns 0 if you are at the EOF or 1 if everything is OK, thus the reason your byte array consists of one char. You have to make sure to use InputStream::available() and not GZIPInputStream::available.
It's InputStream::availble() that actually tells you how many bytes are available.
-Bryan :-)

Originally posted by Dave Donohoe:
Hi all,
In my code I write to a file using GZIPInputStream. Now I want to read the contents of this back into a byte array. When I use myGZip.available() to specify the size of my array, it only reads in the first char in the file - does anyone know why?
or does anyone know how I can specify the size of my byte array without using available()?
Thanks in advance,
D


 
Bryan Fagan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add to what I said earlier, because we don't know how big a file might be, it's probably better to use a Collection like an java.util.ArrayList. An ArrayList can grow as needed.
Note: I used a regular Zip Format and not the GZIP format.
Here is some code below describing what I mean.
/**
* Test reading from a zip file and creating a Byte array
*
*/
import java.io.*;
import java.util.*;
import java.util.zip.*;
public class RZip extends ZipInputStream
{
private ArrayList aByteArray;
private int len;
private InputStream zin;
public RZip( InputStream ins ) throws IOException
{
super( ins );
zin = this.in;
aByteArray = new ArrayList();
}
/**
* Create an array of bytes with ArrayList Collection.
*/
public void createArrayOfBytes() throws IOException
{

int len = 0;
while( ( len = zin.read() ) != -1 )
{
aByteArray.add( new Byte( ( byte )len ) );
}
}
/**
* print out array of bytes.
*/
public void printOutArrayOfBytes()
{
for( int i=0;i<aByteArray.size();i++ )>
{
System.out.println("Byte: " + aByteArray.get( i ).toString() );
}
}
public static void main(String[] args)
{
try
{
ZipFile zFile = new ZipFile( "d:\\java\\test\\Test.zip" );
ZipEntry zEntry = zFile.getEntry( "Test.txt" );
InputStream i = zFile.getInputStream( zEntry );

RZip rZip = new RZip( i );
rZip.createArrayOfBytes();
rZip.printOutArrayOfBytes();
}
catch( Exception e )
{
System.err.println( "MAIN::ERROR: " + e.getMessage() );
System.exit( 1 );
}
}
}
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic