public void save(boolean clock, int crc, byte[] buf, int offset, int length) throws IOException {
   checkClosed();
   if (size <= saved) {
     return;
   }
   length = (int) Math.min(length, size - saved);
   if (null == file) {
     file = new RandomAccessFile(partialFile, "rw");
   }
   file.seek(saved);
   file.write(buf, offset, length);
   scratch[0] = (byte) (clock ? 1 : 0);
   scratch[1] = (byte) ((crc >> 24) & 0xff);
   scratch[2] = (byte) ((crc >> 16) & 0xff);
   scratch[3] = (byte) ((crc >> 8) & 0xff);
   scratch[4] = (byte) (crc & 0xff);
   file.write(scratch, 0, 5);
   file.setLength(file.getFilePointer());
   saved += length;
   lastClock = clock;
   lastCrc = crc;
   if (size <= saved) {
     file.setLength(size);
     file.close();
     rename(partialFile, resultFile);
   }
 }
示例#2
0
  /**
   * 使用切割的方式来替换给定文件中的一段数据
   *
   * @param file 给定的文件
   * @param off 要替换的一段数据的开始位置(包括)
   * @param length 要替换的一段数据的长度,大于1
   * @param newData 用来替换旧数据的新数据
   * @throws IOException
   * @throws LengthTooBigException (fileLength - (off + length)) > 31457280
   *     因为本方法采用的是先把需要替换的数据之后的数据读到内存中,然后将文件截短,最后把之前保存的数据写到文件中。因此读到内存中的数据不能太大
   */
  public static void replaceFileDataByCutWay(File file, long off, long length, byte[] newData)
      throws IOException, LengthTooBigException {

    // 获取文件长度
    long fileLength = file.length();

    // 验证数据合法性
    CheckingUtils.valiLongValue(off, 0, fileLength - 1, "off");
    CheckingUtils.valiLongValue(off + length, off + 1, fileLength, "length");
    CheckingUtils.valiObjectIsNull(newData, "newData");

    if (newData.length > 0) {
      // 计算需读到内存的数据的长度
      long keepDataLength = fileLength - (off + length);

      // 如果需要读到内存的数据长度为0
      if (keepDataLength == 0) {
        // 打开原文件
        RandomAccessFile raf = new RandomAccessFile(file, "rw");

        // 设置长度
        raf.setLength(off);

        // 将新数据写到末尾去
        raf.write(newData);

        // 关闭原文件
        raf.close();
      } else if (keepDataLength <= 31457280) {
        // 打开原文件
        RandomAccessFile raf = new RandomAccessFile(file, "rw");

        // 读取要保存的数据
        byte[] keepData = new byte[(int) keepDataLength];
        raf.seek(off + length);
        raf.read(keepData);

        // 将文件截掉合适的长度
        if (length != 0) {
          raf.setLength(fileLength - length);
        }

        // 写入新数据
        raf.seek(off);
        raf.write(newData);

        // 写入保存的数据
        raf.write(keepData);

        // 关闭原文件
        raf.close();
      } else {
        throw new LengthTooBigException(
            "Need to read the length of data of the memory more than 30720 ((fileLength - (off + length)) > 30720)");
      }
    }
  }
示例#3
0
  protected static void persist(String content, String filePath, boolean append)
      throws IOException {
    // Remove string like <?xml version="1.0" encoding="UTF-8"?>
    // Pattern p = Pattern.compile("(<Comment\\W.*?</Comment>)");
    // Matcher m = p.matcher(content);
    File f = new File(filePath);
    File parent = f.getParentFile();
    if (!parent.exists()) parent.mkdirs();
    if (!f.exists()) f.createNewFile();

    RandomAccessFile raf = null;
    try {
      raf = new RandomAccessFile(f, "rw");
      if (!append) raf.setLength(0);
      byte[] buffer = new byte[1024];
      raf.read(buffer);
      String leadingString = new String(buffer).trim();
      Pattern p = Pattern.compile("(<\\?xml\\W.*?\\?>)");
      Matcher m = p.matcher(leadingString);
      String xmlMeta = ""; // Store the xml version meta data, used to insert to the xml file
      // if needed.
      if (m.find()) {
        xmlMeta = m.group();
      }
      leadingString = m.replaceAll("");
      leadingString =
          leadingString
              .substring(0, Math.min(leadingString.length(), "<CommentsDocument />".length()))
              .replaceAll("\\s", "");

      StringBuilder sb = new StringBuilder();
      content = content.replaceAll(p.pattern(), "");
      if (leadingString.startsWith("<CommentsDocument/>") || "".equals(leadingString)) {
        // There is no comments yet.
        raf.setLength(0);
        sb.append(xmlMeta);
        sb.append("<CommentsDocument>");
        sb.append(content);
        sb.append("</CommentsDocument>");
        raf.write(sb.toString().getBytes());
      } else if (leadingString.startsWith("<CommentsDocument>")) {
        // There are some comments and append new comments.
        long fileLen = raf.length() - "</CommentsDocument>".getBytes().length;
        raf.setLength(fileLen);
        raf.seek(raf.length());
        sb.append(content);
        sb.append("</CommentsDocument>");
        raf.write(sb.toString().getBytes());
      }
    } finally {
      if (raf != null) raf.close();
    }
  }
示例#4
0
  @Override
  public void execute(Controller controller) {
    FSImage fsi = controller.fsImages.get(fsiUuid);
    cm = controller.getCommandManager();
    FileTransferManager ftm = controller.getFileTransferManager();

    if (!fsi.isLocal()) {
      String message = "No such FSImage on the target side";
      failed(new IllegalStateException(), message);
      return;
    }

    Path pathToRoot = fsi.getPathToRoot();
    Path fullPath = pathToRoot.resolve(path.toPath());

    // save memo about this operation on the target side
    ftm.addTarget(operationUuid, fullPath);
    try (RandomAccessFile f = new RandomAccessFile(fullPath.toFile(), "rw")) {
      f.setLength(size);
      // will send next command to the source
      cm.sendCommand(new QueryDownloadFile(operationUuid));
    } catch (IOException e) {
      String message = String.format("Allocation memory failed: \r\n %s", e.toString());
      failed(e, message);
    }
  }
  public void testCreateZipWithLargeFiles() throws IOException {
    MockApplicationInfo info = new MockApplicationInfo();
    File file = new File("/tmp/large.txt");
    RandomAccessFile randomaccessfile = new RandomAccessFile(file, "rw");
    randomaccessfile.setLength(THIRTY_MB);
    randomaccessfile.close();

    info.getApplicationFileBundles()
        .add(
            new DefaultApplicationFileBundle(
                BundleManifest.APPLICATION_LOGS,
                "large-file",
                "a really large file to test zip truncation",
                file.getAbsolutePath()));
    // write a lot of data to a file in the info bundle

    ValidationLog limitedValidationLog = new ValidationLog(info);
    ZipUtility.createSupportZip(info.getApplicationFileBundles(), info, limitedValidationLog, true);
    assertTrue(
        "No warning was issued when truncating an overly large file...",
        limitedValidationLog.hasWarnings());

    ValidationLog unlimitedValidationLog = new ValidationLog(info);
    ZipUtility.createSupportZip(
        info.getApplicationFileBundles(), info, unlimitedValidationLog, false);
    assertFalse(
        "A warning was issued regarding a large file even when the option to limit file sizes was disabled.",
        unlimitedValidationLog.hasWarnings());

    file.delete();
  }
	@Test
	public void testRegisterChannelDuplexWithNonSelectableChannel()
		throws Exception {

		// Normal register, with unselectable channel

		File tempFile = new File("tempFile");

		tempFile.deleteOnExit();

		RandomAccessFile randomAccessFile = new RandomAccessFile(
			tempFile, "rw");

		randomAccessFile.setLength(Integer.MAX_VALUE);

		try (FileChannel fileChannel = randomAccessFile.getChannel()) {
			FutureRegistrationReference futureRegistrationReference =
				(FutureRegistrationReference)_executorIntraband.registerChannel(
					fileChannel);

			Assert.assertSame(
				_executorIntraband, futureRegistrationReference.getIntraband());
			Assert.assertTrue(futureRegistrationReference.isValid());

			futureRegistrationReference.cancelRegistration();

			Assert.assertFalse(futureRegistrationReference.isValid());
		}
		finally {
			tempFile.delete();
		}
	}
示例#7
0
 /*
  * Writes WAV header into new file.
  */
 private void writeWavHeader() {
   try {
     randomAccessWriter = new RandomAccessFile(filePath, "rw");
     randomAccessWriter.setLength(0);
     randomAccessWriter.writeBytes("RIFF");
     randomAccessWriter.writeInt(0); // Final file size not known yet, write 0
     randomAccessWriter.writeBytes("WAVE");
     randomAccessWriter.writeBytes("fmt ");
     randomAccessWriter.writeInt(Integer.reverseBytes(16)); // Sub-chunk size, 16 for PCM
     randomAccessWriter.writeShort(Short.reverseBytes((short) 1)); // AudioFormat, 1 for PCM
     randomAccessWriter.writeShort(
         Short.reverseBytes((short) 1)); // Number of channels, 1 for mono, 2 for stereo
     randomAccessWriter.writeInt(Integer.reverseBytes(RECORDER_SAMPLE_RATE)); // Sample rate
     randomAccessWriter.writeInt(
         Integer.reverseBytes(
             RECORDER_SAMPLE_RATE
                 * 16
                 * 1
                 / 8)); // Byte rate, SampleRate*NumberOfChannels*BitsPerSample/8
     randomAccessWriter.writeShort(
         Short.reverseBytes(
             (short) (1 * 16 / 8))); // Block align, NumberOfChannels*BitsPerSample/8
     randomAccessWriter.writeShort(Short.reverseBytes((short) 16)); // Bits per sample
     randomAccessWriter.writeBytes("data");
     randomAccessWriter.writeInt(0); // Data chunk size not known yet, write 0
   } catch (Exception e) {
     Log.e("Microphone", "Error writing WAV header.");
   }
 }
示例#8
0
  // see DbFile.java for javadocs
  public ArrayList<Page> insertTuple(TransactionId tid, Tuple t)
      throws DbException, IOException, TransactionAbortedException {
    // some code goes here
    BufferPool bp = Database.getBufferPool();
    int id = getId(), i, slots;
    ArrayList<Page> retlist = new ArrayList<Page>();
    PageId pid = null;
    HeapPage p = null;
    for (i = 0; i < numPages(); i++) {
      pid = new HeapPageId(id, i);
      p = (HeapPage) bp.getPage(tid, pid, Permissions.READ_WRITE);
      slots = p.getNumEmptySlots();
      if (slots > 0) {
        p.insertTuple(t);
        retlist.add(p);
        return retlist;
      }
    }

    // create new page and add tuple to it
    pid = new HeapPageId(id, i);
    raf.setLength(raf.length() + BufferPool.PAGE_SIZE);
    p = (HeapPage) bp.getPage(tid, pid, Permissions.READ_WRITE);
    p.insertTuple(t);
    retlist.add(p);
    return retlist;
  }
示例#9
0
  public static DigestBlob resumeTransfer(
      BlobContainer blobContainer, String digest, UUID transferId, long currentPos) {
    DigestBlob digestBlob = new DigestBlob(blobContainer, digest, transferId);
    digestBlob.file = getTmpFilePath(blobContainer, digest, transferId);

    try {
      logger.trace("Resuming DigestBlob {}. CurrentPos {}", digest, currentPos);

      digestBlob.headFileChannel = new FileOutputStream(digestBlob.file, false).getChannel();
      digestBlob.headLength = currentPos;
      digestBlob.headSize = new AtomicLong();
      digestBlob.headCatchedUpLatch = new CountDownLatch(1);

      RandomAccessFile raf = new RandomAccessFile(digestBlob.file, "rw");
      raf.setLength(currentPos);
      raf.close();

      FileOutputStream outputStream = new FileOutputStream(digestBlob.file, true);
      digestBlob.fileChannel = outputStream.getChannel();
    } catch (IOException ex) {
      logger.error("error resuming transfer of {}, id: {}", ex, digest, transferId);
      return null;
    }

    return digestBlob;
  }
示例#10
0
 /**
  * Performs locking. If returns {@code true}, locking was successful and caller holds the lock.
  * Multiple invocations, after lock is acquired, does not have any effect, locking happens only
  * once.
  */
 public synchronized boolean lock() {
   if (fileLock != null) {
     return true;
   }
   try {
     randomAccessFile = new RandomAccessFile(lockFile, "rws");
     fileLock = randomAccessFile.getChannel().tryLock(0L, 1L, false);
     if (fileLock != null) {
       randomAccessFile.setLength(0);
       randomAccessFile.seek(0);
       randomAccessFile.write(payload);
     }
   } catch (IOException | OverlappingFileLockException e) {
     // logging is not configured yet, so use console
     System.err.println("Failed to write lock file");
     e.printStackTrace();
     // handle it as null result
     fileLock = null;
   } finally {
     if (fileLock == null) {
       release();
       return false;
     }
   }
   return true;
 }
示例#11
0
 // 下载文件
 private void download(String filePath, String destination, int threadNum) {
   try {
     // 通过下载路径获取连接
     URL url = new URL(filePath);
     HttpURLConnection conn = (HttpURLConnection) url.openConnection();
     // 设置连接的相关属性
     conn.setRequestMethod("GET");
     conn.setReadTimeout(5000);
     // 判断连接是否正确。
     if (conn.getResponseCode() == 200) {
       // 获取文件大小。
       int fileSize = conn.getContentLength();
       // 得到文件名
       String fileName = getFileName(filePath);
       // 根据文件大小及文件名,创建一个同样大小,同样文件名的文件
       File file = new File(destination + File.separator + fileName);
       RandomAccessFile raf = new RandomAccessFile(file, "rw");
       raf.setLength(fileSize);
       raf.close();
       // 将文件分成threadNum = 5份。
       int block = fileSize % threadNum == 0 ? fileSize / threadNum : fileSize / threadNum + 1;
       for (int threadId = 0; threadId < threadNum; threadId++) {
         // 传入线程编号,并开始下载。
         new DownloadThread(threadId, block, file, url).start();
       }
     }
   } catch (MalformedURLException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
  /**
   * Ensures that at least <code>pos</code> bytes are cached, or the end of the source is reached.
   * The return value is equal to the smaller of <code>pos</code> and the length of the source file.
   */
  private long readUntil(long pos) throws IOException {
    // We've already got enough data cached
    if (pos < length) {
      return pos;
    }
    // pos >= length but length isn't getting any bigger, so return it
    if (foundEOF) {
      return length;
    }

    long len = pos - length;
    cache.seek(length);
    while (len > 0) {
      // Copy a buffer's worth of data from the source to the cache
      // bufLen will always fit into an int so this is safe
      int nbytes = stream.read(buf, 0, (int) Math.min(len, (long) bufLen));
      if (nbytes == -1) {
        foundEOF = true;
        return length;
      }

      cache.setLength(cache.length() + nbytes);
      cache.write(buf, 0, nbytes);
      len -= nbytes;
      length += nbytes;
    }

    return pos;
  }
示例#13
0
  public void run() {
    try {
      System.out.println("socked");
      sock = new Socket(ii, pp);
      oout = new ObjectOutputStream(sock.getOutputStream());
      oout.flush();

      oin = new ObjectInputStream(sock.getInputStream());
      System.out.println("read ");
      rf.setLength(0);
      do {
        System.out.println("read ");
        file f = (file) oin.readObject();
        if (f.length <= 0) break;
        write(f);
      } while (true);
      oout.close();
      oin.close();
      rf.close();
      sock.close();
      xx.ConfirmPopup("Haua file namano shesh!!!");

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
示例#14
0
 /**
  * @param _lines
  * @param _file
  */
 public static void saveTextFile(Object[] _lines, File _file) {
   RandomAccessFile filer = null;
   if (_lines == null) {
     _lines = new Object[0];
   }
   if (_file == null) {
     return;
   }
   try {
     UFile.ensureDirectory(_file);
     filer = new RandomAccessFile(_file, "rw");
     filer.setLength(filer.getFilePointer());
     for (int l = 0; l < _lines.length; l++) {
       if (_lines[l] == null) {
         continue;
       }
       filer.write((_lines[l].toString() + "\n").getBytes());
     }
     filer.close();
   } catch (Exception x) {
     System.out.println(_lines);
     System.out.println(filer);
     x.printStackTrace();
   }
 }
示例#15
0
 /*
  * (non-Javadoc)
  *
  * @see java.nio.channels.SeekableByteChannel#truncate(long)
  */
 @Override
 public PicoFile truncate(long size) throws IOException {
   if (_open) _backing.setLength(size + _head.offset);
   _hashvalid = false;
   _resetDigest();
   return this;
 }
 /**
  * Save a lock password. Does not ensure that the password is as good as the requested mode, but
  * will adjust the mode to be as good as the pattern.
  *
  * @param password The password to save
  * @param quality {@see DevicePolicyManager#getPasswordQuality(android.content.ComponentName )}
  * @param isFallback Specifies if this is a fallback to biometric weak
  */
 public void saveLockPassword(String password, int quality, boolean isFallback) {
   // Compute the hash
   final byte[] hash = passwordToHash(password);
   try {
     // Write the hash to file
     RandomAccessFile raf = new RandomAccessFile(sLockPasswordFilename, "rwd");
     // Truncate the file if pattern is null, to clear the lo
     try {
       if (password == null) {
         raf.setLength(0);
       } else {
         raf.write(hash, 0, hash.length);
       }
     } finally {
       if (raf != null) raf.close();
     }
   } catch (FileNotFoundException fnfe) {
     // Cant do much, unless we want to fail over to using the settings
     // provider
     Log.e(TAG, "Unable to save lock pattern to " + sLockPasswordFilename);
   } catch (IOException ioe) {
     // Cant do much
     Log.e(TAG, "Unable to save lock pattern to " + sLockPasswordFilename);
   }
 }
示例#17
0
  /* (non-Javadoc)
   * @see com.ongraphdb.store.DiskSore1#start()
   */
  public void start() throws IOException {
    File file = new File(dataFileName);
    boolean newStore = file.exists() ? false : true;

    RandomAccessFile dataFile = new RandomAccessFile(dataFileName, "rw");
    dataFile.setLength(initialFileSize);
    dataChannel = dataFile.getChannel();
    dataLock = dataChannel.lock();
    mappedDataBuffer = dataChannel.map(MapMode.READ_WRITE, 0, dataMappedMemorySize);

    if (newStore) {
      nextPosition = NEXT_BYTES;
      mappedDataBuffer.putInt(nextPosition);
    } else {
      nextPosition = mappedDataBuffer.getInt();
    }
    shutdownHookThread =
        new Thread() {
          public void run() {
            try {
              mappedDataBuffer.force();
              dataLock.release();
              dataChannel.close();
              unmap(mappedDataBuffer);
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        };
    Runtime.getRuntime().addShutdownHook(shutdownHookThread);
  }
  /**
   * Prepares the recorder for recording, in case the recorder is not in the INITIALIZING state and
   * the file path was not set the recorder is set to the ERROR state, which makes a reconstruction
   * necessary. In case uncompressed recording is toggled, the header of the wave file is written.
   * In case of an exception, the state is changed to ERROR
   */
  public void prepare() {
    try {
      if (state == State.INITIALIZING) {
        if (rUncompressed) {
          if ((audioRecorder.getState() == AudioRecord.STATE_INITIALIZED) & (filePath != null)) {
            // write file header

            randomAccessWriter = new RandomAccessFile(filePath, "rw");

            randomAccessWriter.setLength(
                0); // Set file length to 0, to prevent unexpected behavior in case the file already
                    // existed
            randomAccessWriter.writeBytes("RIFF");
            randomAccessWriter.writeInt(0); // Final file size not known yet, write 0
            randomAccessWriter.writeBytes("WAVE");
            randomAccessWriter.writeBytes("fmt ");
            randomAccessWriter.writeInt(Integer.reverseBytes(16)); // Sub-chunk size, 16 for PCM
            randomAccessWriter.writeShort(Short.reverseBytes((short) 1)); // AudioFormat, 1 for PCM
            randomAccessWriter.writeShort(
                Short.reverseBytes(nChannels)); // Number of channels, 1 for mono, 2 for stereo
            randomAccessWriter.writeInt(Integer.reverseBytes(sRate)); // Sample rate
            randomAccessWriter.writeInt(
                Integer.reverseBytes(
                    sRate * bSamples * nChannels
                        / 8)); // Byte rate, SampleRate*NumberOfChannels*BitsPerSample/8
            randomAccessWriter.writeShort(
                Short.reverseBytes(
                    (short)
                        (nChannels * bSamples
                            / 8))); // Block align, NumberOfChannels*BitsPerSample/8
            randomAccessWriter.writeShort(Short.reverseBytes(bSamples)); // Bits per sample
            randomAccessWriter.writeBytes("data");
            randomAccessWriter.writeInt(0); // Data chunk size not known yet, write 0

            buffer = new byte[framePeriod * bSamples / 8 * nChannels];
            state = State.READY;
          } else {
            Log.e(
                ExtAudioRecorder.class.getName(),
                "prepare() method called on uninitialized recorder");
            state = State.ERROR;
          }
        } else {
          mediaRecorder.prepare();
          state = State.READY;
        }
      } else {
        Log.e(ExtAudioRecorder.class.getName(), "prepare() method called on illegal state");
        release();
        state = State.ERROR;
      }
    } catch (Exception e) {
      if (e.getMessage() != null) {
        Log.e(ExtAudioRecorder.class.getName(), e.getMessage());
      } else {
        Log.e(ExtAudioRecorder.class.getName(), "Unknown error occured in prepare()");
      }
      state = State.ERROR;
    }
  }
 private void doInstall(File target, RandomAccessFile file) throws IOException {
   file.setLength(0L);
   file.write(bytes);
   if (executable) {
     target.setExecutable(true);
   }
 }
示例#20
0
文件: FileL.java 项目: lzs0420/mytest
 /**
  * @param skip 跳过多少过字节进行插入数据
  * @param str 要插入的字符串
  * @param fileName 文件路径
  */
 public static void writeSkip(long skip, String str, String fileName) throws IOException {
   RandomAccessFile raf = null;
   try {
     raf = new RandomAccessFile(fileName, "rw");
     if (skip < 0 || skip > raf.length()) {
       System.out.println("跳过字节数无效");
       return;
     }
     byte[] b = str.getBytes();
     raf.setLength(raf.length() + b.length);
     for (long i = raf.length() - 1; i > b.length + skip - 1; i--) {
       raf.seek(i - b.length);
       byte temp = raf.readByte();
       raf.seek(i);
       raf.writeByte(temp);
     }
     raf.seek(skip);
     raf.write(b);
   } catch (Exception e) {
     throw new IOException(e);
   } finally {
     try {
       raf.close();
     } catch (IOException e) {
       throw e;
     }
   }
 }
示例#21
0
  public MappedStore(
      File file, FileChannel.MapMode mode, long size, ObjectSerializer objectSerializer)
      throws IOException {
    if (size < 0 || size > 128L << 40) {
      throw new IllegalArgumentException("invalid size: " + size);
    }

    this.file = file;
    this.size = size;
    this.objectSerializer = objectSerializer;

    try {
      RandomAccessFile raf = new RandomAccessFile(file, accesModeFor(mode));
      if (raf.length() != this.size && !file.getAbsolutePath().startsWith("/dev/")) {
        if (mode != FileChannel.MapMode.READ_WRITE) {
          throw new IOException("Cannot resize file to " + size + " as mode is not READ_WRITE");
        }

        raf.setLength(this.size);
      }

      this.fileChannel = raf.getChannel();
      this.address = map0(fileChannel, imodeFor(mode), 0L, size);
      this.cleaner = Cleaner.create(this, new Unmapper(address, size, fileChannel));
    } catch (Exception e) {
      throw wrap(e);
    }
  }
示例#22
0
  public void close() throws IOException {
    flush();

    if (!commitExecutor.isShutdown()) {
      commitExecutor.shutdown();
      try {
        if (!commitExecutor.awaitTermination(5, TimeUnit.MINUTES))
          throw new OException("Background data flush task can not be stopped.");
      } catch (InterruptedException e) {
        OLogManager.instance().error(this, "Data flush thread was interrupted");

        Thread.interrupted();
        throw new OException("Data flush thread was interrupted", e);
      }
    }

    synchronized (syncObject) {
      for (OFileClassic fileClassic : files.values()) {
        if (fileClassic.isOpen()) fileClassic.close();
      }

      if (nameIdMapHolder != null) {
        nameIdMapHolder.setLength(0);
        for (Map.Entry<String, Long> entry : nameIdMap.entrySet()) {
          writeNameIdEntry(new NameFileIdEntry(entry.getKey(), entry.getValue()), false);
        }
        nameIdMapHolder.getFD().sync();
        nameIdMapHolder.close();
      }
    }
  }
 public void write(File to) throws IOException {
   Properties props = new Properties();
   setFields(props, this);
   RandomAccessFile file = new RandomAccessFile(to, "rws");
   FileOutputStream out = null;
   try {
     file.seek(0);
     out = new FileOutputStream(file.getFD());
     /*
      * If server is interrupted before this line, the version file
      * will remain unchanged.
      */
     props.store(out, null);
     /*
      * Now the new fields are flushed to the head of the file, but
      * file length can still be larger then required and therefore
      * the file can contain whole or corrupted fields from its old
      * contents in the end. If server is interrupted here and
      * restarted later these extra fields either should not effect
      * server behavior or should be handled by the server correctly.
      */
     file.setLength(out.getChannel().position());
   } finally {
     if (out != null) {
       out.close();
     }
     file.close();
   }
 }
示例#24
0
  /**
   * 开始下载文件
   *
   * @param listener 监听下载数量的变化,如果不需要了解实时下载的数量,可以设置为null
   * @return 已下载文件大小
   * @throws Exception
   */
  public int download(DownloadProgressListener listener) throws Exception {
    try {
      RandomAccessFile randOut = new RandomAccessFile(this.saveFile, "rw");
      if (this.fileSize > 0) randOut.setLength(this.fileSize);
      randOut.close();
      URL url = new URL(this.downloadUrl);

      if (this.data.size() != this.threads.length) {
        this.data.clear();

        for (int i = 0; i < this.threads.length; i++) {
          this.data.put(i + 1, 0); // 初始化每条线程已经下载的数据长度为0
        }
      }

      for (int i = 0; i < this.threads.length; i++) { // 开启线程进行下载
        int downLength = this.data.get(i + 1);

        if (downLength < this.block && this.downloadSize < this.fileSize) { // 判断线程是否已经完成下载,否则继续下载
          this.threads[i] =
              new DownloadThread(this, url, this.saveFile, this.block, this.data.get(i + 1), i + 1);
          this.threads[i].setPriority(7);
          this.threads[i].start();
        } else {
          this.threads[i] = null;
        }
      }

      this.fileService.save(this.downloadUrl, this.data);
      boolean notFinish = true; // 下载未完成

      while (notFinish) { // 循环判断所有线程是否完成下载
        Thread.sleep(900);
        notFinish = false; // 假定全部线程下载完成

        for (int i = 0; i < this.threads.length; i++) {
          if (this.threads[i] != null && !this.threads[i].isFinish()) { // 如果发现线程未完成下载
            notFinish = true; // 设置标志为下载没有完成

            if (this.threads[i].getDownLength() == -1) { // 如果下载失败,再重新下载
              this.threads[i] =
                  new DownloadThread(
                      this, url, this.saveFile, this.block, this.data.get(i + 1), i + 1);
              this.threads[i].setPriority(7);
              this.threads[i].start();
            }
          }
        }

        if (listener != null) listener.onDownloadSize(this.downloadSize); // 通知目前已经下载完成的数据长度
      }

      fileService.delete(this.downloadUrl);
    } catch (Exception e) {
      print(e.toString());
      throw new Exception("file download fail");
    }
    return this.downloadSize;
  }
 public void setLength(long newLength) throws IOException {
   if (newLength > 0) {
     this.fileendpos = newLength - 1;
   } else {
     this.fileendpos = 0;
   }
   super.setLength(newLength);
 }
  private static void truncateBlock(File blockFile, File metaFile, long oldlen, long newlen)
      throws IOException {
    if (newlen == oldlen) {
      return;
    }
    if (newlen > oldlen) {
      throw new IOException(
          "Cannout truncate block to from oldlen (=" + oldlen + ") to newlen (=" + newlen + ")");
    }

    DataChecksum dcs = BlockMetadataHeader.readHeader(metaFile).getChecksum();
    int checksumsize = dcs.getChecksumSize();
    int bpc = dcs.getBytesPerChecksum();
    long n = (newlen - 1) / bpc + 1;
    long newmetalen = BlockMetadataHeader.getHeaderSize() + n * checksumsize;
    long lastchunkoffset = (n - 1) * bpc;
    int lastchunksize = (int) (newlen - lastchunkoffset);
    byte[] b = new byte[Math.max(lastchunksize, checksumsize)];

    RandomAccessFile blockRAF = new RandomAccessFile(blockFile, "rw");
    try {
      // truncate blockFile
      blockRAF.setLength(newlen);

      // read last chunk
      blockRAF.seek(lastchunkoffset);
      blockRAF.readFully(b, 0, lastchunksize);
    } finally {
      blockRAF.close();
    }

    // compute checksum
    dcs.update(b, 0, lastchunksize);
    dcs.writeValue(b, 0, false);

    // update metaFile
    RandomAccessFile metaRAF = new RandomAccessFile(metaFile, "rw");
    try {
      metaRAF.setLength(newmetalen);
      metaRAF.seek(newmetalen - checksumsize);
      metaRAF.write(b, 0, checksumsize);
    } finally {
      metaRAF.close();
    }
  }
示例#27
0
 public void deleteAll() throws IOException {
   // Clear file
   RandomAccessFile file = new RandomAccessFile(filePath, "rw");
   file.setLength(0);
   // Clear cache
   fieldNameMapping.clear();
   fieldIdMapping.clear();
   fieldTypeStore.deleteAll();
 }
示例#28
0
 /**
  * To call this method the FileLogReader must have been created as writable.
  *
  * @param size
  * @throws IOException
  */
 public synchronized void truncate(long size) throws IOException {
   if (size < mRAF.length()) {
     mRAF.setLength(size);
     FileHeader hdr = getHeader();
     hdr.setFileSize(size);
     hdr.write(mRAF);
     mRAF.seek(size);
   }
 }
 private void writeIntoFile(int numOfDir, int numOfFile) {
   String dirString = String.valueOf(numOfDir) + ".dir";
   String fileString = String.valueOf(numOfFile) + ".dat";
   File dbDir = tableDir.toPath().resolve(dirString).normalize().toFile();
   if (!dbDir.isDirectory()) {
     dbDir.mkdir();
   }
   File dbFile = dbDir.toPath().resolve(fileString).normalize().toFile();
   if (list[numOfDir][numOfFile].isEmpty()) {
     dbFile.delete();
     if (dbDir.list().length == 0) {
       dbDir.delete();
     }
     return;
   }
   RandomAccessFile db;
   try {
     db = new RandomAccessFile(dbFile, "rw");
     try {
       db.setLength(0);
       Iterator<Map.Entry<String, String>> it;
       it = list[numOfDir][numOfFile].entrySet().iterator();
       long[] pointers = new long[list[numOfDir][numOfFile].size()];
       int counter = 0;
       while (it.hasNext()) {
         Map.Entry<String, String> m = (Map.Entry<String, String>) it.next();
         String key = m.getKey();
         db.write(key.getBytes("UTF-8"));
         db.write("\0".getBytes("UTF-8"));
         pointers[counter] = db.getFilePointer();
         db.seek(pointers[counter] + 4);
         ++counter;
       }
       it = list[numOfDir][numOfFile].entrySet().iterator();
       counter = 0;
       while (it.hasNext()) {
         Map.Entry<String, String> m = (Map.Entry<String, String>) it.next();
         String value = m.getValue();
         int curPointer = (int) db.getFilePointer();
         db.seek(pointers[counter]);
         db.writeInt(curPointer);
         db.seek(curPointer);
         db.write(value.getBytes("UTF-8"));
         ++counter;
       }
     } catch (Exception e) {
       db.close();
       throw new Exception(e);
     }
     db.close();
     if (dbDir.list().length == 0) {
       dbDir.delete();
     }
   } catch (Exception e) {
     throw new IllegalArgumentException();
   }
 }
示例#30
0
  private static TempFile createTempFile() throws IOException {
    File tempFile = File.createTempFile(UUID.randomUUID().toString(), null);
    if (logger.isDebugEnabled()) {
      logger.debug("Creating new SpillFile: " + tempFile.getAbsolutePath());
    }
    RandomAccessFile file = new RandomAccessFile(tempFile, "rw");
    file.setLength(SPILL_FILE_SIZE);

    return new TempFile(tempFile, file);
  }