Esempio n. 1
0
  KrollDict createDictForImage(int width, int height, byte[] data) {
    KrollDict d = new KrollDict();

    d.put("x", 0);
    d.put("y", 0);
    d.put("width", width);
    d.put("height", height);

    KrollDict cropRect = new KrollDict();
    cropRect.put("x", 0);
    cropRect.put("y", 0);
    cropRect.put("width", width);
    cropRect.put("height", height);
    d.put("cropRect", cropRect);
    d.put("mediaType", MEDIA_TYPE_PHOTO);
    d.put("media", TiBlob.blobFromData(data, "image/png"));

    return d;
  }
Esempio n. 2
0
  @Override
  public TiBlob read() throws IOException {
    ByteArrayOutputStream baos = null;
    InputStream in = null;
    try {
      baos = new ByteArrayOutputStream();
      in = getInputStream();
      byte buffer[] = new byte[4096];
      while (true) {
        int count = in.read(buffer);
        if (count < 0) {
          break;
        }
        baos.write(buffer, 0, count);
      }
    } finally {
      if (in != null) {
        in.close();
      }
    }

    return TiBlob.blobFromData(
        getTiContext(), baos.toByteArray(), TiMimeTypeHelper.getMimeType(toURL()));
  }
Esempio n. 3
0
 private void setResponseData(HttpEntity entity) throws IOException, ParseException {
   if (entity != null) {
     responseData = TiBlob.blobFromData(proxy.getTiContext(), EntityUtils.toByteArray(entity));
     charset = EntityUtils.getContentCharSet(entity);
   }
 }
Esempio n. 4
0
    public String handleResponse(HttpResponse response) throws HttpResponseException, IOException {
      connected = true;
      String clientResponse = null;

      if (client != null) {
        TiHTTPClient c = client.get();
        if (c != null) {
          c.response = response;
          c.setReadyState(READY_STATE_HEADERS_RECEIVED);
          c.setStatus(response.getStatusLine().getStatusCode());
          c.setStatusText(response.getStatusLine().getReasonPhrase());
          c.setReadyState(READY_STATE_LOADING);
        }

        if (DBG) {
          try {
            Log.w(LCAT, "Entity Type: " + response.getEntity().getClass());
            Log.w(LCAT, "Entity Content Type: " + response.getEntity().getContentType().getValue());
            Log.w(LCAT, "Entity isChunked: " + response.getEntity().isChunked());
            Log.w(LCAT, "Entity isStreaming: " + response.getEntity().isStreaming());
          } catch (Throwable t) {
            // Ignore
          }
        }

        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() >= 300) {
          setResponseText(response.getEntity());
          throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
        }

        entity = response.getEntity();
        if (entity.getContentType() != null) {
          contentType = entity.getContentType().getValue();
        }
        KrollCallback onDataStreamCallback = c.getCallback(ON_DATA_STREAM);
        if (onDataStreamCallback != null) {
          is = entity.getContent();
          charset = EntityUtils.getContentCharSet(entity);

          responseData = null;

          if (is != null) {
            final KrollCallback cb = onDataStreamCallback;
            long contentLength = entity.getContentLength();
            if (DBG) {
              Log.d(LCAT, "Content length: " + contentLength);
            }
            int count = 0;
            int totalSize = 0;
            byte[] buf = new byte[4096];
            if (DBG) {
              Log.d(LCAT, "Available: " + is.available());
            }
            if (aborted) {
              if (entity != null) {
                entity.consumeContent();
              }
            } else {
              while ((count = is.read(buf)) != -1) {
                totalSize += count;
                TiDict o = new TiDict();
                o.put("totalCount", contentLength);
                o.put("totalSize", totalSize);
                o.put("size", count);

                byte[] newbuf = new byte[count];
                System.arraycopy(buf, 0, newbuf, 0, count);
                if (responseData == null) {
                  responseData = TiBlob.blobFromData(proxy.getTiContext(), newbuf, contentType);
                } else {
                  responseData.append(TiBlob.blobFromData(proxy.getTiContext(), newbuf));
                }

                TiBlob blob = TiBlob.blobFromData(proxy.getTiContext(), newbuf);
                o.put("blob", blob);
                o.put("progress", ((double) totalSize) / ((double) contentLength));

                cb.callWithProperties(o);
              }
              if (entity != null) {
                try {
                  entity.consumeContent();
                } catch (IOException e) {
                  e.printStackTrace();
                }
              }
            }
          }
        } else {
          setResponseData(entity);
        }
      }
      return clientResponse;
    }