Example usage for java.nio.channels FileChannel open

List of usage examples for java.nio.channels FileChannel open

Introduction

In this page you can find the example usage for java.nio.channels FileChannel open.

Prototype

public static FileChannel open(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs)
        throws IOException 

Source Link

Document

Opens or creates a file, returning a file channel to access the file.

Usage

From source file:io.druid.segment.writeout.TmpFileSegmentWriteOutMedium.java

@Override
public WriteOutBytes makeWriteOutBytes() throws IOException {
    File file = File.createTempFile("filePeon", null, dir);
    FileChannel ch = FileChannel.open(file.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE);
    closer.register(file::delete);/*from   ww  w  .jav  a2 s . c o  m*/
    closer.register(ch);
    return new FileWriteOutBytes(file, ch);
}

From source file:net.minecrell.serverlistplus.canary.SnakeYAML.java

@SneakyThrows
public static void load(Plugin plugin) {
    try { // Check if it is already loaded
        Class.forName("org.yaml.snakeyaml.Yaml");
        return;//from   w w w.  ja  v  a2 s.c o m
    } catch (ClassNotFoundException ignored) {
    }

    Path path = Paths.get("lib", SNAKE_YAML_JAR);

    if (Files.notExists(path)) {
        Files.createDirectories(path.getParent());

        plugin.getLogman().info("Downloading SnakeYAML...");

        URL url = new URL(SNAKE_YAML);
        MessageDigest sha1 = MessageDigest.getInstance("SHA-1");

        try (ReadableByteChannel source = Channels.newChannel(new DigestInputStream(url.openStream(), sha1));
                FileChannel out = FileChannel.open(path, StandardOpenOption.CREATE_NEW,
                        StandardOpenOption.WRITE)) {
            out.transferFrom(source, 0, Long.MAX_VALUE);
        }

        if (!new String(Hex.encodeHex(sha1.digest())).equals(EXPECTED_HASH)) {
            Files.delete(path);
            throw new IllegalStateException(
                    "Downloaded SnakeYAML, but checksum check failed. Please try again later.");
        }

        plugin.getLogman().info("Successfully downloaded!");
    }

    loadJAR(path);
}

From source file:cn.codepub.redis.directory.RedisLockFactory.java

@Override
public Lock obtainLock(@NonNull Directory dir, String lockName) throws IOException {
    if (!(dir instanceof RedisDirectory)) {
        throw new IllegalArgumentException("Expect argument of type [" + RedisDirectory.class.getName() + "]!");
    }//from  www  . j a va  2  s.c o m
    Path lockFile = lockFileDirectory.resolve(lockName);
    try {
        Files.createFile(lockFile);
        log.debug("Lock file path = {}", lockFile.toFile().getAbsolutePath());
    } catch (IOException ignore) {
        //ignore
        log.debug("Lock file already exists!");
    }
    final Path realPath = lockFile.toRealPath();
    final FileTime creationTime = Files.readAttributes(realPath, BasicFileAttributes.class).creationTime();
    if (LOCK_HELD.add(realPath.toString())) {
        FileChannel fileChannel = null;
        FileLock lock = null;
        try {
            fileChannel = FileChannel.open(realPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
            lock = fileChannel.tryLock();
            if (lock != null) {
                return new RedisLock(lock, fileChannel, realPath, creationTime);
            } else {
                throw new LockObtainFailedException("Lock held by another program: " + realPath);
            }
        } finally {
            if (lock == null) {
                IOUtils.closeQuietly(fileChannel);
                clearLockHeld(realPath);
            }
        }
    } else {
        throw new LockObtainFailedException("Lock held by this virtual machine: " + realPath);
    }
}

From source file:nl.jk_5.nailed.plugin.nmm.NmmMappack.java

@Override
public void prepareWorld(File destination, SettableFuture<Void> future) {
    HttpClient httpClient = HttpClientBuilder.create().build();
    try {//  w  w  w  .j  ava  2  s .  c  om
        String mappack = this.path.split("/", 2)[1];
        HttpGet request = new HttpGet("http://nmm.jk-5.nl/" + this.path + "/versions.json");
        HttpResponse response = httpClient.execute(request);
        MappackInfo list = NmmPlugin.gson.fromJson(EntityUtils.toString(response.getEntity(), "UTF-8"),
                MappackInfo.class);

        HttpGet request2 = new HttpGet(
                "http://nmm.jk-5.nl/" + this.path + "/" + mappack + "-" + list.latest + ".zip");
        HttpEntity response2 = httpClient.execute(request2).getEntity();
        if (response2 != null) {
            File mappackZip = new File(destination, "mappack.zip");
            try (ReadableByteChannel source = Channels.newChannel(response2.getContent());
                    FileChannel out = FileChannel.open(mappackZip.toPath(), StandardOpenOption.CREATE_NEW,
                            StandardOpenOption.WRITE)) {
                out.transferFrom(source, 0, Long.MAX_VALUE);
            }
            ZipUtils.extract(mappackZip, destination);
            mappackZip.delete();
            this.dir = destination;
            File dataDir = new File(destination, ".data");
            dataDir.mkdir();
            File metadataLocation = new File(dataDir, "game.xml");
            new File(destination, "game.xml").renameTo(metadataLocation);
            new File(destination, "scripts").renameTo(new File(dataDir, "scripts"));
            File worldsDir = new File(destination, "worlds");
            for (File f : worldsDir.listFiles()) {
                f.renameTo(new File(destination, f.getName()));
            }
            worldsDir.delete();
            //metadata = XmlMappackMetadata.fromFile(metadataLocation);
            future.set(null);
        } else {
            future.setException(new RuntimeException(
                    "Got an empty response while downloading mappack " + this.path + " from nmm.jk-5.nl"));
        }
    } catch (Exception e) {
        future.setException(
                new RuntimeException("Was not able to download mappack " + this.path + " from nmm.jk-5.nl", e));
    }
}

From source file:io.github.dsheirer.record.wave.WaveWriter.java

/**
 * Opens the file and writes a wave header.
 *///from   w  ww .  j  a  va  2 s .  c o m
private void open() throws IOException {
    int version = 2;

    while (Files.exists(mFile)) {
        mFile = Paths.get(mFile.toFile().getAbsolutePath().replace(".wav", "_" + version + ".wav"));
        version++;
    }

    mFileChannel = (FileChannel.open(mFile, StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW));

    ByteBuffer header = getWaveHeader(mAudioFormat);

    while (header.hasRemaining()) {
        mFileChannel.write(header);
    }
}

From source file:io.druid.indexing.worker.executor.ExecutorLifecycle.java

@LifecycleStart
public void start() throws InterruptedException {
    final File taskFile = Preconditions.checkNotNull(taskExecutorConfig.getTaskFile(), "taskFile");
    final File statusFile = Preconditions.checkNotNull(taskExecutorConfig.getStatusFile(), "statusFile");
    final InputStream parentStream = Preconditions.checkNotNull(taskExecutorConfig.getParentStream(),
            "parentStream");

    try {//  w  w w.  j a v a 2  s. c  o m
        task = jsonMapper.readValue(taskFile, Task.class);

        log.info("Running with task: %s", jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(task));
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }

    // Avoid running the same task twice on the same machine by locking the task base directory.

    final File taskLockFile = taskConfig.getTaskLockFile(task.getId());

    try {
        synchronized (this) {
            if (taskLockChannel == null && taskLockFileLock == null) {
                taskLockChannel = FileChannel.open(taskLockFile.toPath(), StandardOpenOption.CREATE,
                        StandardOpenOption.WRITE);

                log.info("Attempting to lock file[%s].", taskLockFile);
                final long startLocking = System.currentTimeMillis();
                final long timeout = DateTimes.utc(startLocking).plus(taskConfig.getDirectoryLockTimeout())
                        .getMillis();
                while (taskLockFileLock == null && System.currentTimeMillis() < timeout) {
                    taskLockFileLock = taskLockChannel.tryLock();
                    if (taskLockFileLock == null) {
                        Thread.sleep(100);
                    }
                }

                if (taskLockFileLock == null) {
                    throw new ISE("Could not acquire lock file[%s] within %,dms.", taskLockFile,
                            timeout - startLocking);
                } else {
                    log.info("Acquired lock file[%s] in %,dms.", taskLockFile,
                            System.currentTimeMillis() - startLocking);
                }
            } else {
                throw new ISE("Already started!");
            }
        }
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }

    if (taskExecutorConfig.isParentStreamDefined()) {
        // Spawn monitor thread to keep a watch on parent's stdin
        // If stdin reaches eof, the parent is gone, and we should shut down
        parentMonitorExec.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    while (parentStream.read() != -1) {
                        // Toss the byte
                    }
                } catch (Exception e) {
                    log.error(e, "Failed to read from stdin");
                }

                // Kind of gross, but best way to kill the JVM as far as I know
                log.info("Triggering JVM shutdown.");
                System.exit(2);
            }
        });
    }

    // Won't hurt in remote mode, and is required for setting up locks in local mode:
    try {
        if (!task.isReady(taskActionClientFactory.create(task))) {
            throw new ISE("Task[%s] is not ready to run yet!", task.getId());
        }
    } catch (Exception e) {
        throw new ISE(e, "Failed to run task[%s] isReady", task.getId());
    }

    statusFuture = Futures.transform(taskRunner.run(task), new Function<TaskStatus, TaskStatus>() {
        @Override
        public TaskStatus apply(TaskStatus taskStatus) {
            try {
                log.info("Task completed with status: %s",
                        jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(taskStatus));

                final File statusFileParent = statusFile.getParentFile();
                if (statusFileParent != null) {
                    FileUtils.forceMkdir(statusFileParent);
                }
                jsonMapper.writeValue(statusFile, taskStatus);

                return taskStatus;
            } catch (Exception e) {
                throw Throwables.propagate(e);
            }
        }
    });
}

From source file:com.spotify.helios.agent.AgentService.java

/**
 * Create a new agent instance./*from  w w  w  .  ja v  a  2s  .co m*/
 *
 * @param config The service configuration.
 * @param environment The DropWizard environment.
 * @throws ConfigurationException If an error occurs with the DropWizard configuration.
 * @throws InterruptedException If the thread is interrupted.
 * @throws IOException IOException
 */
public AgentService(final AgentConfig config, final Environment environment)
        throws ConfigurationException, InterruptedException, IOException {
    // Create state directory, if necessary
    final Path stateDirectory = config.getStateDirectory().toAbsolutePath().normalize();
    if (!Files.exists(stateDirectory)) {
        try {
            Files.createDirectories(stateDirectory);
        } catch (IOException e) {
            log.error("Failed to create state directory: {}", stateDirectory, e);
            throw Throwables.propagate(e);
        }
    }

    // Take a file lock in the state directory to ensure this is the only agent using it
    final Path lockPath = config.getStateDirectory().resolve("lock");
    try {
        stateLockFile = FileChannel.open(lockPath, CREATE, WRITE);
        stateLock = stateLockFile.tryLock();
        if (stateLock == null) {
            throw new IllegalStateException("State lock file already locked: " + lockPath);
        }
    } catch (OverlappingFileLockException e) {
        throw new IllegalStateException("State lock file already locked: " + lockPath);
    } catch (IOException e) {
        log.error("Failed to take state lock: {}", lockPath, e);
        throw Throwables.propagate(e);
    }

    final Path idPath = config.getStateDirectory().resolve("id");
    final String id;
    try {
        if (Files.exists(idPath)) {
            id = new String(Files.readAllBytes(idPath), UTF_8);
        } else {
            id = config.getId();
            Files.write(idPath, id.getBytes(UTF_8));
        }
    } catch (IOException e) {
        log.error("Failed to set up id file: {}", idPath, e);
        throw Throwables.propagate(e);
    }

    // Configure metrics
    final MetricRegistry metricsRegistry = environment.metrics();
    final RiemannSupport riemannSupport = new RiemannSupport(metricsRegistry, config.getRiemannHostPort(),
            config.getName(), "helios-agent");
    final RiemannFacade riemannFacade = riemannSupport.getFacade();
    if (config.isInhibitMetrics()) {
        log.info("Not starting metrics");
        metrics = new NoopMetrics();
    } else {
        log.info("Starting metrics");
        metrics = new MetricsImpl(metricsRegistry, MetricsImpl.Type.AGENT);
        environment.lifecycle().manage(riemannSupport);
        if (!Strings.isNullOrEmpty(config.getStatsdHostPort())) {
            environment.lifecycle()
                    .manage(new ManagedStatsdReporter(config.getStatsdHostPort(), metricsRegistry));
        }

        final FastForwardConfig ffwdConfig = config.getFfwdConfig();
        if (ffwdConfig != null) {
            environment.lifecycle().manage(FastForwardReporter.create(metricsRegistry, ffwdConfig.getAddress(),
                    ffwdConfig.getMetricKey(), ffwdConfig.getReportingIntervalSeconds()));
        }
    }

    // This CountDownLatch will signal EnvironmentVariableReporter and LabelReporter when to report
    // data to ZK. They only report once and then stop, so we need to tell them when to start
    // reporting otherwise they'll race with ZooKeeperRegistrarService and might have their data
    // erased if they are too fast.
    final CountDownLatch zkRegistrationSignal = new CountDownLatch(1);

    this.zooKeeperClient = setupZookeeperClient(config, id, zkRegistrationSignal);
    final DockerHealthChecker dockerHealthChecker = new DockerHealthChecker(metrics.getSupervisorMetrics(),
            TimeUnit.SECONDS, 30, riemannFacade);
    environment.lifecycle().manage(dockerHealthChecker);
    environment.lifecycle().manage(new RiemannHeartBeat(TimeUnit.MINUTES, 2, riemannFacade));

    // Set up model
    final ZooKeeperModelReporter modelReporter = new ZooKeeperModelReporter(riemannFacade,
            metrics.getZooKeeperMetrics());
    final ZooKeeperClientProvider zkClientProvider = new ZooKeeperClientProvider(zooKeeperClient,
            modelReporter);
    final KafkaClientProvider kafkaClientProvider = new KafkaClientProvider(config.getKafkaBrokers());

    final TaskHistoryWriter historyWriter;
    if (config.isJobHistoryDisabled()) {
        historyWriter = null;
    } else {
        historyWriter = new TaskHistoryWriter(config.getName(), zooKeeperClient,
                stateDirectory.resolve(TASK_HISTORY_FILENAME));
    }

    try {
        this.model = new ZooKeeperAgentModel(zkClientProvider, kafkaClientProvider, config.getName(),
                stateDirectory, historyWriter);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }

    // Set up service registrar
    this.serviceRegistrar = createServiceRegistrar(config.getServiceRegistrarPlugin(),
            config.getServiceRegistryAddress(), config.getDomain());

    final ZooKeeperNodeUpdaterFactory nodeUpdaterFactory = new ZooKeeperNodeUpdaterFactory(zooKeeperClient);

    final DockerClient dockerClient;
    if (isNullOrEmpty(config.getDockerHost().dockerCertPath())) {
        dockerClient = new PollingDockerClient(config.getDockerHost().uri());
    } else {
        final Path dockerCertPath = java.nio.file.Paths.get(config.getDockerHost().dockerCertPath());
        final DockerCertificates dockerCertificates;
        try {
            dockerCertificates = new DockerCertificates(dockerCertPath);
        } catch (DockerCertificateException e) {
            throw Throwables.propagate(e);
        }

        dockerClient = new PollingDockerClient(config.getDockerHost().uri(), dockerCertificates);
    }

    final DockerClient monitoredDockerClient = MonitoredDockerClient.wrap(riemannFacade, dockerClient);

    this.hostInfoReporter = new HostInfoReporter((OperatingSystemMXBean) getOperatingSystemMXBean(),
            nodeUpdaterFactory, config.getName(), dockerClient, config.getDockerHost(), 1, TimeUnit.MINUTES,
            zkRegistrationSignal);

    this.agentInfoReporter = new AgentInfoReporter(getRuntimeMXBean(), nodeUpdaterFactory, config.getName(), 1,
            TimeUnit.MINUTES, zkRegistrationSignal);

    this.environmentVariableReporter = new EnvironmentVariableReporter(config.getName(), config.getEnvVars(),
            nodeUpdaterFactory, zkRegistrationSignal);

    this.labelReporter = new LabelReporter(config.getName(), config.getLabels(), nodeUpdaterFactory,
            zkRegistrationSignal);

    final String namespace = "helios-" + id;

    final List<ContainerDecorator> decorators = Lists.newArrayList();

    if (!isNullOrEmpty(config.getRedirectToSyslog())) {
        decorators.add(new SyslogRedirectingContainerDecorator(config.getRedirectToSyslog()));
    }

    if (!config.getBinds().isEmpty()) {
        decorators.add(new BindVolumeContainerDecorator(config.getBinds()));
    }

    if (!config.getExtraHosts().isEmpty()) {
        decorators.add(new AddExtraHostContainerDecorator(config.getExtraHosts()));
    }

    final SupervisorFactory supervisorFactory = new SupervisorFactory(model, monitoredDockerClient,
            config.getEnvVars(), serviceRegistrar, decorators, config.getDockerHost(), config.getName(),
            metrics.getSupervisorMetrics(), namespace, config.getDomain(), config.getDns());

    final ReactorFactory reactorFactory = new ReactorFactory();

    final PortAllocator portAllocator = new PortAllocator(config.getPortRangeStart(), config.getPortRangeEnd());

    final PersistentAtomicReference<Map<JobId, Execution>> executions;
    try {
        executions = PersistentAtomicReference.create(stateDirectory.resolve("executions.json"),
                JOBID_EXECUTIONS_MAP, Suppliers.ofInstance(EMPTY_EXECUTIONS));
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }

    final Reaper reaper = new Reaper(dockerClient, namespace);
    this.agent = new Agent(model, supervisorFactory, reactorFactory, executions, portAllocator, reaper);

    final ZooKeeperHealthChecker zkHealthChecker = new ZooKeeperHealthChecker(zooKeeperClient,
            Paths.statusHosts(), riemannFacade, TimeUnit.MINUTES, 2);
    environment.lifecycle().manage(zkHealthChecker);

    if (!config.getNoHttp()) {

        environment.healthChecks().register("docker", dockerHealthChecker);
        environment.healthChecks().register("zookeeper", zkHealthChecker);

        // Report health checks as a gauge metric
        environment.healthChecks().getNames().forEach(name -> environment.metrics()
                .register("helios." + name + ".ok", new HealthCheckGauge(environment.healthChecks(), name)));

        environment.jersey().register(new AgentModelTaskResource(model));
        environment.jersey().register(new AgentModelTaskStatusResource(model));
        environment.lifecycle().manage(this);

        this.server = ServiceUtil
                .createServerFactory(config.getHttpEndpoint(), config.getAdminEndpoint(), config.getNoHttp())
                .build(environment);
    } else {
        this.server = null;
    }
    environment.lifecycle().manage(this);
}

From source file:com.google.cloud.dataflow.sdk.io.TextIOTest.java

License:asdf

private GcsUtil buildMockGcsUtil() throws IOException {
    GcsUtil mockGcsUtil = Mockito.mock(GcsUtil.class);

    // Any request to open gets a new bogus channel
    Mockito.when(mockGcsUtil.open(Mockito.any(GcsPath.class))).then(new Answer<SeekableByteChannel>() {
        @Override//from   ww  w.  j  a  v  a  2 s  .com
        public SeekableByteChannel answer(InvocationOnMock invocation) throws Throwable {
            return FileChannel.open(Files.createTempFile("channel-", ".tmp"), StandardOpenOption.CREATE,
                    StandardOpenOption.DELETE_ON_CLOSE);
        }
    });

    // Any request for expansion returns a list containing the original GcsPath
    // This is required to pass validation that occurs in TextIO during apply()
    Mockito.when(mockGcsUtil.expand(Mockito.any(GcsPath.class))).then(new Answer<List<GcsPath>>() {
        @Override
        public List<GcsPath> answer(InvocationOnMock invocation) throws Throwable {
            return ImmutableList.of((GcsPath) invocation.getArguments()[0]);
        }
    });

    return mockGcsUtil;
}

From source file:org.eclipse.jgit.lfs.server.fs.LfsServerTest.java

protected long getContent(String hexId, Path f) throws IOException {
    try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
        HttpGet request = new HttpGet(server.getURI() + "/lfs/objects/" + hexId);
        HttpResponse response = client.execute(request);
        checkResponseStatus(response);//  w  w w.j av a 2s.  co m
        HttpEntity entity = response.getEntity();
        long pos = 0;
        try (InputStream in = entity.getContent();
                ReadableByteChannel inChannel = Channels.newChannel(in);
                FileChannel outChannel = FileChannel.open(f, StandardOpenOption.CREATE_NEW,
                        StandardOpenOption.WRITE)) {
            long transferred;
            do {
                transferred = outChannel.transferFrom(inChannel, pos, MiB);
                pos += transferred;
            } while (transferred > 0);
        }
        return pos;
    }
}

From source file:org.eclipse.jgit.lfs.server.fs.LfsServerTest.java

/**
 * Creates a file with random content, repeatedly writing a random string of
 * 4k length to the file until the file has at least the specified length.
 *
 * @param f/*w w w  .  j  a  va 2s  .c o  m*/
 *            file to fill
 * @param size
 *            size of the file to generate
 * @return length of the generated file in bytes
 * @throws IOException
 */
protected long createPseudoRandomContentFile(Path f, long size) throws IOException {
    SecureRandom rnd = new SecureRandom();
    byte[] buf = new byte[4096];
    rnd.nextBytes(buf);
    ByteBuffer bytebuf = ByteBuffer.wrap(buf);
    try (FileChannel outChannel = FileChannel.open(f, StandardOpenOption.CREATE_NEW,
            StandardOpenOption.WRITE)) {
        long len = 0;
        do {
            len += outChannel.write(bytebuf);
            if (bytebuf.position() == 4096) {
                bytebuf.rewind();
            }
        } while (len < size);
    }
    return Files.size(f);
}