My work around to fetch data even when I don't know the exact filesize or 
ending EOF.

  private byte[] getImageBytes(BlobData blobData) {
    if (blobData == null) {
      return null;
    }

    BlobKey blobKey = new BlobKey(blobData.getKey());
    if (blobKey == null) {
      return null;
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    long filesize = blobData.getSize();
    int chunkSize = 1024;
    long offset = 0;
    while (offset < filesize) {

      long limit = offset + chunkSize - 1;
      if (filesize < limit) {
        limit = filesize - 1;
      }

      System.out.println("offset=" + offset + " limit=" + limit + " size=" + 
filesize);

      byte[] b = null;
      try {
        b = blobstoreService.fetchData(blobKey, offset, limit);
      } catch (Exception e) {
        //e.printStackTrace();
        System.out.println("Ga_Service_Image.getImageBytes(): doing 
workaround");
        workaround(out, blobKey, offset, filesize);
        break;
      }
      try {
        if (b != null) {
          out.write(b);
        }
      } catch (IOException e) {
        e.printStackTrace();
        return null;
      }

      offset += chunkSize;
      if (offset > filesize) {
        offset = filesize;
      }
    }

    byte[] filebytes = out.toByteArray();

    System.out.println("getImageBytes(): filebytes size: " + filebytes.length + 
" blobData.size=" + blobData.getSize());

    return filebytes;
  }

  private void workaround(ByteArrayOutputStream out, BlobKey blobKey, long 
offset, long filesize) {
    int chunkSize = 1;
    long limit = 0;
    while (offset < filesize) {

      limit = offset + chunkSize - 1;
      if (filesize < limit) {
        limit = filesize;
      }

      System.out.println("offset=" + offset + " limit=" + limit + " size=" + 
filesize);

      byte[] b = null;
      try {
        b = blobstoreService.fetchData(blobKey, offset, limit);
      } catch (Exception e) {
        //e.printStackTrace();
        return;
      }
      try {
        out.write(b);
      } catch (IOException e) {
        e.printStackTrace();
        return;
      }

      offset += chunkSize;
      if (offset > filesize) {
        offset = filesize;
      }
    }
  }

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-java@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to