Search in sources :

Example 1 with StreamCopyException

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

the class DetachableInputStream method detach.

@Override
public void detach() throws OMException {
    MemoryBlob blob = Blobs.createMemoryBlob();
    try {
        blob.readFrom(target);
    } catch (StreamCopyException ex) {
        throw new OMException(ex.getCause());
    }
    if (closeOnDetach) {
        try {
            target.close();
        } catch (IOException ex) {
            throw new OMException(ex);
        }
    }
    target = blob.readOnce();
}
Also used : MemoryBlob(org.apache.axiom.blob.MemoryBlob) StreamCopyException(org.apache.axiom.ext.io.StreamCopyException) IOException(java.io.IOException) OMException(org.apache.axiom.om.OMException)

Example 2 with StreamCopyException

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

the class OverflowBlob method readFrom.

@Override
public long readFrom(InputStream in, long length, boolean commit) throws StreamCopyException {
    // TODO: this will not work if the blob is in state UNCOMMITTED and we have already switched to a temporary file
    long read = 0;
    long toRead = length == -1 ? Long.MAX_VALUE : length;
    while (true) {
        int c;
        try {
            int len = chunkSize - chunkOffset;
            if (len > toRead) {
                len = (int) toRead;
            }
            c = in.read(getCurrentChunk(), chunkOffset, len);
        } catch (IOException ex) {
            throw new StreamCopyException(StreamCopyException.READ, ex);
        }
        if (c == -1) {
            break;
        }
        read += c;
        toRead -= c;
        chunkOffset += c;
        if (chunkOffset == chunkSize) {
            chunkIndex++;
            chunkOffset = 0;
            if (chunkIndex == chunks.length) {
                FileOutputStream fileOutputStream;
                try {
                    fileOutputStream = switchToTempFile();
                } catch (IOException ex) {
                    throw new StreamCopyException(StreamCopyException.WRITE, ex);
                }
                byte[] buf = new byte[4096];
                while (true) {
                    int c2;
                    try {
                        c2 = in.read(buf, 0, (int) Math.min(toRead, 4096));
                    } catch (IOException ex) {
                        throw new StreamCopyException(StreamCopyException.READ, ex);
                    }
                    if (c2 == -1) {
                        break;
                    }
                    try {
                        fileOutputStream.write(buf, 0, c2);
                    } catch (IOException ex) {
                        throw new StreamCopyException(StreamCopyException.WRITE, ex);
                    }
                    read += c2;
                    toRead -= c2;
                }
                try {
                    fileOutputStream.close();
                } catch (IOException ex) {
                    throw new StreamCopyException(StreamCopyException.WRITE, ex);
                }
                break;
            }
        }
    }
    state = commit ? STATE_COMMITTED : STATE_UNCOMMITTED;
    return read;
}
Also used : FileOutputStream(java.io.FileOutputStream) StreamCopyException(org.apache.axiom.ext.io.StreamCopyException) IOException(java.io.IOException)

Example 3 with StreamCopyException

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

the class MemoryBlobOutputStream method readFrom.

@Override
public long readFrom(InputStream in, long length) throws StreamCopyException {
    if (chunk == null) {
        throw new IllegalStateException();
    }
    long read = 0;
    long toRead = length == -1 ? Long.MAX_VALUE : length;
    while (toRead > 0) {
        updateChunk();
        int c;
        try {
            c = in.read(chunk.buffer, chunk.size, (int) Math.min(toRead, chunk.buffer.length - chunk.size));
        } catch (IOException ex) {
            throw new StreamCopyException(StreamCopyException.READ, ex);
        }
        if (c == -1) {
            break;
        }
        chunk.size += c;
        read += c;
        toRead -= c;
    }
    return read;
}
Also used : StreamCopyException(org.apache.axiom.ext.io.StreamCopyException) IOException(java.io.IOException)

Example 4 with StreamCopyException

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

the class OverflowableBlobImpl method readFrom.

long readFrom(InputStream in, long length, boolean commit) throws StreamCopyException {
    if (state == State.COMMITTED) {
        throw new IllegalStateException();
    }
    long read = 0;
    long toRead = length == -1 ? Long.MAX_VALUE : length;
    while (toRead > 0) {
        if (overflowOutputStream != null) {
            read += IOUtils.copy(in, overflowOutputStream, toRead);
            break;
        } else if (chunkIndex == chunks.length) {
            try {
                switchToOverflowBlob();
            } catch (IOException ex) {
                throw new StreamCopyException(StreamCopyException.WRITE, ex);
            }
        } else {
            int c;
            try {
                int len = chunkSize - chunkOffset;
                if (len > toRead) {
                    len = (int) toRead;
                }
                c = in.read(getCurrentChunk(), chunkOffset, len);
            } catch (IOException ex) {
                throw new StreamCopyException(StreamCopyException.READ, ex);
            }
            if (c == -1) {
                break;
            }
            read += c;
            toRead -= c;
            chunkOffset += c;
            if (chunkOffset == chunkSize) {
                chunkIndex++;
                chunkOffset = 0;
            }
        }
    }
    if (commit && overflowOutputStream != null) {
        try {
            overflowOutputStream.close();
        } catch (IOException ex) {
            throw new StreamCopyException(StreamCopyException.WRITE, ex);
        }
    }
    state = commit ? State.COMMITTED : State.UNCOMMITTED;
    return read;
}
Also used : StreamCopyException(org.apache.axiom.ext.io.StreamCopyException) IOException(java.io.IOException)

Example 5 with StreamCopyException

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

the class ByteArrayOutputStreamWithReadFromSupport method readFrom.

public long readFrom(InputStream inputStream, long length) throws StreamCopyException {
    readFromCalled = true;
    long read = 0;
    while (length < 0 || read < length) {
        int b;
        try {
            b = inputStream.read();
        } catch (IOException ex) {
            throw new StreamCopyException(StreamCopyException.READ, ex);
        }
        if (b == -1) {
            break;
        }
        try {
            write(b);
        } catch (IOException ex) {
            throw new StreamCopyException(StreamCopyException.WRITE, ex);
        }
    }
    return read;
}
Also used : StreamCopyException(org.apache.axiom.ext.io.StreamCopyException) IOException(java.io.IOException)

Aggregations

StreamCopyException (org.apache.axiom.ext.io.StreamCopyException)12 IOException (java.io.IOException)10 NullInputStream (org.apache.commons.io.input.NullInputStream)2 FileOutputStream (java.io.FileOutputStream)1 MemoryBlob (org.apache.axiom.blob.MemoryBlob)1 ReadFromSupport (org.apache.axiom.ext.io.ReadFromSupport)1 OMException (org.apache.axiom.om.OMException)1 ExceptionInputStream (org.apache.axiom.testutils.io.ExceptionInputStream)1 ExceptionOutputStream (org.apache.axiom.testutils.io.ExceptionOutputStream)1