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

📄 filewritercallback.java

📁 java开源的企业总线.xmlBlaster
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    * to clean up the destination file in case of an exception when writing.    */   private void putAllChunksTogether(String fileName, long expectedChunks, InputStream is, boolean isCompleteMsg) throws Exception {      File file = new File(this.directory, fileName);      if (file == null)         throw new Exception("the file for '" + fileName + "' was null");      if (file.exists()) {         if (file.isDirectory())            throw new Exception("can not write on '" + fileName + "' in directory '" + this.directory + "' since it is a directory");         if (!this.overwrite) {            log.warning("file '" + fileName + "' in directory '" + this.directory + "' exists already. Will keep the old one");            return;         }         else            log.warning("file '" + fileName + "' in directory '" + this.directory + "' exists already. Will overwrite it unless the chunks are not there anymore");      }      try {         File lock = null;         String lockName = null;         if (this.lockExtention != null) {            lockName = fileName + this.lockExtention;            lock = new File(this.directory, lockName);            lock.createNewFile();         }         log.info("storing file '" + fileName + "' on directory '" + this.directory + "'");         File[] files = null;         long numChunks = 0L;         if (!isCompleteMsg) {            files = getChunkFilenames(fileName, '.'); // retrieves the chunks in correct order            if (files.length > expectedChunks)               log.warning("Too many chunks belonging to '" + fileName + "' are found. They are '" + files.length + "' but should be '" + expectedChunks + "'");            else if (files.length < expectedChunks) {               if (file.exists()) {                  log.warning("The number of chunks is '" + files.length + "' which is less than the expected '" + expectedChunks + "' but the file '" + file.getAbsolutePath() + "' exists. So we will use the exisiting file (the chunks where probably already deleted)");                  return;               }               else                  throw new Exception("Too few chunks belonging to '" + fileName + "' are found. They are '" + files.length + "' but should be '" + expectedChunks + "'");            }            numChunks = files.length > expectedChunks ? expectedChunks : files.length;         }         // put all chunks together in one single file         int bufSize = BUF_SIZE;         byte[] buf = new byte[bufSize];         FileOutputStream fos = null;         try {            fos = new FileOutputStream(file);            for (int i=0; i < numChunks; i++) {               log.info("adding chunk '" + i + "' to file '" + fileName + "'");               FileInputStream fis = new FileInputStream(files[i]);               int ret = 0;               while ( (ret=fis.read(buf)) > -1) {                  fos.write(buf, 0, ret);               }               fis.close();            }            if (is != null) {               int ret = 0;               while ( (ret=is.read(buf)) > -1) {                  fos.write(buf, 0, ret);               }               is.close();            }            /*            if (lastContent != null && lastContent.length != 0)               fos.write(lastContent);            */            fos.close();         }         catch (Throwable ex) {            if (fos != null) {               if (file.exists()) {                  if (file.canWrite()) {                     if (!file.delete())                        log.warning("An exception occured when putting all chunks together for '" + fileName + "' but could not delete the file for an unknown reason. The original exception was '" + ex.getMessage() + "'");                  }                  else                     log.warning("An exception occured when putting all chunks together for '" + fileName + "' but could not delete the file since it is not writable. The original exception was '" + ex.getMessage() + "'");               }            }            if (ex instanceof Exception)               throw (Exception)ex;            else               throw new Exception(ex);         }         // clean up all chunks since complete file created         if (!isCompleteMsg && !this.keepDumpFiles) {            for (int i=0; i < files.length; i++)               deleteFile(files[i]);         }         if (lock != null) {            boolean deleted = lock.delete();            if (!deleted)               throw new Exception("can not delete lock file '" + lockName + "' in directory '" + this.directory + "'");         }      }      catch (IOException ex) {         throw new Exception("update: an exception occured when storing the file '" + fileName + "'", ex);      }   }   /**    * Deletes the specified file from the file system.    * Never throws Exceptions    * @param file the file object to be deleted.    * @return if it can not delete the file it returns false, true otherwise.    */   private final static boolean deleteFile(File file) {      if (file.exists()) {         if (file.canWrite()) {            try {               if (!file.delete())                  log.warning("The file '" + file.getName() + "' could not be deleted (unknown reason)");               else                  return true;            }            catch (Throwable ex) {               log.warning("An Exception occured when trying to delete file '" + file.getName() + "': " + ex.getMessage());            }         }         else            log.warning("The file '" + file.getName() + "' could not be deleted since it does not exist");      }      else         log.warning("The file '" + file.getName() + "' could not be deleted since it does not exist");      return false;   }   public void update(String topic, InputStream is, Map attrMap) throws Exception {      String filename = null;      boolean isLastMsg = true;      String exMsg = null;      long chunkCount = 0L;      if (attrMap != null) {         ClientProperty prop = (ClientProperty)attrMap.get(FILENAME_ATTR);         if (prop == null) {            prop = (ClientProperty)attrMap.get(FILENAME_ATTR_OLD_FASHION);         }         if (prop != null)            filename = prop.getStringValue();         if (filename == null || filename.length() < 1) {            prop = (ClientProperty)attrMap.get(TIMESTAMP_ATTR);            if (prop != null) {               String timestamp = prop.getStringValue();               filename = "xbl" + timestamp + ".msg";            }            else               throw new Exception("update: the message '" + topic + "' should contain either the filename or the timestamp in the properties, but none was found. Can not create a filename to store the data on.");         }         prop = XBMessage.get(XBConnectionMetaData.JMSX_GROUP_SEQ, attrMap);         if (prop != null) {            isLastMsg = false;            chunkCount = prop.getLongValue();            prop = XBMessage.get(XBConnectionMetaData.JMSX_GROUP_EOF, attrMap);            if (prop != null) {               isLastMsg = prop.getBooleanValue();               prop = XBMessage.get(XBConnectionMetaData.JMSX_GROUP_EX, attrMap);               if (prop != null)                  exMsg = prop.getStringValue();            }         }         else            isLastMsg = true;      }      if (filename == null) {         // fileName = topic + (new Timestamp()).getTimestamp() + ".msg";         filename = topic;         log.warning("The message did not contain any filename nor timestamp. Will write to '" + filename + "'");      }      log.fine("storing file '" + filename + "' on directory '" + this.directory.getName() + "'");      boolean isCompleteMsg = isLastMsg && chunkCount == 0L;      if (exMsg == null) { // no exception         if (isLastMsg)            putAllChunksTogether(filename, chunkCount, is, isCompleteMsg);         else            storeChunk(this.tmpDirectory, filename, chunkCount, '.', this.overwrite, is);      }      else if (!isCompleteMsg) { // clean up old chunks         File[] files = getChunkFilenames(filename, '.'); // retrieves the chunks in correct order         for (int i=0; i < files.length; i++)            deleteFile(files[i]);      }   }}

⌨️ 快捷键说明

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