예제 #1
0
  public static void main(String args[]) throws Exception {
    String inputFile = "samplein.txt";
    String outputFile = "sampleout.txt";

    RandomAccessFile inf = new RandomAccessFile(inputFile, "r");
    RandomAccessFile outf = new RandomAccessFile(outputFile, "rw");
    long inputLength = new File(inputFile).length();

    FileChannel inc = inf.getChannel();
    FileChannel outc = outf.getChannel();

    MappedByteBuffer inputData = inc.map(FileChannel.MapMode.READ_ONLY, 0, inputLength);

    Charset latin1 = Charset.forName("ISO-8859-1");
    CharsetDecoder decoder = latin1.newDecoder();
    CharsetEncoder encoder = latin1.newEncoder();

    CharBuffer cb = decoder.decode(inputData);

    // Process char data here

    ByteBuffer outputData = encoder.encode(cb);

    outc.write(outputData);

    inf.close();
    outf.close();
  }
예제 #2
0
 /**
  * reads the content of an existing file using the current domain
  *
  * @param domain The namespace used to identify the application domain (1st level directory) to
  *     use
  * @param path The path relative to the domain for the file
  * @param block The sequential block number for the data to be read starting with 1
  * @param len The length of the block in bytes
  * @return The contents of the file
  */
 public byte[] readFromFile(String path, int block, int len) {
   byte[] buffer = null;
   try {
     if (_isLocal) {
       File tmpFile = new File(_rootFile.getCanonicalPath() + File.separatorChar + path);
       if (tmpFile.isFile()) {
         RandomAccessFile in = new RandomAccessFile(tmpFile, "r");
         in.seek((block - 1) * len);
         int result = -1;
         buffer = new byte[len];
         if (in.getFilePointer() < in.length()) {
           result = in.read(buffer);
           ByteArrayOutputStream out = new ByteArrayOutputStream(result);
           out.write(buffer, 0, result);
           in.close();
           return out.toByteArray();
         } else {
           in.close();
         }
       }
     } else {
       buffer = _remote.readFromFile(_domain, path, block, len);
     }
   } catch (Exception ex) {
     ex.printStackTrace();
   }
   return buffer;
 }
예제 #3
0
 public void shutdown() {
   try {
     mDirectoryFile.close();
     mDataFile.close();
   } catch (IOException ioe) {
     System.err.println("LineServer: error closing files");
   }
 }
예제 #4
0
  private void merge(SingleHit[] hits, String prefix, int chrom) throws IOException {
    String postmp = getPositionsFname(prefix, chrom) + ".tmp";
    String weightstmp = getWeightsFname(prefix, chrom) + ".tmp";
    String lastmp = getLaSFname(prefix, chrom) + ".tmp";
    RandomAccessFile positionsRAF = new RandomAccessFile(postmp, "rw");
    RandomAccessFile weightsRAF = new RandomAccessFile(weightstmp, "rw");
    RandomAccessFile lasRAF = new RandomAccessFile(lastmp, "rw");
    int newsize = getPositionsBuffer().limit() + hits.length;
    IntBP posfile =
        new IntBP(positionsRAF.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, newsize * 4));
    FloatBP weightfile =
        new FloatBP(weightsRAF.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, newsize * 4));
    IntBP lasfile =
        new IntBP(lasRAF.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, newsize * 4));

    int oldp = 0;
    int newp = 0;
    int pos = 0;
    IntBP oldpositions = getPositionsBuffer();
    FloatBP oldweights = getWeightsBuffer();
    IntBP oldlas = getLASBuffer();
    while (oldp < oldpositions.limit() || newp < hits.length) {
      while (newp < hits.length
          && (oldp == oldpositions.limit() || hits[newp].pos <= oldpositions.get(oldp))) {
        posfile.put(pos, hits[newp].pos);
        weightfile.put(pos, hits[newp].weight);
        lasfile.put(pos, Hits.makeLAS(hits[newp].length, hits[newp].strand));
        newp++;
        pos++;
      }
      while (oldp < oldpositions.limit()
          && (newp == hits.length || oldpositions.get(oldp) <= hits[newp].pos)) {
        posfile.put(pos, oldpositions.get(oldp));
        weightfile.put(pos, oldweights.get(oldp));
        lasfile.put(pos, oldlas.get(oldp));
        oldp++;
        pos++;
      }
      //            System.err.println(String.format("%d %d %d", pos, newp, oldp));
    }
    posfile = null;
    weightfile = null;
    lasfile = null;
    oldpositions = null;
    oldweights = null;
    oldlas = null;
    positionsRAF.close();
    weightsRAF.close();
    lasRAF.close();
    /* ideally this part with the renames would atomic... */
    (new File(postmp)).renameTo(new File(getPositionsFname(prefix, chrom)));
    (new File(weightstmp)).renameTo(new File(getWeightsFname(prefix, chrom)));
    (new File(lastmp)).renameTo(new File(getLaSFname(prefix, chrom)));
  }
 public synchronized void forceClose() throws IOException {
   if (cnt > 0) {
     // normal case, close exceptions propagated
     cnt = 0;
     in.close();
   } else {
     // should already be closed, ignore exception
     try {
       in.close();
     } catch (IOException ioex) {
     }
   }
 }
예제 #6
0
  /**
   * Add the given text string to the end of a given file.
   *
   * @param inStr The string to be added.
   * @param fileStr the name of the file to be added.
   */
  public static void addStringToFile(String inStr, String fileStr) throws Exception {

    RandomAccessFile raFile = new RandomAccessFile(fileStr, "rw");
    raFile.seek(raFile.length());
    raFile.writeBytes(inStr);
    raFile.close();
  }
예제 #7
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;
  }
예제 #8
0
  public static void main(String[] args) {
    /*System.out.println(modular_exponent_32(2,36,37));
    System.out.println(modular_exponent_32(2,18,37));
    System.out.println(modular_exponent_32(2,9,37));
    int d = Integer.numberOfTrailingZeros(64);
    System.out.println(d);
    */
    int counter = how_many_primes(1000000);
    System.out.println(counter);

    try {
      RandomAccessFile out = new RandomAccessFile("mrprimes.txt", "rw");
      out.writeBytes("2\n");
      for (int i = 3; i < 1000000; i += 2) {
        if (miller_rabin_32(i)) {
          out.writeBytes(Integer.toString(i) + "\n");
        }
      }
      // for (int i=0; i<counter; i++){
      // out.seek(i*4);
      // System.out.println(","+out.readInt());
      // }
      out.close();
    } catch (IOException e) {
    }
    System.out.println(miller_rabin_32(1105));
  }
예제 #9
0
  /**
   * Ecrit la piece donnée dans le fichier temporaire sur le disque
   *
   * @param piece : pièce à écrire
   * @param num : numéros de la pièce
   */
  private synchronized void writePieceTmpFile(byte[] piece, int num) {
    if (num < 0 || num >= this.nbPieces()) {
      throw new IllegalArgumentException();
    }
    if (piece.length > _piecesize) {
      throw new IllegalArgumentException();
    }

    try {
      RandomAccessFile writer_tmp = new RandomAccessFile(this, "rw");
      FileChannel writer = writer_tmp.getChannel();

      int index_piece = ((int) this.length() - this.headerSize()) / _piecesize;

      if (piece.length < _piecesize) {
        piece = Arrays.copyOf(piece, _piecesize);
      }
      Tools.write(writer, 4 + _key.length() + 4 + 4 + 4 * num, index_piece);
      Tools.write(writer, this.headerSize() + _piecesize * index_piece, piece);

      writer.force(true);
      writer_tmp.close();
    } catch (Exception e) {
      System.out.println("Unable to write tmp file piece");
      e.printStackTrace();
    }
  }
예제 #10
0
 /**
  * reads the content of an existing file using the current domain
  *
  * @param domain The namespace used to identify the application domain (1st level directory) to
  *     use
  * @param path The path relative to the domain for the file
  * @param offset the offset from the beginning of the file.
  * @param len The length of the block in bytes
  * @return The contents of the file
  */
 public byte[] readByteFromFile(String path, long offset, int len)
     throws EOFException, FileAccessException {
   try {
     if (_isLocal) {
       File tmpFile = new File(_rootFile.getCanonicalPath() + File.separatorChar + path);
       if (tmpFile.isFile()) {
         RandomAccessFile raf = new RandomAccessFile(tmpFile, "r");
         byte[] buffer = new byte[len];
         raf.seek(offset);
         int totalByteRead = 0;
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         int result = 0;
         while (totalByteRead < len && raf.getFilePointer() < raf.length()) {
           result = raf.read(buffer, 0, (len - totalByteRead));
           if (result != -1) {
             out.write(buffer, 0, result);
             totalByteRead += result;
           } else if (totalByteRead == 0) throw new EOFException("End of file reached!");
           else break;
         }
         raf.close();
         out.flush();
         out.close();
         return out.toByteArray();
       } else throw new FileAccessException("Path is not a file");
     } else return _remote.readByteFromFile(_domain, path, offset, len);
   } catch (EOFException eofe) {
     throw eofe;
   } catch (FileAccessException fae) {
     throw fae;
   } catch (Exception e) {
     throw new FileAccessException(e);
   }
 }
예제 #11
0
파일: GIDE.java 프로젝트: rexut/jkcemu
 private void execFormatTrackTask() {
   File file = this.ioFile;
   long pos = this.ioFilePos;
   int cnt = this.ioByteCnt;
   if ((file != null) && (pos >= 0) && (cnt > 0)) {
     boolean err = false;
     RandomAccessFile raf = null;
     try {
       raf = new RandomAccessFile(file, "rw");
       raf.seek(pos);
       while (cnt > 0) {
         raf.write(0);
         --cnt;
       }
       raf.close();
       raf = null;
     } catch (IOException ex) {
       err = true;
       if (!this.writeErrShown) {
         this.writeErrShown = true;
         EmuUtil.fireShowError(this.owner, null, ex);
       }
     } finally {
       EmuUtil.doClose(raf);
     }
     if (err) {
       this.errorReg = ERROR_UNCORRECTABLE_DATA;
       this.statusReg |= STATUS_ERROR;
     }
     fireInterrupt();
   }
 }
예제 #12
0
 public static boolean clearWorldReference(World world) {
   String worldname = world.getName();
   if (regionfiles == null) return false;
   if (rafField == null) return false;
   ArrayList<Object> removedKeys = new ArrayList<Object>();
   try {
     for (Object o : regionfiles.entrySet()) {
       Map.Entry e = (Map.Entry) o;
       File f = (File) e.getKey();
       if (f.toString().startsWith("." + File.separator + worldname)) {
         SoftReference ref = (SoftReference) e.getValue();
         try {
           RegionFile file = (RegionFile) ref.get();
           if (file != null) {
             RandomAccessFile raf = (RandomAccessFile) rafField.get(file);
             raf.close();
             removedKeys.add(f);
           }
         } catch (Exception ex) {
           ex.printStackTrace();
         }
       }
     }
   } catch (Exception ex) {
     MyWorlds.log(
         Level.WARNING, "Exception while removing world reference for '" + worldname + "'!");
     ex.printStackTrace();
   }
   for (Object key : removedKeys) {
     regionfiles.remove(key);
   }
   return true;
 }
예제 #13
0
  /** Met dans le cache le résultat de l'url indiquée */
  public boolean putInCache(String url) throws Exception {

    // Lecture
    MyInputStream in = null;
    try {
      in = Util.openStream(url);
      byte buf[] = in.readFully();

      // Nettoyage et Ecriture
      String id = codage(url);
      File g = new File(dir + Util.FS + id);
      g.delete();
      RandomAccessFile f = null;
      try {
        f = new RandomAccessFile(dir + Util.FS + id, "rw");
        f.write(buf);
      } finally {
        if (f != null) f.close();
      }
      aladin.trace(3, "Put in cache [" + url + "]");
      return true;
    } catch (Exception e) {
      if (aladin.levelTrace >= 3) e.printStackTrace();
    } finally {
      if (in != null) in.close();
    }

    return false;
  }
  /**
   * Reads a file from the given InputStream and saves it into the local download directory
   *
   * @param in The InputStream from which to read the file
   * @param start The starting byte of the piece that will be downloaded from this peer
   * @param end The end byte (exclusive) of the piece that will be downloaded from this peer
   * @throws java.io.IOException if an error occurs while downloading the file
   */
  private void saveFile(InputStream in, int start, int end) throws IOException {
    if (start >= end) {
      throw new IllegalStateException(
          "Start byte (" + start + ") must be less than the end byte (" + end + ")");
    }

    RandomAccessFile fileOut = null;
    try {
      File downloadFile = new File(Client.downloadDir, _file);
      // Open the file in read/write mode with synchronous content/metadata writing
      fileOut = new RandomAccessFile(downloadFile, "rws");
      fileOut.seek(start);

      byte[] bytes = new byte[_chunkSize];
      int bytesRead;
      int pos = start;
      while ((bytesRead = in.read(bytes, 0, _chunkSize)) >= 0 && pos < end) {
        fileOut.write(bytes, 0, Math.min(bytesRead, end - pos));
        pos += bytesRead;
      }
    } catch (SocketException e) {
      System.err.println(
          "[DownloadFromPeerRunnable] Disconnected from peer at " + _peer.getInetAddress());
      _callback.onFinishChunk(_file, false);
    } finally {
      if (fileOut != null) {
        try {
          fileOut.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
예제 #15
0
 @Override
 public ByteBuffer readPage(File file, long position, ByteBuffer pageBuffer)
     throws IOException, InterruptedException {
   long start = System.currentTimeMillis();
   RandomAccessFile randomAccessFile = randomAccessFile(file);
   try {
     randomAccessFile.seek(position);
     randomAccessFile.readFully(pageBuffer.array(), pageBuffer.arrayOffset(), pageSizeBytes);
     if (Thread.interrupted()) {
       throw new InterruptedException();
     }
     long stop = System.currentTimeMillis();
     if (LOG.isLoggable(Level.FINE)) {
       LOG.log(
           Level.FINE,
           "Read page at {0} of {1}: {2} msec",
           new Object[] {position, file, stop - start});
     }
   } catch (EOFException e) {
     LOG.log(
         Level.SEVERE,
         "Caught EOFException while reading {0}, position {1}",
         new Object[] {file, position});
     LOG.log(Level.SEVERE, "stack", e);
     throw e;
   } finally {
     randomAccessFile.close();
   }
   return pageBuffer;
 }
 protected void finalize() throws Throwable {
   try {
     in.close();
   } finally {
     super.finalize();
   }
 }
예제 #17
0
파일: Receiver.java 프로젝트: bh00t/Sharing
  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();
    }
  }
예제 #18
0
  /**
   * Loads a query from the mgf file.
   *
   * @param file The file to read the query from.
   * @param indexElement The index element pointing to that specific ms2 query.
   * @return
   * @oaram index The query's 1-based index in the MGF file. This index is stored in the returned
   *     Ms2Query object.
   */
  private static Ms2Query loadIndexedQueryFromFile(File file, IndexElement indexElement, int index)
      throws JMzReaderException {
    RandomAccessFile accFile = null;
    try {
      accFile = new RandomAccessFile(file, "r");

      // read the indexed element
      byte[] byteBuffer = new byte[indexElement.getSize()];

      // read the file from there
      accFile.seek(indexElement.getStart());
      accFile.read(byteBuffer);
      String ms2Buffer = new String(byteBuffer);
      // create the query
      Ms2Query query = new Ms2Query(ms2Buffer, index);

      return query;
    } catch (FileNotFoundException e) {
      throw new JMzReaderException("MGF file could not be found.", e);
    } catch (IOException e) {
      throw new JMzReaderException("Failed to read from MGF file", e);
    } finally {
      if (accFile != null) {
        try {
          accFile.close();
        } catch (IOException e) {
          // ignore
        }
      }
    }
  }
예제 #19
0
 /** Method description */
 public void close() {
   try {
     f.close(); // close file
   } catch (IOException e) {
     System.out.println("File close error");
     errflag = true;
   }
 }
 private static void preallocateTestFile(final String fileName) throws Exception {
   // Create the file on the Indexes Disk and pre-allocate size
   RandomAccessFile file = new RandomAccessFile(indexFileSystemPath + "/" + fileName, "rw");
   for (long i = 0; i < FILE_SIZE; i += PAGE_SIZE) {
     file.write(BLANK_PAGE, 0, PAGE_SIZE);
   }
   file.close();
 }
예제 #21
0
 private void append(SingleHit[] hits, String prefix, int chrom) throws IOException {
   RandomAccessFile positionsRAF = new RandomAccessFile(getPositionsFname(prefix, chrom), "rw");
   RandomAccessFile weightsRAF = new RandomAccessFile(getWeightsFname(prefix, chrom), "rw");
   RandomAccessFile lasRAF = new RandomAccessFile(getLaSFname(prefix, chrom), "rw");
   positionsRAF.seek(positionsRAF.length());
   weightsRAF.seek(weightsRAF.length());
   lasRAF.seek(lasRAF.length());
   for (int i = 0; i < hits.length; i++) {
     SingleHit h = hits[i];
     positionsRAF.writeInt(h.pos);
     weightsRAF.writeFloat(h.weight);
     lasRAF.writeInt(makeLAS(h.length, h.strand));
   }
   positionsRAF.close();
   weightsRAF.close();
   lasRAF.close();
 }
예제 #22
0
파일: MessageFile.java 프로젝트: imace/zava
 /** Close this file if no-one else is using it (see {@link #use()}). NOP if already closed. */
 public void closeIfUnused() throws IOException {
   synchronized (channel) {
     if (isOpen() && --usageCounter <= 0) {
       checkpoint(true);
       raf.close();
     }
   }
 }
예제 #23
0
  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();
    }
  }
예제 #24
0
  public void resort(String prefix, int chrom) throws IOException {
    IntBP positions = getPositionsBuffer();
    FloatBP weights = getWeightsBuffer();
    IntBP las = getLASBuffer();
    long indices[] = new long[positions.limit()];
    for (int i = 0; i < indices.length; i++) {
      long v = positions.get(i);
      v <<= 32;
      v |= i;
      indices[i] = v;
    }
    Arrays.sort(indices);

    String postmp = getPositionsFname(prefix, chrom) + ".tmp";
    String weightstmp = getWeightsFname(prefix, chrom) + ".tmp";
    String lastmp = getLaSFname(prefix, chrom) + ".tmp";
    RandomAccessFile positionsRAF = new RandomAccessFile(postmp, "rw");
    RandomAccessFile weightsRAF = new RandomAccessFile(weightstmp, "rw");
    RandomAccessFile lasRAF = new RandomAccessFile(lastmp, "rw");
    int newsize = getPositionsBuffer().limit();
    IntBP posfile =
        new IntBP(positionsRAF.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, newsize * 4));
    FloatBP weightfile =
        new FloatBP(weightsRAF.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, newsize * 4));
    IntBP lasfile =
        new IntBP(lasRAF.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, newsize * 4));

    for (int i = 0; i < indices.length; i++) {
      int index = (int) (indices[i] & 0xffffffffL);
      int pos = (int) (indices[i] >> 32);
      posfile.put(i, pos);
      weightfile.put(i, weights.get(index));
      lasfile.put(i, las.get(index));
    }
    posfile = null;
    weightfile = null;
    lasfile = null;
    positionsRAF.close();
    weightsRAF.close();
    lasRAF.close();
    /* ideally this part with the renames would atomic... */
    (new File(postmp)).renameTo(new File(getPositionsFname(prefix, chrom)));
    (new File(weightstmp)).renameTo(new File(getWeightsFname(prefix, chrom)));
    (new File(lastmp)).renameTo(new File(getLaSFname(prefix, chrom)));
  }
예제 #25
0
 /**
  * Stop recording. This is not strictly necessary since the RandomAccessFile will keep most of the
  * information that has been logged.
  */
 public static void endRecord() {
   try {
     if (!recording) return;
     if (R != null) R.close();
     if (recording) recording = false;
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
예제 #26
0
 public static byte[] loadFileIntoByteArray(String path) throws IOException {
   RandomAccessFile f = new RandomAccessFile(path, "r");
   try {
     byte[] data = new byte[(int) f.length()];
     f.readFully(data);
     return data;
   } finally {
     f.close();
   }
 }
예제 #27
0
파일: MessageFile.java 프로젝트: imace/zava
 /** Close this file even if it is in use. NOP if already closed. */
 @Override
 public void close() throws IOException {
   synchronized (channel) {
     if (isOpen()) {
       --usageCounter;
       checkpoint(true);
       raf.close();
     }
   }
 }
    protected void destroy(Throwable error) {
      if (error != null) {

        Debug.out(error);
      }

      synchronized (lock) {
        if (context_destroyed) {

          return;
        }

        context_destroyed = true;

        if (channels != null) {

          List<channel> channels_copy = new ArrayList<channel>(channels);

          for (channel c : channels_copy) {

            c.destroy();
          }
        }

        if (raf != null) {

          try {
            raf.close();

          } catch (Throwable e) {
          }

          raf = null;
        }

        if (stream_details != null) {

          try {
            stream_details.getStream().close();

          } catch (Throwable e) {

          }

          stream_details = null;
        }

        if (error != null) {

          save_to.delete();
        }
      }

      DiskManagerFileInfoStream.this.destroyed(this);
    }
예제 #29
0
  /**
   * Creates and initializes an SPV block store. Will create the given file if it's missing. This
   * operation will block on disk.
   */
  public SPVBlockStore(NetworkParameters params, File file) throws BlockStoreException {
    checkNotNull(file);
    this.params = checkNotNull(params);
    try {
      this.numHeaders = DEFAULT_NUM_HEADERS;
      boolean exists = file.exists();
      // Set up the backing file.
      randomAccessFile = new RandomAccessFile(file, "rw");
      long fileSize = getFileSize();
      if (!exists) {
        log.info("Creating new SPV block chain file " + file);
        randomAccessFile.setLength(fileSize);
      } else if (randomAccessFile.length() != fileSize) {
        throw new BlockStoreException(
            "File size on disk does not match expected size: "
                + randomAccessFile.length()
                + " vs "
                + fileSize);
      }

      FileChannel channel = randomAccessFile.getChannel();
      fileLock = channel.tryLock();
      if (fileLock == null)
        throw new BlockStoreException("Store file is already locked by another process");

      // Map it into memory read/write. The kernel will take care of flushing writes to disk at the
      // most
      // efficient times, which may mean that until the map is deallocated the data on disk is
      // randomly
      // inconsistent. However the only process accessing it is us, via this mapping, so our own
      // view will
      // always be correct. Once we establish the mmap the underlying file and channel can go away.
      // Note that
      // the details of mmapping vary between platforms.
      buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, fileSize);

      // Check or initialize the header bytes to ensure we don't try to open some random file.
      byte[] header;
      if (exists) {
        header = new byte[4];
        buffer.get(header);
        if (!new String(header, "US-ASCII").equals(HEADER_MAGIC))
          throw new BlockStoreException("Header bytes do not equal " + HEADER_MAGIC);
      } else {
        initNewStore(params);
      }
    } catch (Exception e) {
      try {
        if (randomAccessFile != null) randomAccessFile.close();
      } catch (IOException e2) {
        throw new BlockStoreException(e2);
      }
      throw new BlockStoreException(e);
    }
  }
예제 #30
0
 public static String read(String s) throws IOException {
   String s1;
   RandomAccessFile randomaccessfile;
   s1 = "";
   randomaccessfile = null;
   File file = new File(s);
   randomaccessfile = new RandomAccessFile(file, "r");
   s1 = randomaccessfile.readLine();
   randomaccessfile.close();
   return s1;
 }