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

📄 cm.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                } catch (IOException ignored) {
                }
            }
        }
        return buffer;
    }


    /**
     * saves a StructuredDocument in specified dir, and file name
     *
     * @param  dn               directory name
     * @param  fn               file name
     * @param  doc              StructuredDocument to save
     * @exception  IOException  if an I/O error occurs
     * @since 1.0
     */
    public void save(String dn, String fn, StructuredDocument doc)
    throws IOException {
        save(dn,
        fn,
        doc,
        DiscoveryService.DEFAULT_LIFETIME,
        DiscoveryService.DEFAULT_EXPIRATION);
    }


    /**
     * saves a StructuredDocument in specified dir, and file name, and associated doc timeouts
     *
     * @param  dn               directory name
     * @param  fn               file name
     * @param  doc              StructuredDocument to save
     * @param  timeoutInMyCache  Local time out, after which the documents expires
     * @param  timeoutForOthers  remote time out distributed in responses,
     * after which the documents expires
     * @exception  IOException  if an I/O error occurs
     * @since 1.0
     */
    public void save(String dn,
    String fn,
    StructuredDocument doc,
    long timeoutInMyCache,
    long timeoutForOthers)
    throws IOException {

        File file = getFile(dn, fn);

        // remove the old version from the index if we can
        //PDA requirements 21.02.2002
        //package net.jxta.impl.index.* is not ported to PDA
        //search method "INDEX" is not used on a PDA for th etime being
        /*if (searchMethod == INDEX && file.exists()) {
            FileInputStream in = null;

            try {
                in = new FileInputStream(file);
                ((IndexService.Index) indexes.get(dn)).remove(fn, in);
            } catch (IndexService.BadDocumentException e) {
                // we could index it before
                if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Index : Bad Document", e );
            } catch (IndexService.IndexException e) {
                // we could inex before
                if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Index : Index Exception", e );
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {

                    }
                }
            }
        }*/
        //PDA requirements 21.02.2002
        FileOutputStream op = null;

        try {
            // save the new version
            op = new FileOutputStream(file);
            doc.sendToStream(op);
            op.close();
            op = null;

            //PDA requirements 21.02.2002
            //package net.jxta.impl.index.* is not ported to PDA
            //search method "INDEX" is not used on a PDA for the time being
            /*if (searchMethod == INDEX) {
                try {
                    if (LOG.isDebugEnabled() ) {
                        if (LOG.isEnabledFor(Priority.DEBUG))
                            LOG.debug("indexing "+file + ": " + dn + " " + fn);
                    }
                  ((IndexService.Index) indexes.get(dn)).add(fn, doc.getStream());
                } catch (IOException e) {
                    if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Index : IO Exception", e );
                    //XXX  revisit for alternative action
                    throw e;
                } catch (IndexService.BadDocumentException e) {
                    if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Save failed", e);
                    // do nothing just a bad doc
                } catch (IndexService.IndexException e) {
                    if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Save failed", e);
                    //XXX  revisit for alternative action
                    // disable index use, and move on
                    // indexes.remove(dn);
                }
            } else*/ if (searchMethod == CACHE) {
            //PDA requirements 21.02.2002
              ((CmCache) caches.get(dn)).add(fn, doc);
            }


            // Initiate or renew the expiration schedule
            long now = System.currentTimeMillis();
            expirer.scheduleFileExpiration(file,
            now + timeoutInMyCache,
            timeoutForOthers);

        } catch (IOException e) {
            if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("Cm cannot write doc", e);
            throw e;
        } finally {
            if (op != null) {
                try {
                    op.close();
                } catch (IOException e) {
                }
            }
        }
    }

    /**
     * saves a StructuredDocument in specified dir, and file name from byte array
     *
     * @param  dn               directory name
     * @param  fn               file name
     * @param  buffer           byte array to write to file
     * @exception  IOException  if an I/O error occurs
     * @since 1.0
     */
    public void saveBytes(String dn, String fn, byte[] buffer) throws IOException {
        saveBytes(dn, fn, buffer, -1, -1);
    }


    /**
     * saves a StructuredDocument in specified dir, and file name from byte array
     *
     * @param  dn               directory name
     * @param  fn               file name
     * @param  buffer           byte array to write to file
     * @param  timeoutInMyCache  Local time out, after which the documents expires
     * @param  timeoutForOthers  remote time out distributed in responses, after
     * which the documents expires
     * @exception  IOException  if an I/O error occurs
     * @since 1.0
     */
    public void saveBytes(String dn,
    String fn,
    byte[] buffer,
    long timeoutInMyCache,
    long timeoutForOthers) throws IOException {

        File file = getFile(dn, fn);
        FileOutputStream op = null;

        try {
            op = new FileOutputStream(file);
            op.write(buffer);

            //PDA requirements 21.02.2002
            //package net.jxta.impl.index.* is not ported to PDA
            //search method "INDEX" is not used on a PDA for the time being
            /*if (searchMethod == INDEX ) {

                ByteArrayInputStream bis = new ByteArrayInputStream(buffer);
                try {
                    ((IndexService.Index) indexes.get(dn)).add(fn, bis);
                } catch (IOException e) {
                    //XXX  revisit for alternative action
                    if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Index : IO Exception", e );
                } catch (IndexService.BadDocumentException bde) {
                    // do nothing just a bad doc
                    if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Index : Bad Document", bde );
                } catch (IndexService.IndexException ie) {
                    //XXX  revisit for alternative action
                    if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Index : Index Exception", ie );
                    // disable index use, and move on
                    // indexes.remove(dn);
                }
            }*/
            //PDA requirements 21.02.2002
        } catch (IOException e) {
            if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Cm cannot write doc");
        } finally {
            if (op != null) {
                try {
                    op.close();
                } catch (IOException e) {
                }
            }
        }
    }


    /**
     * saves a StructuredDocument in specified dir, and file name from a input stream
     *
     * @param  dn               directory name
     * @param  fn               file name
     * @param  source           Description of Parameter
     * @exception  IOException  if an I/O error occurs
     * @since 1.0
     */
    public void saveBytes(String dn, String fn, InputStream source) throws IOException {
        saveBytes(dn, fn, source, -1, -1);
    }


    /**
     * Saves in specified dir, and file name from a input stream
     *
     * @param  dn               directory name
     * @param  fn               file name
     * @param  source           Description of Parameter
     * @param  timeoutInMyCache  Local time out, after which the documents expires
     * @param  timeoutForOthers  remote time out distributed in responses, after
     * which the documents expires
     * @exception  IOException  if an I/O error occurs
     * @since 1.0
     */
    public void saveBytes(String dn,
    String fn,
    InputStream source,
    long timeoutInMyCache,
    long timeoutForOthers) throws IOException {

        File file = getFile(dn, fn);
        FileOutputStream op = null;
        byte[] buffer;

        try {
            op = new FileOutputStream(file);
            buffer = new byte[4096];

            do {
                int res = source.read(buffer);

                if (res == 0) {
                    continue;
                } else if (res == -1) {
                    break;
                }
                op.write(buffer, 0, res);
            } while (true);

            op.flush();
            op.close();
            op = null;
            source.close();

            // reopen the file we just saved for the indexer.
            source = new FileInputStream( file );

            //PDA requirements 21.02.2002
            //package net.jxta.impl.index.* is not ported to PDA
            //search method "INDEX" is not used on a PDA for the time being
            /*if (searchMethod == INDEX) {
                try {
                    ((IndexService.Index) indexes.get(dn)).add(fn, source);
                } catch (IOException e) {
                    //XXX  revisit for alternative action
                    if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Index : IO Exception", e );
                } catch (IndexService.BadDocumentException bde) {
                    // do nothing just a bad doc
                    if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Index : Bad Document", bde );
                } catch (IndexService.IndexException ie) {
                    //XXX  revisit for alternative action
                    // disable index use, and move on
                    // indexes.remove(dn);
                    if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Index : Index Error", ie );
                }
            } else*/ if (searchMethod == CACHE) {
            //PDA requirements 21.02.2002

               ((CmCache) caches.get(dn)).add(fn);
            }

            expirer.scheduleFileExpiration(file,
            System.currentTimeMillis() +
            timeoutInMyCache,
            timeoutForOthers);

        } catch (IOException e) {
            if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Cm cannot write doc", e);
        } finally {
            if (source != null) {
                try {
                    source.close();
                    source = null;
                } catch (IOException e) {
                }
            }

            if (op != null) {
                try {
                    op.close();
                    op = null;
                } catch (IOException e) {
                }
            }
        }
    }


    /**
     * Returns true if the directory exists
     *
     * @since 1.0
     *
     * @param  dn               direcory name
     * @exception  IOException  if an I/O error occurs
     */
    public void cleanupFolder(String dn) throws IOException {

        if (dn == null) {
            return;
        }

        if (LOG.isDebugEnabled()) {
            if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug(" cleaning up " + dn);
        }

        File dir = new File(rootDir, dn);
        String[] files = getFileNames(dn);

        for (int i = 0; files != null && i < files.length; ++i) {
            File file = new File(dir, files[i]);
            if (!file.delete()) {
                if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("  failed removing " + files[i]);
            }
        }
    }


⌨️ 快捷键说明

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