private void writeToFromFile(OutputStream os, RequestParams.FileWrapper wrapper)
      throws IOException {

    // Send the meta data.
    writeMetaData(os, wrapper.file.getName(), wrapper.contentType);

    int bytesRead;
    long bytesWritten = 0, totalSize = wrapper.file.length();

    // Open the file for reading.
    FileInputStream in = new FileInputStream(wrapper.file);

    // Upload the file's contents in Base64.
    Base64OutputStream bos = new Base64OutputStream(os, Base64.NO_CLOSE | Base64.NO_WRAP);

    // Read from file until no more data's left to read.
    while ((bytesRead = in.read(buffer)) != -1) {
      bos.write(buffer, 0, bytesRead);
      bytesWritten += bytesRead;
      progressHandler.sendProgressMessage(bytesWritten, totalSize);
    }

    // Close the Base64 output stream.
    AsyncHttpClient.silentCloseOutputStream(bos);

    // End the meta data.
    endMetaData(os);

    // Safely close the input stream.
    AsyncHttpClient.silentCloseInputStream(in);
  }
  private void writeToFromStream(OutputStream os, RequestParams.StreamWrapper entry)
      throws IOException {

    // Send the meta data.
    writeMetaData(os, entry.name, entry.contentType);

    int bytesRead;

    // Upload the file's contents in Base64.
    Base64OutputStream bos = new Base64OutputStream(os, Base64.NO_CLOSE | Base64.NO_WRAP);

    // Read from input stream until no more data's left to read.
    while ((bytesRead = entry.inputStream.read(buffer)) != -1) {
      bos.write(buffer, 0, bytesRead);
    }

    // Close the Base64 output stream.
    AsyncHttpClient.silentCloseOutputStream(bos);

    // End the meta data.
    endMetaData(os);

    // Close input stream.
    if (entry.autoClose) {
      // Safely close the input stream.
      AsyncHttpClient.silentCloseInputStream(entry.inputStream);
    }
  }
Example #3
0
 /**
  * Tests Base64OutputStream.write(null).
  *
  * @throws Exception for some failure scenarios.
  */
 @Test
 public void testWriteToNullCoverage() throws Exception {
   ByteArrayOutputStream bout = new ByteArrayOutputStream();
   Base64OutputStream out = new Base64OutputStream(bout);
   try {
     out.write(null, 0, 0);
     fail("Expcted Base64OutputStream.write(null) to throw a NullPointerException");
   } catch (NullPointerException e) {
     // Expected
   }
 }
Example #4
0
  /**
   * Test the Base64OutputStream implementation against the special NPE inducing input identified in
   * the CODEC-98 bug.
   *
   * @throws Exception for some failure scenarios.
   */
  @Test
  public void testCodec98NPE() throws Exception {
    byte[] codec98 = StringUtils.getBytesUtf8(Base64TestData.CODEC_98_NPE);
    byte[] codec98_1024 = new byte[1024];
    System.arraycopy(codec98, 0, codec98_1024, 0, codec98.length);
    ByteArrayOutputStream data = new ByteArrayOutputStream(1024);
    Base64OutputStream stream = new Base64OutputStream(data, false);
    stream.write(codec98_1024, 0, 1024);
    stream.close();

    byte[] decodedBytes = data.toByteArray();
    String decoded = StringUtils.newStringUtf8(decodedBytes);
    assertEquals("codec-98 NPE Base64OutputStream", Base64TestData.CODEC_98_NPE_DECODED, decoded);
  }
Example #5
0
  /**
   * Tests Base64OutputStream.write for expected IndexOutOfBoundsException conditions.
   *
   * @throws Exception for some failure scenarios.
   */
  @Test
  public void testWriteOutOfBounds() throws Exception {
    byte[] buf = new byte[1024];
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    Base64OutputStream out = new Base64OutputStream(bout);

    try {
      out.write(buf, -1, 1);
      fail("Expected Base64OutputStream.write(buf, -1, 1) to throw a IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException ioobe) {
      // Expected
    }

    try {
      out.write(buf, 1, -1);
      fail("Expected Base64OutputStream.write(buf, 1, -1) to throw a IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException ioobe) {
      // Expected
    }

    try {
      out.write(buf, buf.length + 1, 0);
      fail(
          "Expected Base64OutputStream.write(buf, buf.length + 1, 0) to throw a IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException ioobe) {
      // Expected
    }

    try {
      out.write(buf, buf.length - 1, 2);
      fail(
          "Expected Base64OutputStream.write(buf, buf.length - 1, 2) to throw a IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException ioobe) {
      // Expected
    }
  }