Распаковка большого двоичного объекта в Java на Google App Engine - PullRequest
3 голосов
/ 21 сентября 2011

Я что-то строю на Google App Engine на Java (JDO). Я программно сжимаю большой байт [] с помощью Deflater, а затем сохраняю сжатый байт [], как в blobstore. Это прекрасно работает:

 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);
    }
}

Используя статические функции compress () и putInBlobStore () в моем классе функций, я могу сжать и сохранить байт [] примерно так:

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

Довольно мило. Я действительно копаю GAE.

Но теперь проблема:

Я храню сжатый HTML, который я хотел бы получить и распаковать на лету, чтобы отобразить в iframe на странице JSP. Сжатие быстрое, но декомпрессия требует ВСЕГДА! Даже когда сжатый HTML составляет 15 КБ, иногда декомпрессия просто умирает.

Вот мой подход к декомпрессии:

 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));

Есть какие-нибудь мысли о том, как лучше всего взять сжатый HTML-код из магазина блобов, распаковать его и отобразить? Даже если мне нужно будет передать его в очередь задач и опросить о завершении, показывая индикатор выполнения - это было бы хорошо. Мне действительно все равно, пока это эффективно и в конечном итоге РАБОТАЕТ. Буду признателен за любые советы, которыми вы можете поделиться со мной здесь.

Спасибо за вашу помощь.

Ответы [ 2 ]

0 голосов
/ 22 сентября 2011

Я взял идею Ника Джонсона и прочитал в Blobstore непосредственно против обслуживания больших объектов.Теперь молниеносно!Вот код:

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";
        }

Я получил класс ChainedBlobstoreInputStream отсюда: Чтение BlobstoreInputStream> = 1 МБ в размере

0 голосов
/ 21 сентября 2011

вы можете посмотреть в RequestBuilder, который работает асинхронно

RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET,"/blobserve?key=" + htmlBlobKey);
try {
requestBuilder.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable exception) {
  GWT.log(exception.getMessage());
}
public void onResponseReceived(Request request, Response response) {
  doSomething(response.getText());//here update your iframe and stop progress indicator
}
});
} catch (RequestException ex) {
  GWT.log(ex.getMessage());
}
...