Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6595953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:00:32+00:00 2026-05-25T18:00:32+00:00

I’m building something on Google App Engine in Java (JDO). I’m programmatically compressing a

  • 0

I’m building something on Google App Engine in Java (JDO). I’m programmatically compressing a large byte[] with Deflater and then storing the compressed byte[] as in the blobstore. This works great:

 public class Functions {

public static byte[] compress(byte[] input) throws UnsupportedEncodingException, IOException, MessagingException
    {

        Deflater df = new Deflater();       //this function mainly generate the byte code
        df.setLevel(Deflater.BEST_COMPRESSION);
        df.setInput(input);

        ByteArrayOutputStream baos = new ByteArrayOutputStream(input.length);   //we write the generated byte code in this array
        df.finish();
        byte[] buff = new byte[1024];   //segment segment pop....segment set 1024
        while(!df.finished())
        {
            int count = df.deflate(buff);       //returns the generated code... index
            baos.write(buff, 0, count);     //write 4m 0 to count
        }
        baos.close();

        int baosLength = baos.toByteArray().length;
        int inputLength = input.length;
        //System.out.println("Original: "+inputLength);
        // System.out.println("Compressed: "+ baosLength);

        return baos.toByteArray();

    }

 public static byte[] decompress(byte[] input) throws UnsupportedEncodingException, IOException, DataFormatException
    {

        Inflater decompressor = new Inflater();
        decompressor.setInput(input);

        // Create an expandable byte array to hold the decompressed data
        ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

        // Decompress the data
        byte[] buf = new byte[1024];
        while (!decompressor.finished()) {
            try {
                int count = decompressor.inflate(buf);
                bos.write(buf, 0, count);
            } catch (DataFormatException e) {
            }
        }
        try {
            bos.close();
        } catch (IOException e) {
        }

        // Get the decompressed data
        byte[] decompressedData = bos.toByteArray();

        return decompressedData;


    }

 public static BlobKey putInBlobStore(String contentType, byte[] filebytes) throws IOException {

        // Get a file service
          FileService fileService = FileServiceFactory.getFileService();


          AppEngineFile file = fileService.createNewBlobFile(contentType);

          // Open a channel to write to it
          boolean lock = true;
          FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);

          // This time we write to the channel using standard Java
          BufferedInputStream in = new BufferedInputStream(new ByteArrayInputStream(filebytes));
          byte[] buffer;
          int defaultBufferSize = 524288;
          if(filebytes.length > defaultBufferSize){
              buffer = new byte[defaultBufferSize]; // 0.5 MB buffers
          }
          else{
              buffer = new byte[filebytes.length]; // buffer the size of the data
          }

            int read;
            while( (read = in.read(buffer)) > 0 ){ //-1 means EndOfStream
                System.out.println(read);
                if(read < defaultBufferSize){
                    buffer = new byte[read];
                }
                ByteBuffer bb = ByteBuffer.wrap(buffer);
                writeChannel.write(bb);
            }
            writeChannel.closeFinally();

        return fileService.getBlobKey(file);
    }
}

Using the static compress() and putInBlobStore() functions in my Functions class, I can compress and store a byte[] like so:

BlobKey dataBlobKey =  Functions.putInBlobStore("MULTIPART_FORM_DATA", Functions.compress(orginalDataByteArray));

Pretty sweet. I’m really digging GAE.

But now, the problem:

I’m storing compressed HTML which I’d like to retrieve and decompress on the fly to display in an iframe within a JSP page. Compression is quick, but decompression takes FOREVER! Even when the compressed HTML is 15k, sometimes the decompression just dies.

Here’s my decompression approach:

 URL file = new URL("/blobserve?key=" + htmlBlobKey);
         URLConnection conn = file.openConnection();
         conn.setReadTimeout(30000);
         conn.setConnectTimeout(30000);
         InputStream inputStream = conn.getInputStream();
         byte[] data = IOUtils.toByteArray(inputStream);
         return new String(Functions.decompress(data));

Any thoughts on how best to take compressed HTML from the blobstore, decompress it and display it? Even if I need to pass it to a task queue and poll for completion while showing a progress bar – that would be fine. I truly don’t care, so long as it’s efficient and ultimately WORKS. I’d appreciate any guidance you can share with me here.

Thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:00:32+00:00Added an answer on May 25, 2026 at 6:00 pm

    I took Nick Johnson’s idea and read from the Blobstore directly vs serving the blobs. Now it’s lightning fast! Here’s the code:

    try{
            ChainedBlobstoreInputStream inputStream = new ChainedBlobstoreInputStream(this.getHtmlBlobKey());
            //StringWriter writer = new StringWriter();
             byte[] data = IOUtils.toByteArray(inputStream);
             return new String(Functions.decompress(Encrypt.AESDecrypt(data)));
             //return new String(data);
        } 
        catch(Exception e){
                return "No HTML Version";
            }
    

    I got the ChainedBlobstoreInputStream class from here:
    Reading a BlobstoreInputStream >= 1MB in size

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I am writing an app with both english and french support. The app requests
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.