use of java.nio.channels.ReadableByteChannel in project hazelcast-simulator by hazelcast.
the class SimulatorInstaller method install.
void install() {
File simulatorHome = new File(FileUtils.getUserHomePath(), "hazelcast-simulator-" + version);
System.setProperty("SIMULATOR_HOME", simulatorHome.getAbsolutePath());
System.out.println("Installing Simulator: " + version);
File userHome = getUserHome();
try {
if (version.endsWith("SNAPSHOT")) {
File archive = new File(format("%s/.m2/repository/com/hazelcast/simulator/dist/%s/dist-%s-dist.tar.gz", userHome, version, version));
if (archive.exists()) {
simulatorHome.delete();
decompress(archive);
} else if (!simulatorHome.exists()) {
throw new IllegalStateException("Could not install simulator, archive: " + archive.getAbsolutePath() + " not found");
}
} else if (!simulatorHome.exists()) {
File archive = new File(getUserHome(), format("hazelcast-simulator-%s-dist.tar.gz", version));
URL url = new URL(format("http://repo1.maven.org/maven2/" + "com/hazelcast/simulator/dist/%s/dist-%s-dist.tar.gz", version, version));
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
System.out.printf("File [%s] doesn't exist; downloading\n", archive.getAbsolutePath());
FileOutputStream fos = new FileOutputStream(archive);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
decompress(archive);
archive.delete();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of java.nio.channels.ReadableByteChannel in project sis by apache.
the class StorageConnector method createChannelDataInput.
/**
* Creates a view for the input as a {@link ChannelDataInput} if possible.
* This is also a starting point for {@link #createDataInput()} and {@link #createByteBuffer()}.
* This method is one of the {@link #OPENERS} methods and should be invoked at most once per
* {@code StorageConnector} instance.
*
* @param asImageInputStream whether the {@code ChannelDataInput} needs to be {@link ChannelImageInputStream} subclass.
* @throws IOException if an error occurred while opening a channel for the input.
*/
private ChannelDataInput createChannelDataInput(final boolean asImageInputStream) throws IOException, DataStoreException {
/*
* Before to try to wrap an InputStream, mark its position so we can rewind if the user asks for
* the InputStream directly. We need to reset because ChannelDataInput may have read some bytes.
* Note that if mark is unsupported, the default InputStream.mark(…) implementation does nothing.
*/
reset();
if (storage instanceof InputStream) {
((InputStream) storage).mark(DEFAULT_BUFFER_SIZE);
}
/*
* Following method call recognizes ReadableByteChannel, InputStream (with special case for FileInputStream),
* URL, URI, File, Path or other types that may be added in future Apache SIS versions.
* If the given storage is already a ReadableByteChannel, then the factory will return it as-is.
*/
final ChannelFactory factory = ChannelFactory.prepare(storage, getOption(OptionKey.URL_ENCODING), false, getOption(OptionKey.OPEN_OPTIONS));
if (factory == null) {
return null;
}
/*
* ChannelDataInput depends on ReadableByteChannel, which itself depends on storage
* (potentially an InputStream). We need to remember this chain in 'Coupled' objects.
*/
final String name = getStorageName();
final ReadableByteChannel channel = factory.readable(name, null);
addView(ReadableByteChannel.class, channel, null, factory.isCoupled() ? CASCADE_ON_RESET : 0);
// User-supplied buffer.
ByteBuffer buffer = getOption(OptionKey.BYTE_BUFFER);
if (buffer == null) {
// Default buffer if user did not specified any.
buffer = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE);
}
final ChannelDataInput asDataInput;
if (asImageInputStream) {
asDataInput = new ChannelImageInputStream(name, channel, buffer, false);
} else {
asDataInput = new ChannelDataInput(name, channel, buffer, false);
}
addView(ChannelDataInput.class, asDataInput, ReadableByteChannel.class, CASCADE_ON_RESET);
/*
* Following is an undocumented mechanism for allowing some Apache SIS implementations of DataStore
* to re-open the same channel or input stream another time, typically for re-reading the same data.
*/
if (factory.canOpen()) {
addView(ChannelFactory.class, factory);
}
return asDataInput;
}
use of java.nio.channels.ReadableByteChannel in project sis by apache.
the class NTv2 method getOrLoad.
/**
* Returns the grid of the given name. This method returns the cached instance if it still exists,
* or load the grid otherwise.
*
* @param file name of the datum shift grid file to load.
*/
@SuppressWarnings("null")
static DatumShiftGridFile<Angle, Angle> getOrLoad(final Path file) throws FactoryException {
final Path resolved = DataDirectory.DATUM_CHANGES.resolve(file).toAbsolutePath();
DatumShiftGridFile<?, ?> grid = DatumShiftGridFile.CACHE.peek(resolved);
if (grid == null) {
final Cache.Handler<DatumShiftGridFile<?, ?>> handler = DatumShiftGridFile.CACHE.lock(resolved);
try {
grid = handler.peek();
if (grid == null) {
try (ReadableByteChannel in = Files.newByteChannel(resolved)) {
DatumShiftGridLoader.log(NTv2.class, file);
final Loader loader = new Loader(in, file);
grid = loader.readGrid();
loader.reportWarnings();
} catch (IOException | NoninvertibleTransformException | RuntimeException e) {
throw DatumShiftGridLoader.canNotLoad("NTv2", file, e);
}
grid = grid.useSharedData();
}
} finally {
handler.putAndUnlock(grid);
}
}
return grid.castTo(Angle.class, Angle.class);
}
use of java.nio.channels.ReadableByteChannel in project sis by apache.
the class StorageConnectorTest method testGetAsInputStream.
/**
* Tests the {@link StorageConnector#getStorageAs(Class)} method for the {@link InputStream} type.
* The {@code InputStream} was specified as a URL.
*
* @throws DataStoreException if an error occurred while using the storage connector.
* @throws IOException if an error occurred while reading the test file.
*/
@Test
@DependsOnMethod("testGetAsImageInputStream")
public void testGetAsInputStream() throws DataStoreException, IOException {
final StorageConnector connection = create(false);
final InputStream in = connection.getStorageAs(InputStream.class);
assertNotSame(connection.getStorage(), in);
assertSame("Expected cached value.", in, connection.getStorageAs(InputStream.class));
assertInstanceOf("Expected Channel backend.", InputStreamAdapter.class, in);
final ImageInputStream input = ((InputStreamAdapter) in).input;
assertInstanceOf("Expected Channel backend.", ChannelImageInputStream.class, input);
assertSame(input, connection.getStorageAs(DataInput.class));
assertSame(input, connection.getStorageAs(ImageInputStream.class));
final ReadableByteChannel channel = ((ChannelImageInputStream) input).channel;
assertTrue(channel.isOpen());
connection.closeAllExcept(null);
assertFalse(channel.isOpen());
}
use of java.nio.channels.ReadableByteChannel in project sis by apache.
the class StorageConnectorTest method testCloseAllExcept.
/**
* Tests the {@link StorageConnector#closeAllExcept(Object)} method.
*
* @throws DataStoreException if an error occurred while using the storage connector.
* @throws IOException if an error occurred while reading the test file.
*/
@Test
@DependsOnMethod("testGetAsDataInputFromStream")
public void testCloseAllExcept() throws DataStoreException, IOException {
final StorageConnector connection = create(true);
final DataInput input = connection.getStorageAs(DataInput.class);
final ReadableByteChannel channel = ((ChannelImageInputStream) input).channel;
assertTrue("channel.isOpen()", channel.isOpen());
connection.closeAllExcept(input);
assertTrue("channel.isOpen()", channel.isOpen());
channel.close();
}
Aggregations