1

I developed my own rest api using c# servicestack on mono. It works as expected except when it comes to file download. I noticed it appends some bits at the start of file. for example see the image below:this hexa representation of a downloaded image

I filed bug to mono bugzilla, Meanwhile, I want to override image response on my client to remove first appended stuff to make image work. I tried it on c# client by editing received stream before saving it to file and it works fine.

I need to know how and where I can override volley library to get clean images not malformed ones with the best performance.

Update 04:37 PM: I believe I need to modify com.android.volley.toolbox.ImageRequest >> I will try it and post solution if it works with me.

Regards, Shaheen

Shaheen
  • 141
  • 2
  • 6
  • The extra bytes may be because the response is being returned with Chunked encoding. Is there a `Transfer-Encoding: chunked` header in the response? See [this post](http://stackoverflow.com/q/17755322/795339) and [this post](http://stackoverflow.com/q/17757875/795339) for more info. – Mike Mertsock Nov 12 '13 at 04:38
  • thanks esker, yes this is the same problem. I will try xamiran bug solution. – Shaheen Nov 12 '13 at 06:45

1 Answers1

1

I modified the method doParse in com.android.volley.toolbox.ImageRequest

private Response<Bitmap> doParse(NetworkResponse response){

        byte[] data = response.data;
        byte [] pattern = fromHexString("FFD8FFE000");
        int position = matchPosition(data,pattern);
        if(position>0)
            data = Arrays.copyOfRange(data, position, data.length-position);
        ....
        ....
        ....
        ....
       ..}

here is the helper methods I used:

  public static int matchPosition(byte [] a, byte [] b){
      int matchLength=0;
      int maxSearch = 30>a.length?a.length:30;
    for (int i =0;i<maxSearch;i++) {
      if (a[i]==b[0] && i+b.length<a.length){
          for(int j = 0;j< b.length;j++ )
          {
              if((i+j)==a.length-1)
                  return -1;
              if(a[i+j]==b[j])
                  matchLength++;
          }
          if(matchLength == b.length)
              return i;
          else
              matchLength = 0; 
      }
    }
    return -1; 
  }


private static byte[] fromHexString(final String encoded) {
    if ((encoded.length() % 2) != 0)
        throw new IllegalArgumentException("Input string must contain an even number of characters");

    final byte result[] = new byte[encoded.length()/2];
    final char enc[] = encoded.toCharArray();
    for (int i = 0; i < enc.length; i += 2) {
        StringBuilder curr = new StringBuilder(2);
        curr.append(enc[i]).append(enc[i + 1]);
        result[i/2] = (byte) Integer.parseInt(curr.toString(), 16);
    }
    return result;
}

and this work around resolved the issue explained above!

Shaheen
  • 141
  • 2
  • 6