saajdatasource.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 640 行 · 第 1/2 页
JAVA
640 行
if (closed) {
throw new java.io.IOException("streamClosed");
}
int byteswritten = 0;
if (memorybuflist != null) { // Can write to memory.
do {
if (null == currentMemoryBuf) {
currentMemoryBuf = new byte[READ_CHUNK_SZ];
currentMemoryBufSz = 0;
memorybuflist.add(currentMemoryBuf);
}
// bytes to write is the min. between the remaining bytes and what is left in this buffer.
int bytes2write = Math.min((length - byteswritten),
(currentMemoryBuf.length
- currentMemoryBufSz));
// copy the data.
System.arraycopy(data, byteswritten, currentMemoryBuf,
currentMemoryBufSz, bytes2write);
byteswritten += bytes2write;
currentMemoryBufSz += bytes2write;
if (byteswritten
< length) { // only get more if we really need it.
currentMemoryBuf = new byte[READ_CHUNK_SZ];
currentMemoryBufSz = 0;
memorybuflist.add(currentMemoryBuf); // add it to the chain.
}
} while (byteswritten < length);
}
totalsz += length;
}
/**
* get the filename of the content if it is cached to disk.
*
* @return file object pointing to file, or null for memory-stored content
*/
public File getDiskCacheFile() {
return diskCacheFile;
}
public InputStream getInputStream() throws IOException {
return new SAAJInputStream(); // Return the memory held stream.
}
public OutputStream getOutputStream() throws IOException {
//TODO: Method implementation
return null;
}
public String getContentType() {
return contentType;
}
public String getName() {
String ret = null;
try {
flushToDisk();
if (diskCacheFile != null) {
ret = diskCacheFile.getAbsolutePath();
}
} catch (Exception e) {
diskCacheFile = null;
}
return ret;
}
/**
* Inner class to handle getting an input stream to this data source Handles creating an input
* stream to the source.
*/
private class SAAJInputStream extends java.io.InputStream {
/** bytes read. */
protected long bread = 0;
/** The real stream. */
private FileInputStream fileIS;
/** The position in the list were we are reading from. */
int currentIndex;
/** the buffer we are currently reading from. */
byte[] currentBuf;
/** The current position in there. */
int currentBufPos;
/** The read stream has been closed. */
boolean readClosed;
/**
* Constructor Instream.
*
* @throws java.io.IOException if the Instream could not be created or if the data source
* has been deleted
*/
protected SAAJInputStream() throws java.io.IOException {
if (deleted) {
throw new java.io.IOException("resourceDeleted");
}
readers.put(this, null);
}
/**
* Query for the number of bytes available for reading.
*
* @return the number of bytes left
* @throws java.io.IOException if this stream is not in a state that supports reading
*/
public int available() throws java.io.IOException {
if (deleted) {
throw new java.io.IOException("resourceDeleted");
}
if (readClosed) {
throw new java.io.IOException("streamClosed");
}
return new Long(Math.min(Integer.MAX_VALUE, totalsz - bread)).intValue();
}
/**
* Read a byte from the stream.
*
* @return byte read or -1 if no more data.
* @throws java.io.IOException
*/
public int read() throws java.io.IOException {
synchronized (SAAJDataSource.this) {
byte[] retb = new byte[1];
int br = read(retb, 0, 1);
if (br == -1) {
return -1;
}
return 0xFF & retb[0];
}
}
/** Not supported. */
public boolean markSupported() {
return false;
}
/**
* Not supported.
*
* @param readlimit
*/
public void mark(int readlimit) {
}
/**
* Not supported.
*
* @throws java.io.IOException
*/
public void reset() throws IOException {
throw new IOException("noResetMark");
}
public long skip(long skipped) throws IOException {
if (deleted) {
throw new IOException("resourceDeleted");
}
if (readClosed) {
throw new IOException("streamClosed");
}
if (skipped < 1) {
return 0; // nothing to skip.
}
synchronized (SAAJDataSource.this) {
skipped = Math.min(skipped, totalsz - bread); // only skip what we've read.
if (skipped == 0) {
return 0;
}
List ml = memorybuflist; // hold the memory list.
int bwritten = 0;
if (ml != null) {
if (null == currentBuf) { // get the buffer we need to read from.
currentBuf = (byte[])ml.get(currentIndex);
currentBufPos = 0; // start reading from the begining.
}
do {
long bcopy = Math.min(currentBuf.length - currentBufPos,
skipped - bwritten);
bwritten += bcopy;
currentBufPos += bcopy;
if (bwritten < skipped) {
currentBuf = (byte[])ml.get(++currentIndex);
currentBufPos = 0;
}
} while (bwritten < skipped);
}
if (null != fileIS) {
fileIS.skip(skipped);
}
bread += skipped;
}
return skipped;
}
public int read(byte[] b, int off, int len) throws IOException {
if (deleted) {
throw new IOException("resourceDeleted");
}
if (readClosed) {
throw new IOException("streamClosed");
}
if (b == null) {
throw new RuntimeException("nullInput");
}
if (off < 0) {
throw new IndexOutOfBoundsException("negOffset " + off);
}
if (len < 0) {
throw new IndexOutOfBoundsException("length " + len);
}
if (len + off > b.length) {
throw new IndexOutOfBoundsException("writeBeyond");
}
if (len == 0) {
return 0;
}
int bwritten = 0;
synchronized (SAAJDataSource.this) {
if (bread == totalsz) {
return -1;
}
List ml = memorybuflist;
long longlen = len;
longlen = Math.min(longlen, totalsz -
bread); // Only return the number of bytes in the data store that is left.
len = new Long(longlen).intValue();
if (ml != null) {
if (null == currentBuf) { // Get the buffer we need to read from.
currentBuf = (byte[])ml.get(currentIndex);
currentBufPos = 0; // New buffer start from the begining.
}
do {
// The bytes to copy, the minimum of the bytes left in this buffer or bytes remaining.
int bcopy = Math.min(currentBuf.length - currentBufPos, len - bwritten);
// Copy the data.
System.arraycopy(currentBuf, currentBufPos, b, off + bwritten, bcopy);
bwritten += bcopy;
currentBufPos += bcopy;
if (bwritten < len) { // Get the next buffer.
currentBuf = (byte[])ml.get(++currentIndex);
currentBufPos = 0;
}
} while (bwritten < len);
}
if ((bwritten == 0) && (null != diskCacheFile)) {
if (null == fileIS) { // we are now reading from disk.
fileIS = new java.io.FileInputStream(diskCacheFile);
if (bread > 0) {
fileIS.skip(bread); // Skip what we've read so far.
}
}
if (cachediskstream != null) {
cachediskstream.flush();
}
bwritten = fileIS.read(b, off, len);
}
if (bwritten > 0) {
bread += bwritten;
}
}
return bwritten;
}
/**
* close the stream.
*
* @throws IOException
*/
public synchronized void close() throws IOException {
if (!readClosed) {
readers.remove(this);
readClosed = true;
if (fileIS != null) {
fileIS.close();
}
fileIS = null;
}
}
protected void finalize() throws Throwable {
super.finalize();
close();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?