⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 managedmemorydatasource.java

📁 Java有关XML编程需要用到axis 的源代码 把里面bin下的包导入相应的Java工程 进行使用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            Object[] array = readers.keySet().toArray();            for (int i = 0; i < array.length; i++) {                Instream stream = (Instream) array[i];                if (null != stream) {                    try {                        stream.close();                    } catch (Exception e) {                    }                }            }            readers.clear();            try {                diskCacheFile.delete();                ret = true;            } catch (Exception e) {                // Give it our best shot.                diskCacheFile.deleteOnExit();            }        }        return ret;    }    // inner classes cannot have static declarations...    /** Field is_log           */    protected static Log is_log =        LogFactory.getLog(Instream.class.getName());    /**     * Inner class to handle getting an input stream to this data source     *  Handles creating an input stream to the source.     */    private class Instream extends java.io.InputStream {        /** bytes read. */        protected long bread = 0;        /** The real stream. */        java.io.FileInputStream fin = null;        /** The position in the list were we are reading from. */        int currentIndex =                0;        /** the buffer we are currently reading from. */        byte[] currentBuf = null;        /** The current position in there. */        int currentBufPos = 0;        /** The read stream has been closed. */        boolean readClosed = false;        /**         * Constructor Instream.         *         * @throws java.io.IOException if the Instream could not be created or         *              if the data source has been deleted         */        protected Instream() throws java.io.IOException {            if (deleted) {                throw new java.io.IOException(                        Messages.getMessage("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(                        Messages.getMessage("resourceDeleted"));            }            if (readClosed) {                throw new java.io.IOException(                        Messages.getMessage("streamClosed"));            }            int ret = new Long(Math.min(Integer.MAX_VALUE, totalsz - bread)).intValue();            if (debugEnabled) {                is_log.debug("available() = " + ret + ".");            }            return ret;        }        /**         * 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 (ManagedMemoryDataSource.this) {                byte[] retb = new byte[1];                int br = read(retb, 0, 1);                if (br == -1) {                    return -1;                }                return 0xFF & retb[0];            }        }        /**         * Not supported.         *         * @return         */        public boolean markSupported() {            if (debugEnabled) {                is_log.debug("markSupported() = " + false + ".");            }            return false;        }        /**         * Not supported.         *         * @param readlimit         */        public void mark(int readlimit) {            if (debugEnabled) {                is_log.debug("mark()");            }        }        /**         * Not supported.         *         * @throws java.io.IOException         */        public void reset() throws java.io.IOException {            if (debugEnabled) {                is_log.debug("reset()");            }            throw new java.io.IOException(Messages.getMessage("noResetMark"));        }        public long skip(long skipped) throws java.io.IOException {            if (debugEnabled) {                is_log.debug("skip(" + skipped + ").");            }            if (deleted) {                throw new java.io.IOException(                        Messages.getMessage("resourceDeleted"));            }            if (readClosed) {                throw new java.io.IOException(                        Messages.getMessage("streamClosed"));            }            if (skipped < 1) {                return 0;    // nothing to skip.            }            synchronized (ManagedMemoryDataSource.this) {                skipped = Math.min(skipped,                        totalsz                        - bread);    // only skip what we've read.                if (skipped == 0) {                    return 0;                }                java.util.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 != fin) {                    fin.skip(skipped);                }                bread += skipped;            }            if (debugEnabled) {                is_log.debug("skipped " + skipped + ".");            }            return skipped;        }        public int read(byte[] b, int off, int len) throws java.io.IOException {            if (debugEnabled) {                is_log.debug(this.hashCode() + " read(" + off + ", " + len                        + ")");            }            if (deleted) {                throw new java.io.IOException(                        Messages.getMessage("resourceDeleted"));            }            if (readClosed) {                throw new java.io.IOException(                        Messages.getMessage("streamClosed"));            }            if (b == null) {                throw new InternalException(Messages.getMessage("nullInput"));            }            if (off < 0) {                throw new IndexOutOfBoundsException(                        Messages.getMessage("negOffset", "" + off));            }            if (len < 0) {                throw new IndexOutOfBoundsException(                        Messages.getMessage("length", "" + len));            }            if (len + off > b.length) {                throw new IndexOutOfBoundsException(                        Messages.getMessage("writeBeyond"));            }            if (len == 0) {                return 0;            }            int bwritten = 0;            synchronized (ManagedMemoryDataSource.this) {                if (bread == totalsz) {                    return -1;                }                java.util.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 (debugEnabled) {                    is_log.debug("len = " + len);                }                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 (debugEnabled) {                        is_log.debug(Messages.getMessage("reading", "" + len));                    }                    if (null == fin) {           // we are now reading from disk.                        if (debugEnabled) {                            is_log.debug(                                    Messages.getMessage(                                            "openBread",                                            diskCacheFile.getCanonicalPath()));                        }                        if (debugEnabled) {                            is_log.debug(Messages.getMessage("openBread",                                    "" + bread));                        }                        fin = new java.io.FileInputStream(diskCacheFile);                        if (bread > 0) {                            fin.skip(bread);     // Skip what we've read so far.                        }                    }                    if (cachediskstream != null) {                        if (debugEnabled) {                            is_log.debug(Messages.getMessage("flushing"));                        }                        cachediskstream.flush();                    }                    if (debugEnabled) {                        is_log.debug(Messages.getMessage("flushing"));                        is_log.debug("len=" + len);                        is_log.debug("off=" + off);                        is_log.debug("b.length=" + b.length);                    }                    bwritten = fin.read(b, off, len);                }                if (bwritten > 0) {                    bread += bwritten;                }            }            if (debugEnabled) {                is_log.debug(this.hashCode()                        + Messages.getMessage("read", "" + bwritten));            }            return bwritten;        }        /**         * close the stream.         *         * @throws java.io.IOException         */        public synchronized void close() throws java.io.IOException {            if (debugEnabled) {                is_log.debug("close()");            }            if (!readClosed) {                readers.remove(this);                readClosed = true;                if (fin != null) {                    fin.close();                }                fin = null;            }        }        protected void finalize() throws Throwable {            close();        }    }                                          // endof innerclass Instream    // Used to test.    /**     * Method main     *     * @param arg     */    public static void main(String arg[]) {    // test        try {            String readFile = arg[0];            String writeFile = arg[1];            java.io.FileInputStream ss =                    new java.io.FileInputStream(readFile);            ManagedMemoryDataSource ms =                    new ManagedMemoryDataSource(ss, 1024 * 1024, "foo/data", true);            javax.activation.DataHandler dh =                    new javax.activation.DataHandler(ms);            java.io.InputStream is = dh.getInputStream();            java.io.FileOutputStream fo =                    new java.io.FileOutputStream(writeFile);            byte[] buf = new byte[512];            int read = 0;            do {                read = is.read(buf);                if (read > 0) {                    fo.write(buf, 0, read);                }            } while (read > -1);            fo.close();            is.close();        } catch (java.lang.Exception e) {            log.error(Messages.getMessage("exception00"), e);        }    }    /**     * 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;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -