co.cask.tigon.io.Locations.java Source code

Java tutorial

Introduction

Here is the source code for co.cask.tigon.io.Locations.java

Source

/*
 * Copyright  2014 Cask Data, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package co.cask.tigon.io;

import com.google.common.io.InputSupplier;
import com.google.common.io.OutputSupplier;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.twill.filesystem.Location;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.util.Comparator;
import javax.annotation.Nullable;

/**
 * Utility class to help interaction with {@link org.apache.twill.filesystem.Location}.
 */
public final class Locations {

    public static final Comparator<Location> LOCATION_COMPARATOR = new Comparator<Location>() {
        @Override
        public int compare(Location o1, Location o2) {
            return o1.toURI().compareTo(o2.toURI());
        }
    };

    /**
     * Creates a new {@link com.google.common.io.InputSupplier} that can provides {@link SeekableInputStream} of
     * the given path.
     *
     * @param fs The {@link org.apache.hadoop.fs.FileSystem} for the given path.
     * @param path The path to create {@link co.cask.tigon.io.SeekableInputStream} when requested.
     * @return A {@link com.google.common.io.InputSupplier}.
     */
    public static InputSupplier<? extends SeekableInputStream> newInputSupplier(final FileSystem fs,
            final Path path) {
        return new InputSupplier<SeekableInputStream>() {
            @Override
            public SeekableInputStream getInput() throws IOException {
                return SeekableInputStream.create(fs.open(path));
            }
        };
    }

    /**
     * Creates a new {@link com.google.common.io.InputSupplier} that can provides {@link SeekableInputStream} from
     * the given location.
     *
     * @param location Location for the input stream.
     * @return A {@link com.google.common.io.InputSupplier}.
     */
    public static InputSupplier<? extends SeekableInputStream> newInputSupplier(final Location location) {
        return new InputSupplier<SeekableInputStream>() {
            @Override
            public SeekableInputStream getInput() throws IOException {
                return SeekableInputStream.create(location.getInputStream());
            }
        };
    }

    /**
     * Creates a new {@link com.google.common.io.OutputSupplier} that can provides {@link java.io.OutputStream} for
     * the given location.
     *
     * @param location Location for the output.
     * @return A {@link com.google.common.io.OutputSupplier}.
     */
    public static OutputSupplier<? extends OutputStream> newOutputSupplier(final Location location) {
        return new OutputSupplier<OutputStream>() {
            @Override
            public OutputStream getOutput() throws IOException {
                return location.getOutputStream();
            }
        };
    }

    /**
     * Creates a {@link org.apache.twill.filesystem.Location} instance which represents the parent of the given location.
     *
     * @param location location to extra parent from.
     * @return an instance representing the parent location or {@code null} if there is no parent.
     */
    @Nullable
    public static Location getParent(Location location) {
        URI source = location.toURI();

        // If it is root, return null
        if ("/".equals(source.getPath())) {
            return null;
        }

        URI resolvedParent = URI.create(source.toString() + "/..").normalize();
        return location.getLocationFactory().create(resolvedParent);
    }

    /**
     * Create the directory represented by the location if not exists.
     *
     * @param location the location for the directory.
     * @throws java.io.IOException If the location cannot be created
     */
    public static void mkdirsIfNotExists(Location location) throws IOException {
        // Need to check && mkdir && check to deal with race condition
        if (!location.isDirectory() && !location.mkdirs() && !location.isDirectory()) {
            throw new IOException("Failed to create directory at " + location.toURI());
        }
    }

    private Locations() {
    }
}