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();
}
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;
}
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;
}
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;
}
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;
}
Aggregations