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

📄 cmsrfsfileviewer.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

        /**
         * @see InputStreamReader#InputStreamReader(java.io.InputStream)
         */
        public InputStreamCounter(InputStream in) {

            super(in);
            m_delegate = new InputStreamReader(in);
        }

        /**
         * @see InputStreamReader#InputStreamReader(java.io.InputStream, java.nio.charset.Charset)
         */
        public InputStreamCounter(InputStream in, Charset cs) {

            super(in, cs);
        }

        /**
         * @see InputStreamReader#InputStreamReader(java.io.InputStream, java.nio.charset.CharsetDecoder)
         */
        public InputStreamCounter(InputStream in, CharsetDecoder dec) {

            super(in, dec);
            m_delegate = new InputStreamReader(in, dec);
        }

        /**
         * @see InputStreamReader#InputStreamReader(java.io.InputStream, String)
         */
        public InputStreamCounter(InputStream in, String charsetName)
        throws UnsupportedEncodingException {

            super(in, charsetName);
            m_delegate = new InputStreamReader(in, charsetName);
        }

        /**
         * @see InputStreamReader#close()
         */
        public void close() throws IOException {

            m_delegate.close();
            m_position = 0;
        }

        /**
         * @see InputStreamReader#getEncoding()
         */
        public String getEncoding() {

            return m_delegate.getEncoding();
        }

        /**
         * Returns the position of the last character read from the underlying stream.<p>
         * 
         * @return the position of the last character read from the underlying stream
         */
        public long position() {

            return m_position;
        }

        /**
         * Read a character from the proxied <code>InputStreamReader</code> 
         * and increase the internal position.<p>
         * 
         * @see InputStreamReader#read()
         */
        public int read() throws IOException {

            int result = m_delegate.read();
            if (result != -1) {
                m_position++;
            }
            return result;
        }

        /**
         * Read the <code>char[]</code> from the proxied <code>InputStreamReader</code> 
         * and increase the internal position.<p>
         * 
         * @see InputStreamReader#read(char[])
         */

        public int read(char[] cbuf) throws IOException {

            int result = m_delegate.read(cbuf);
            if (result != -1) {
                m_position += result;
            }
            return result;
        }

        /**
         * Read the <code>char[]</code> from the proxied <code>InputStreamReader</code> 
         * and increase the internal position.<p>
         * 
         * @see InputStreamReader#read(char[], int, int)
         */
        public int read(char[] cbuf, int off, int len) throws IOException {

            int result = m_delegate.read(cbuf, off, len);
            if (result != -1) {
                m_position += result;
            }
            return result;
        }

        /**
         * @see InputStreamReader#ready()
         */
        public boolean ready() throws IOException {

            return m_delegate.ready();
        }

        /**
         * Reset the proxied <code>InputStreamReader</code> 
         * and the internal position.<p>
         * 
         * @see InputStreamReader#reset()
         */
        public void reset() throws IOException {

            m_delegate.reset();
            m_position = 0;
        }

        /**
         * Skip on the proxied <code>InputStreamReader</code> 
         * and increase the internal position.<p>
         * 
         * @see InputStreamReader#skip(long)
         */
        public long skip(long n) throws IOException {

            long result = m_delegate.skip(n);
            m_position -= result;
            return result;
        }

    }

    /** The log object for this class. */
    protected static final Log LOG = CmsLog.getLog(CmsRfsFileViewer.class);

    /** Maps file paths to internal info instances. */
    protected Map m_fileName2lineIndex;

    /** The path to the underlying file. */
    protected String m_filePath;

    /** The current window (numbered from zero to amount of possible different windows).  */
    protected int m_windowPos;

    /** The amount of lines to show. */
    protected int m_windowSize;

    /** Decides whethter the view onto the underlying file via readFilePortion is enabled. */
    private boolean m_enabled;

    /** The character encoding of the underlying file. */
    private Charset m_fileEncoding;

    /** 
     * If value is <code>true</code>, all setter methods will throw a 
     * <code>{@link CmsRuntimeException}</code><p>. 
     * 
     * Only the method <code>{@link #clone()}</code> returns a clone that has set this 
     * member to <code>false</code> allowing modification to take place.<p>
     */
    private boolean m_frozen;

    /** 
     * If true the represented file is a standard OpenCms log file and may be displayed 
     * in more convenient ways (in future versions) because the format is known. 
     */
    private boolean m_isLogfile;

    /**
     * Creates an instance with default settings that tries to use the log file path obtained 
     * from <code>{@link OpenCms}'s {@link org.opencms.main.CmsSystemInfo}</code> instance.<p>
     * 
     * If the log file path is invalid or not configured correctly a logging is performed and the 
     * path remains empty to allow user-specified file selection.<p>
     */
    public CmsRfsFileViewer() {

        m_isLogfile = true;
        m_fileName2lineIndex = new HashMap();
        // system default charset: see http://java.sun.com/j2se/corejava/intl/reference/faqs/index.html#default-encoding
        m_fileEncoding = Charset.forName(new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding());
        m_enabled = true;
        m_windowSize = 200;
    }

    /**
     * Returns a clone of this file view settings that is not "frozen" and therefore allows modifications.<p>
     * 
     * Every instance that plans to modify settings has to obtain a clone first that may be 
     * modified. The original instance returned from  
     * (<code>{@link org.opencms.workplace.CmsWorkplaceManager#getFileViewSettings()}</code>) will throw 
     * a <code>{@link CmsRuntimeException}</code> for each setter invocation. <p>
     * 
     * @return a clone of this file view settings that is not "frozen" and therefore allows modifications
     */
    public Object clone() {

        // first run after installation: filePath is null:
        if (m_filePath == null) {
            if (OpenCms.getRunLevel() >= OpenCms.RUNLEVEL_3_SHELL_ACCESS) {
                this.m_filePath = OpenCms.getSystemInfo().getLogFileRfsPath();
            }
        }
        CmsRfsFileViewer clone = new CmsRfsFileViewer();
        try {
            // strings are immutable: no outside modification possible.
            clone.setFilePath(m_filePath);
        } catch (CmsRfsException e) {
            // will never happen because m_filePath was verified in setFilePath of this instance.
        } catch (CmsRuntimeException e) {
            // will never happen because m_filePath was verified in setFilePath of this instance.
        }
        clone.m_fileEncoding = m_fileEncoding;
        clone.m_isLogfile = m_isLogfile;
        clone.m_enabled = m_enabled;
        //clone.m_windowPos = m_windowPos;
        clone.setWindowSize(m_windowSize);
        // this is a self-managed cache of line indices that won't be persisted
        // drop old index entries before copy
        expireIndices();
        clone.m_fileName2lineIndex = m_fileName2lineIndex;
        // allow clone-modifications. 
        clone.m_frozen = false;
        return clone;
    }

    /**
     * Returns the canonical name of the character encoding of the underlying file.<p>
     * 
     * If no special choice is fed into 
     * <code>{@link #setFileEncoding(String)}</code> before this call 
     * always the system default character encoding is returned.<p>
     * 
     * This value may be ignored outside and will be ignored inside if the 
     * underlying does not contain textual content.<p>
     * 
     * @return the canonical name of the character encoding of the underlying file
     */
    public String getFileEncoding() {

        return m_fileEncoding.name();
    }

    /**
     * Returns the path denoting the file that is accessed.<p>
     * 
     * @return the path denoting the file that is accessed
     */
    public String getFilePath() {

        return m_filePath;
    }

    /**
     * Returns true if the view's internal file path points to a log file in standard OpenCms format.<p> 
     * 
     * @return true if the view's internal file path points to a log file in standard OpenCms format
     */
    public boolean getIsLogfile() {

        // method name is bean-convention of apache.commons.beanutils (unlike eclipse's convention for booleans)
        return m_isLogfile;
    }

    /**
     * Returns the start position of the current display.<p>
     * 
     * This is a count of "windows" that 
     * consist of viewable text with "windowSize" lines of text (for a non-standard log file) or 
     * log-entries (for a standard log file).<p>
     * 
     * @return the start position of the current display
     */
    public int getWindowPos() {

        return m_windowPos;
    }

    /**
     * Get the amount of lines (or entries depending on wether a standard log file is shown) 
     * to display per page. <p>
     * 
     * @return the amount of lines to display per page
     */
    public int getWindowSize() {

        return m_windowSize;
    }

    /**
     * Returns true if this view upon the underlying file via 
     * <code>{@link #readFilePortion()}</code> is enabled.<p>
     * 
     * 
     * @return true if this view upon the underlying file via 
     * <code>{@link #readFilePortion()}</code> is enabled.<p>
     */
    public boolean isEnabled() {

        return m_enabled;
    }

    /**
     * Return the view portion of lines of text from the underlying file or an 
     * empty String if <code>{@link #isEnabled()}</code> returns <code>false</code>.<p>
     * 
     * @return the view portion of lines of text from the underlying file or an 
     *         empty String if <code>{@link #isEnabled()}</code> returns <code>false</code>
     * @throws CmsRfsException if something goes wrong
     */
    public String readFilePortion() throws CmsRfsException {

        CmsRfsFileLineIndexInfo lineInfo = initIndexer(m_filePath);
        if (m_enabled) {
            Reader reader = null;
            try {
                reader = new InputStreamReader(new FileInputStream(m_filePath), m_fileEncoding);
                long skip = lineInfo.getLineBreakPosition(m_windowPos * m_windowSize, m_windowSize);
                // this might look complicated but avoids infinite loops (expect the worst from implementation relying on)
                int maxSkipTries = 100;
                while (skip > 0 && maxSkipTries > 0) {
                    long skipped = reader.skip(skip);

⌨️ 快捷键说明

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