Exemplo n.º 1
0
 @Override
 protected Bitmap doInBackground(String... strings) {
   String url = strings[0];
   Bitmap bm = null;
   try {
     URL aURL = new URL(url);
     URLConnection conn = aURL.openConnection();
     conn.connect();
     InputStream is = conn.getInputStream();
     int totalLen = conn.getContentLength();
     InputStreamWrapper bis = new InputStreamWrapper(is, 8192, totalLen);
     bis.setProgressListener(
         new InputStreamWrapper.InputStreamProgressListener() {
           @Override
           public void onProgress(float progressValue, long bytesLoaded, long bytesTotal) {
             publishProgress((int) (progressValue * 100));
           }
         });
     bm = BitmapFactory.decodeStream(bis);
     bis.close();
     is.close();
   } catch (Exception e) {
     e.printStackTrace();
   }
   return bm;
 }
Exemplo n.º 2
0
  @Test
  public void readShouldReadInputStreamCorrectlyAndShouldCloseStream() throws Exception {
    // Read content shorter than buffer size ...
    String content = "This is the way to grandma's house.";
    InputStream stream = new ByteArrayInputStream(content.getBytes());
    InputStreamWrapper wrapper = new InputStreamWrapper(stream);
    assertThat(wrapper.isClosed(), is(false));
    assertThat(IoUtil.read(wrapper), is(content));
    assertThat(wrapper.isClosed(), is(true));

    // Read content longer than buffer size ...
    for (int i = 0; i != 10; ++i) {
      content += content; // note this doubles each time!
    }
    stream = new ByteArrayInputStream(content.getBytes());
    wrapper = new InputStreamWrapper(stream);
    assertThat(wrapper.isClosed(), is(false));
    assertThat(IoUtil.read(wrapper), is(content));
    assertThat(wrapper.isClosed(), is(true));
  }
 @Override
 protected Bitmap doInBackground(String... strings) {
   String path = strings[0];
   Bitmap bm = null;
   try {
     File file = new File(path);
     FileInputStream fis = new FileInputStream(file);
     InputStreamWrapper bis = new InputStreamWrapper(fis, 8192, file.length());
     bis.setProgressListener(
         new InputStreamProgressListener() {
           public void onProgress(float progressValue, long bytesLoaded, long bytesTotal) {
             publishProgress((int) (progressValue * 100));
           }
         });
     bm = BitmapFactory.decodeStream(bis);
     bis.close();
   } catch (Exception e) {
     e.printStackTrace();
   }
   return bm;
 }
Exemplo n.º 4
0
  public void testSetInput() throws Exception {
    XmlPullParser xpp = factory.newPullParser();
    assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES));

    // another test

    byte[] binput = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo/>").getBytes("UTF-8");
    InputStreamWrapper isw = new InputStreamWrapper(new ByteArrayInputStream(binput));
    assertEquals("no read() called in just contructed reader", false, isw.calledRead());

    xpp.setInput(isw, "UTF-8");
    assertEquals("UTF-8", xpp.getInputEncoding());
    checkParserStateNs(xpp, 0, XmlPullParser.START_DOCUMENT, null, 0, null, null, null, false, -1);
    assertEquals("read() not called before next()", false, isw.calledRead());

    xpp.nextToken();
    assertEquals("read() must be called after next()", true, isw.calledRead());

    // needs to resolve:
    // java.lang.InternalError: Converter malfunction (UTF-16) -- please submit a bug report via
    // http://java.sun.com/cgi-bin/bugreport.cgi

    //
    //      // add BOM
    //        //byte[] binput1 = new byte[]{((byte)'\u00FE'), ((byte)'\u00FF')};
    //        //byte[] binput1 = new byte[]{((byte)'\u00FF'), ((byte)'\u00FE')};
    //      byte[] binput1 = new byte[0];
    //        byte[] binput2 =
    //            ("<?xml version=\"1.0\" encoding=\"UTF16\"?><foo/>").getBytes("UTF16");
    //        binput = new byte[ binput1.length + binput2.length ] ;
    //        System.arraycopy(binput1, 0, binput, 0, binput1.length);
    //        System.arraycopy(binput2, 0, binput, binput1.length, binput2.length);
    //        isw = new InputStreamWrapper(
    //            new ByteArrayInputStream( binput ));
    //        assertEquals("no read() called in just contructed reader", false, isw.calledRead());
    //
    //        //xpp.setInput(isw, "UTF-16" ); //TODO why Xerces2 causes java Unicode decoder to fail
    // ????
    //      xpp.setInput(isw, "UTF16" );
    //        //assertEquals("UTF-16", xpp.getInputEncoding());
    //        checkParserStateNs(xpp, 0, XmlPullParser.START_DOCUMENT, null, 0, null, null, null,
    // false, -1);
    //        assertEquals("read() not called before next()", false, isw.calledRead());
    //
    //        xpp.nextToken();
    //        assertEquals("read() must be called after next()", true, isw.calledRead());
    //
    // check input detecting  -- for mutlibyte sequences ...
    final String FEATURE_DETECT_ENCODING =
        "http://xmlpull.org/v1/doc/features.html#detect-encoding";
    if (xpp.getFeature(FEATURE_DETECT_ENCODING)) {

      PackageTests.addNote("* optional feature  " + FEATURE_DETECT_ENCODING + " is supported\n");

      isw = new InputStreamWrapper(new ByteArrayInputStream(binput));
      assertEquals("no read() called in just contructed reader", false, isw.calledRead());

      xpp.setInput(isw, null);
      assertEquals(null, xpp.getInputEncoding());

      checkParserStateNs(
          xpp, 0, XmlPullParser.START_DOCUMENT, null, 0, null, null, null, false, -1);
      // assertEquals("read() not called before next()", false, isw.calledRead());

      xpp.nextToken();
      assertEquals("read() must be called after next()", true, isw.calledRead());
      assertEquals("UTF16", xpp.getInputEncoding());
    }

    // check input detecting  -- default
    binput = ("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><foo/>").getBytes("ISO-8859-1");
    isw = new InputStreamWrapper(new ByteArrayInputStream(binput));
    assertEquals("no read() called in just contructed reader", false, isw.calledRead());

    xpp.setInput(isw, null);
    assertEquals(null, xpp.getInputEncoding());
    checkParserStateNs(xpp, 0, XmlPullParser.START_DOCUMENT, null, 0, null, null, null, false, -1);
    // assertEquals("read() not called before next()", false, isw.calledRead());

    xpp.next();
    assertEquals("read() must be called after next()", true, isw.calledRead());
    checkParserStateNs(
        xpp, 1, XmlPullParser.START_TAG, null, 0, "", "foo", null, true /*empty*/, 0);

    if (xpp.getFeature(FEATURE_DETECT_ENCODING)) {
      assertEquals("ISO-8859-1", xpp.getInputEncoding());
    }
  }