05-24-2009 06:46 PM
In my application, I have a few static string constants that are very long. The strings will be displayed on screen when given buttons are clicked. I find out that once I added these strings, the cod file size increased by 30% which is undesirable. I expect those buttons will rarely be clicked but I have to have those text for viewing.
Is there a better way to handle these strings so that they do not inflate cod file, e.g., compress them.
Thanks in advance.
Solved! Go to Solution.
05-25-2009 01:49 AM
05-25-2009 02:20 AM
05-10-2010 11:15 AM - edited 05-10-2010 11:16 AM
public String CompressData(String dataToCom)
{
byte[] data23 = dataToCom.getBytes();
System.out.print(" data23.length()"+ data23.length);
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzipStream = new GZIPOutputStream( baos, 6, GZIPOutputStream.MAX_LOG2_WINDOW_LENGTH );
gzipStream.write( data23 );
gzipStream.close();
byte[] compressedData = baos.toByteArray();
String CompressedDataString = new String (compressedData);
return CompressedDataString;
}
catch (Exception e)
{
System.out.print("Exceptiomn"+e);
}
return null;
}
public String DecompressData(String dataToDecompress)
{
try
{
byte[] data= dataToDecompress.getBytes();
// the Data.leng must be as same as the original String length not as the copressered data length .....
ByteArrayInputStream bais= new ByteArrayInputStream(data,0,data.length);
GZIPInputStream gzis = new GZIPInputStream(bais);
StringBuffer sb = new StringBuffer();
int c;
while ((c = gzis.read()) != -1)
{
char cc = (char)c;
sb.append(cc);
}
String DecompresedData = sb.toString();
return DecompresedData;
}
catch(Exception e)
{
System.out.println(e.toString());
}
return null;
}