Java Code Examples for java.nio.channels.FileChannel#open()

The following examples show how to use java.nio.channels.FileChannel#open() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestMetastoreDatabaseFactory.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
private boolean take() throws IOException {
    if (!isLocked.get()) {
        synchronized (syncObject) {
            if (!isLocked.get()) {
                if (fileChannel == null) {
                    Path lockPath = Paths.get(System.getProperty("user.home")).resolve(".embedmysql.lock");
                    fileChannel = FileChannel.open(lockPath, StandardOpenOption.CREATE,
                            StandardOpenOption.WRITE, StandardOpenOption.READ);
                }
                fileLock = fileChannel.lock();
                isLocked.set(true);
                return true;
            }
            return true;
        }
    }
    return true;
}
 
Example 2
Source File: JobPrefixFile.java    From helios with Apache License 2.0 6 votes vote down vote up
private JobPrefixFile(final String prefix, final Path directory) throws IOException {
  Preconditions.checkNotNull(directory);
  this.prefix = prefix == null
                ? "tmp-" + new SimpleDateFormat("yyyyMMdd").format(new Date()) + "-"
                  + toHexString(ThreadLocalRandom.current().nextInt())
                : prefix;

  // Make sure directory exists, then create prefix file
  Files.createDirectories(directory);
  file = directory.resolve(this.prefix);
  final Path tmp = directory.resolve(this.prefix + ".tmp");
  try {
    // If we didn't create the file with the .tmp extension, there would be a race condition
    // where another process could read the file and try to delete it before we obtained the lock
    this.channel = FileChannel.open(tmp, CREATE_NEW, WRITE);
    this.lock = channel.lock();
    Files.move(tmp, file);
  } catch (Exception e) {
    deleteIfExists(tmp);
    deleteIfExists(file);
    close(this.channel);
    throw new RuntimeException("Failed to create job prefix file " + file, e);
  }
}
 
Example 3
Source File: LocalTranslog.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private FileChannel openReader(long generationId) throws IOException {
    ensureOpen();
    if (readChannels.containsKey(generationId)) {
        return readChannels.get(generationId);
    }
    try {
        Path translogFilePath = this.translogPath.resolve(getFileNameFromId(tmpTranslogGeneration.get()));
        if (!Files.exists(translogFilePath)) {
            return null;
        }
        // maybe a lot of readers try to open reader and put it to readChannel cache, because read lock is shared
        FileChannel readChannel = FileChannel.open(translogFilePath, StandardOpenOption.READ);
        FileChannel originReadChannel = readChannels.putIfAbsent(generationId, readChannel);
        if (originReadChannel != null) {
            IOUtils.close(readChannel);
            return originReadChannel;
        } else {
            return readChannel;
        }
    } catch (Throwable e) {
        throw e;
    }
}
 
Example 4
Source File: XdrStreamIteratorTest.java    From monsoon with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test(expected = RuntimeException.class)
public void truncated_file() throws Exception {
    final MappedByteBuffer buf;
    try (FileChannel fd = FileChannel.open(tmpfile, READ)) {
        buf = fd.map(FileChannel.MapMode.READ_ONLY, 0, fd.size() - 10);
    }

    XdrStreamIterator iter = new XdrStreamIterator(buf);

    assertTrue(buf.position() > 0);  // Must have validated buffer.
    assertTrue(iter.hasNext());

    /* Read all the things. */
    List<TimeSeriesCollection> read_tsdata = new ArrayList<>();
    iter.forEachRemaining(read_tsdata::add);
}
 
Example 5
Source File: CheckZombieLockTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Setup all the files and directories needed for the tests
 *
 * @return writable directory created that needs to be deleted when done
 * @throws RuntimeException
 */
private static File setup() throws RuntimeException {
    // First do some setup in the temporary directory (using same logic as
    // FileHandler for %t pattern)
    String tmpDir = System.getProperty("java.io.tmpdir"); // i.e. %t
    if (tmpDir == null) {
        tmpDir = System.getProperty("user.home");
    }
    File tmpOrHomeDir = new File(tmpDir);
    // Create a writable directory here (%t/writable-lockfile-dir)
    File writableDir = new File(tmpOrHomeDir, WRITABLE_DIR);
    if (!createFile(writableDir, true)) {
        throw new RuntimeException("Test setup failed: unable to create"
                + " writable working directory "
                + writableDir.getAbsolutePath() );
    }

    // try to determine whether file locking is supported
    final String uniqueFileName = UUID.randomUUID().toString()+".lck";
    try {
        FileChannel fc = FileChannel.open(Paths.get(writableDir.getAbsolutePath(),
                uniqueFileName),
                StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND,
                StandardOpenOption.DELETE_ON_CLOSE);
        try {
            fc.tryLock();
        } catch(IOException x) {
            supportsLocking = false;
        } finally {
            fc.close();
        }
    } catch (IOException t) {
        // should not happen
        System.err.println("Failed to create new file " + uniqueFileName +
                " in " + writableDir.getAbsolutePath());
        throw new RuntimeException("Test setup failed: unable to run test", t);
    }
    return writableDir;
}
 
Example 6
Source File: PerfDao.java    From oneops with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the state filename and open a file channel for writing.
 *
 * @param filename file name of state
 */
public void setStateFilename(String filename) {
    stateFilename = filename;
    File sFile = new File(stateFilename);
    try {
        if (!sFile.exists()) {
            sFile.createNewFile();
        }
        logger.info("Creating the file channel for " + stateFilename);
        statChannel = FileChannel.open(Paths.get(stateFilename), StandardOpenOption.WRITE);
    } catch (Exception ex) {
        logger.error("Error setting stat file." + sFile.getAbsolutePath(), ex);
        System.exit(1);
    }
}
 
Example 7
Source File: LocalRecoverableFsDataOutputStream.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
LocalRecoverableFsDataOutputStream(LocalRecoverable resumable) throws IOException {
	this.targetFile = checkNotNull(resumable.targetFile());
	this.tempFile = checkNotNull(resumable.tempFile());

	if (!tempFile.exists()) {
		throw new FileNotFoundException("File Not Found: " + tempFile.getAbsolutePath());
	}

	this.fileChannel = FileChannel.open(tempFile.toPath(), StandardOpenOption.WRITE, StandardOpenOption.APPEND);
	if (this.fileChannel.position() < resumable.offset()) {
		throw new IOException("Missing data in tmp file: " + tempFile.getAbsolutePath());
	}
	this.fileChannel.truncate(resumable.offset());
	this.fos = Channels.newOutputStream(fileChannel);
}
 
Example 8
Source File: AtomicAppend.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
static FileChannel newFileChannel(File file) throws IOException {
    if (rand.nextBoolean()) {
        return new FileOutputStream(file, true).getChannel();
    } else {
        return FileChannel.open(file.toPath(), APPEND);
    }
}
 
Example 9
Source File: CommitIdDatabaseTest.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
@Test
void rebuildingBadDatabase() throws Exception {
    final int numCommits = 10;
    final File repoDir = tempDir;
    final File commitIdDatabaseFile = new File(repoDir, "commit_ids.dat");

    // Create a repository which contains some commits.
    GitRepository repo = new GitRepository(mock(Project.class), repoDir, commonPool(), 0, Author.SYSTEM);
    Revision headRevision = null;
    try {
        for (int i = 1; i <= numCommits; i++) {
            headRevision = repo.commit(new Revision(i), 0, Author.SYSTEM, "",
                                       Change.ofTextUpsert("/" + i + ".txt", "")).join();
        }
    } finally {
        repo.internalClose();
    }

    // Wipe out the commit ID database.
    assertThat(commitIdDatabaseFile).exists();
    try (FileChannel ch = FileChannel.open(commitIdDatabaseFile.toPath(), StandardOpenOption.WRITE)) {
        ch.truncate(0);
    }

    // Open the repository again to see if the commit ID database is regenerated automatically.
    repo = new GitRepository(mock(Project.class), repoDir, commonPool(), null);
    try {
        assertThat(repo.normalizeNow(Revision.HEAD)).isEqualTo(headRevision);
        for (int i = 1; i <= numCommits; i++) {
            assertThat(repo.find(new Revision(i + 1), "/" + i + ".txt").join()).hasSize(1);
        }
    } finally {
        repo.internalClose();
    }

    assertThat(Files.size(commitIdDatabaseFile.toPath())).isEqualTo((numCommits + 1) * 24L);
}
 
Example 10
Source File: Task.java    From jlibs with Apache License 2.0 5 votes vote down vote up
protected void prepareTransferFromFile(File file) throws IOException{
    fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ);
    fileOffset = 0;
    fileLength = fileChannel.size();
    if(out instanceof ChunkedOutput)
        ((ChunkedOutput)out).startChunk(fileLength);
}
 
Example 11
Source File: LargeMessageControllerImpl.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * @throws FileNotFoundException
 */
private FileChannel checkOpen() throws IOException {
   FileChannel channel = cachedChannel;
   if (cachedFile != null || !channel.isOpen()) {
      channel = FileChannel.open(cachedFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE);
      cachedChannel = channel;
   }
   return channel;
}
 
Example 12
Source File: ObjectHeaderTest.java    From jhdf with MIT License 5 votes vote down vote up
@BeforeEach
void setUp() throws IOException, URISyntaxException {
	final URI testFileUri = this.getClass().getResource("/hdf5/test_file.hdf5").toURI();
	fc = FileChannel.open(Paths.get(testFileUri), StandardOpenOption.READ);
	sb = Superblock.readSuperblock(fc, 0);
	hdfFc = new HdfFileChannel(fc, sb);
}
 
Example 13
Source File: DPColumn.java    From indexr with Apache License 2.0 5 votes vote down vote up
private void openWriteFiles() {
    try {
        OpenOption[] writeOptions = new StandardOpenOption[]{
                StandardOpenOption.WRITE,
                StandardOpenOption.CREATE};

        packFileWrite = FileChannel.open(packFilePath, writeOptions);
        dpnFileWrite = FileChannel.open(dpnFilePath, writeOptions);
        indexFileWrite = FileChannel.open(indexFilePath, writeOptions);
        extIndexFileWrite = FileChannel.open(extIndexFilePath, writeOptions);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 14
Source File: TestMaxCachedBufferSize.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void loop() {
    final String fileName = String.format("%s%d", FILE_NAME_PREFIX, id);

    try {
        for (int i = 0; i < iters; i += 1) {
            final int writeSize = getWriteSize();

            // This will allocate a HeapByteBuffer. It should not
            // be a direct buffer, otherwise the write() method on
            // the channel below will not create a temporary
            // direct buffer for the write.
            final ByteBuffer buffer = ByteBuffer.allocate(writeSize);

            // Put some random data on it.
            while (buffer.hasRemaining()) {
                buffer.put((byte) random.nextInt());
            }
            buffer.rewind();

            final Path file = Paths.get(fileName);
            try (FileChannel outChannel = FileChannel.open(file, CREATE, TRUNCATE_EXISTING, WRITE)) {
                // The write() method will create a temporary
                // direct buffer for the write and attempt to cache
                // it. It's important that buffer is not a
                // direct buffer, otherwise the temporary buffer
                // will not be created.
                long res = outChannel.write(buffer);
            }

            if ((i + 1) % VERBOSE_PERIOD == 0) {
                System.out.printf(
                  " Worker %3d | %8d Iters | Small %8d Large %8d | Direct %4d / %7dK\n",
                  id, i + 1, smallBufferCount, largeBufferCount,
                  directPool.getCount(), directPool.getTotalCapacity() / 1024);
            }
        }
    } catch (IOException e) {
        throw new Error("I/O error", e);
    }
}
 
Example 15
Source File: Basic.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Random rand = new Random();

    // allocate a few direct buffers
    int bufferCount = 5 + rand.nextInt(20);
    buffers = new ArrayList<ByteBuffer>(bufferCount);
    long totalCapacity = 0L;
    for (int i=0; i<bufferCount; i++) {
        int cap = 1024 + rand.nextInt(4096);
        buffers.add( ByteBuffer.allocateDirect(cap) );
        totalCapacity += cap;
    }

    // create a mapped buffer
    Path tmpfile = Files.createTempFile("blah", null);
    tmpfile.toFile().deleteOnExit();
    try (FileChannel fc = FileChannel.open(tmpfile, READ, WRITE)) {
        mbb = fc.map(FileChannel.MapMode.READ_WRITE, 10, 100);
        bufferCount++;
        totalCapacity += mbb.capacity();
    }

    // use platform MXBeans directly
    List<BufferPoolMXBean> pools =
        ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
    check(pools, bufferCount, totalCapacity);

    // use MBeanServer
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    Set<ObjectName> mbeans = server.queryNames(
        new ObjectName("java.nio:type=BufferPool,*"), null);
    pools = new ArrayList<BufferPoolMXBean>();
    for (ObjectName name: mbeans) {
        BufferPoolMXBean pool = ManagementFactory
            .newPlatformMXBeanProxy(server, name.toString(), BufferPoolMXBean.class);
        pools.add(pool);
    }
    check(pools, bufferCount, totalCapacity);

    // attempt to unmap mapped buffer
    WeakReference<MappedByteBuffer> ref = new WeakReference<>(mbb);
    mbb = null;
    do {
        System.gc();
        Thread.sleep(250);
    } while (ref.get() != null);
}
 
Example 16
Source File: FileBasedLock.java    From tus-java-server with MIT License 4 votes vote down vote up
protected FileChannel createFileChannel() throws IOException {
    return FileChannel.open(lockPath, CREATE, WRITE);
}
 
Example 17
Source File: PageCacheWarmer.java    From antsdb with GNU Lesser General Public License v3.0 4 votes vote down vote up
private FileChannel openFile(MinkeFile mfile) throws IOException {
    Path path = mfile.file.toPath();
    FileChannel ch = FileChannel.open(path, StandardOpenOption.READ);
    return ch;
}
 
Example 18
Source File: FileScrubberStep.java    From buck with Apache License 2.0 4 votes vote down vote up
private FileChannel readWriteChannel(Path path) throws IOException {
  return FileChannel.open(path, StandardOpenOption.READ, StandardOpenOption.WRITE);
}
 
Example 19
Source File: KeyValueContainerCheck.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
private static void verifyChecksum(BlockData block,
    ContainerProtos.ChunkInfo chunk, File chunkFile,
    ChunkLayOutVersion layout,
    DataTransferThrottler throttler, Canceler canceler) throws IOException {
  ChecksumData checksumData =
      ChecksumData.getFromProtoBuf(chunk.getChecksumData());
  int checksumCount = checksumData.getChecksums().size();
  int bytesPerChecksum = checksumData.getBytesPerChecksum();
  Checksum cal = new Checksum(checksumData.getChecksumType(),
      bytesPerChecksum);
  ByteBuffer buffer = ByteBuffer.allocate(bytesPerChecksum);
  long bytesRead = 0;
  try (FileChannel channel = FileChannel.open(chunkFile.toPath(),
      ChunkUtils.READ_OPTIONS, ChunkUtils.NO_ATTRIBUTES)) {
    if (layout == ChunkLayOutVersion.FILE_PER_BLOCK) {
      channel.position(chunk.getOffset());
    }
    for (int i = 0; i < checksumCount; i++) {
      // limit last read for FILE_PER_BLOCK, to avoid reading next chunk
      if (layout == ChunkLayOutVersion.FILE_PER_BLOCK &&
          i == checksumCount - 1 &&
          chunk.getLen() % bytesPerChecksum != 0) {
        buffer.limit((int) (chunk.getLen() % bytesPerChecksum));
      }

      int v = channel.read(buffer);
      if (v == -1) {
        break;
      }
      bytesRead += v;
      buffer.flip();

      throttler.throttle(v, canceler);

      ByteString expected = checksumData.getChecksums().get(i);
      ByteString actual = cal.computeChecksum(buffer)
          .getChecksums().get(0);
      if (!expected.equals(actual)) {
        throw new OzoneChecksumException(String
            .format("Inconsistent read for chunk=%s" +
                " checksum item %d" +
                " expected checksum %s" +
                " actual checksum %s" +
                " for block %s",
                ChunkInfo.getFromProtoBuf(chunk),
                i,
                Arrays.toString(expected.toByteArray()),
                Arrays.toString(actual.toByteArray()),
                block.getBlockID()));
      }

    }
    if (bytesRead != chunk.getLen()) {
      throw new OzoneChecksumException(String
          .format("Inconsistent read for chunk=%s expected length=%d"
                  + " actual length=%d for block %s",
              chunk.getChunkName(),
              chunk.getLen(), bytesRead, block.getBlockID()));
    }
  }
}
 
Example 20
Source File: FileLock.java    From embedded-cassandra with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new {@link FileLock} instance for the specified file. Note! This method creates a {@link FileChannel}
 * for the specified file and it must be closed in the end.
 *
 * @param file the file that should be locked
 * @return a new {@link FileLock}
 * @throws IOException in the case of I/O errors
 */
public static FileLock of(Path file) throws IOException {
	Objects.requireNonNull(file, "'file' must not be null");
	return new FileLock(FileChannel.open(FileUtils.createIfNotExists(file), StandardOpenOption.WRITE));
}