Search in sources :

Example 1 with ReadFromSupport

use of org.apache.axiom.ext.io.ReadFromSupport in project webservices-axiom by apache.

the class IOUtils method copy.

/**
     * Copy bytes between streams. This method supports the {@link ReadFromSupport} interface. It
     * will not call {@link Closeable#close()} on either of the two streams.
     * 
     * @param in
     *            the stream to read bytes from
     * @param out
     *            the stream to write bytes to
     * @param length
     *            the maximum number of bytes to copy, or -1 to copy an unlimited number of bytes
     * @return the number of bytes copied
     * @throws StreamCopyException
     *             if a read/write operation on one of the streams triggered an {@link IOException}
     */
public static long copy(InputStream in, OutputStream out, long length) throws StreamCopyException {
    if (out instanceof ReadFromSupport) {
        return ((ReadFromSupport) out).readFrom(in, length);
    } else {
        byte[] buffer = new byte[4096];
        long read = 0;
        long toRead = length == -1 ? Long.MAX_VALUE : length;
        while (toRead > 0) {
            int c;
            try {
                c = in.read(buffer, 0, (int) Math.min(toRead, buffer.length));
            } catch (IOException ex) {
                throw new StreamCopyException(StreamCopyException.READ, ex);
            }
            if (c == -1) {
                break;
            }
            try {
                out.write(buffer, 0, c);
            } catch (IOException ex) {
                throw new StreamCopyException(StreamCopyException.WRITE, ex);
            }
            read += c;
            toRead -= c;
        }
        return read;
    }
}
Also used : ReadFromSupport(org.apache.axiom.ext.io.ReadFromSupport) StreamCopyException(org.apache.axiom.ext.io.StreamCopyException) IOException(java.io.IOException)

Example 2 with ReadFromSupport

use of org.apache.axiom.ext.io.ReadFromSupport in project webservices-axiom by apache.

the class TestReadFromSupport method runTest.

@Override
protected void runTest(WritableBlob blob) throws Throwable {
    int chunkSize = size / 4;
    byte[] content = new byte[chunkSize * 4];
    new Random().nextBytes(content);
    OutputStream out = blob.getOutputStream();
    try {
        out.write(Arrays.copyOfRange(content, 0, chunkSize));
        ((ReadFromSupport) out).readFrom(new ByteArrayInputStream(Arrays.copyOfRange(content, chunkSize, chunkSize * 2)), -1);
        ((ReadFromSupport) out).readFrom(new ByteArrayInputStream(Arrays.copyOfRange(content, chunkSize * 2, chunkSize * 4)), chunkSize);
        out.write(content, chunkSize * 3, chunkSize);
    } finally {
        out.close();
    }
    InputStream in = blob.getInputStream();
    try {
        assertThat(IOUtils.toByteArray(in)).isEqualTo(content);
    } finally {
        in.close();
    }
}
Also used : Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ReadFromSupport(org.apache.axiom.ext.io.ReadFromSupport)

Example 3 with ReadFromSupport

use of org.apache.axiom.ext.io.ReadFromSupport in project webservices-axiom by apache.

the class TestWriteAfterCommit method runTest.

@Override
protected void runTest(WritableBlob blob) throws Throwable {
    OutputStream out = blob.getOutputStream();
    out.close();
    try {
        out.write(new byte[10]);
        fail("Expected exception");
    } catch (IllegalStateException ex) {
    // OK
    } catch (IOException ex) {
    // OK
    }
    try {
        out.write(new byte[10], 3, 5);
        fail("Expected exception");
    } catch (IllegalStateException ex) {
    // OK
    } catch (IOException ex) {
    // OK
    }
    try {
        out.write(0);
        fail("Expected exception");
    } catch (IllegalStateException ex) {
    // OK
    } catch (IOException ex) {
    // OK
    }
    if (out instanceof ReadFromSupport) {
        try {
            ((ReadFromSupport) out).readFrom(new NullInputStream(10), -1);
            fail("Expected exception");
        } catch (IllegalStateException ex) {
        // OK
        } catch (IOException ex) {
        // OK
        }
    }
}
Also used : OutputStream(java.io.OutputStream) ReadFromSupport(org.apache.axiom.ext.io.ReadFromSupport) IOException(java.io.IOException) NullInputStream(org.apache.commons.io.input.NullInputStream)

Aggregations

ReadFromSupport (org.apache.axiom.ext.io.ReadFromSupport)3 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Random (java.util.Random)1 StreamCopyException (org.apache.axiom.ext.io.StreamCopyException)1 NullInputStream (org.apache.commons.io.input.NullInputStream)1