/** * Fills a buffer with random data and compare the result with a standard image input stream. * We allocate a small buffer for the {@code ChannelDataInput} in order to force frequent * interactions between the buffer and the channel. * * @throws IOException should never happen since we read and write in memory only. */ @Test public void testAllReadMethods() throws IOException { final byte[] array = createRandomArray(STREAM_LENGTH); referenceStream = new DataInputStream(new ByteArrayInputStream(array)); testedStream = new ChannelDataInput("testAllReadMethods", new DripByteChannel(array, random, 1, 1024), ByteBuffer.allocate(random.nextInt(BUFFER_MAX_CAPACITY) + Double.BYTES), false); transferRandomData(testedStream, array.length - ARRAY_MAX_LENGTH, 16); }
/** * Creates a new {@link ChannelDecoder} instance for the specified dataset. * The {@code file} parameter can be one of the following values: * * <ul> * <li>{@link TestData#NETCDF_2D_GEOGRAPHIC} — uses a geographic CRS for global data over the world.</li> * <li>{@link TestData#NETCDF_4D_PROJECTED} — uses a projected CRS with elevation and time.</li> * </ul> * * @param file the file as one of the above-cited constants. * @return the decoder for the specified dataset. * @throws IOException if an I/O error occurred while opening the file. * @throws DataStoreException if a logical error occurred. */ public static Decoder createChannelDecoder(final TestData file) throws IOException, DataStoreException { final InputStream in = file.open(); final ChannelDataInput input = new ChannelDataInput(file.name(), Channels.newChannel(in), ByteBuffer.allocate(4096), false); return new ChannelDecoder(input, null, GeometryLibrary.JAVA2D, LISTENERS); } }
/** * Creates a in-memory data input for the given buffer. */ MemoryDataTransfer(final String filename, final Buffer data) throws IOException { final ChannelDataInput input = new ChannelDataInput(filename, this, (data instanceof ByteBuffer) ? (ByteBuffer) data : null, true); if (data instanceof ByteBuffer) reader = input.new BytesReader ( null); else if (data instanceof ShortBuffer) reader = input.new ShortsReader ( (ShortBuffer) data); else if (data instanceof IntBuffer) reader = input.new IntsReader ( (IntBuffer) data); else if (data instanceof LongBuffer) reader = input.new LongsReader ( (LongBuffer) data); else if (data instanceof FloatBuffer) reader = input.new FloatsReader ( (FloatBuffer) data); else if (data instanceof DoubleBuffer) reader = input.new DoublesReader((DoubleBuffer) data); else if (data instanceof CharBuffer) reader = input.new CharsReader ( (CharBuffer) data); else throw new IllegalArgumentException(Errors.format(Errors.Keys.UnknownType_1, Classes.getClass(data))); }
/** * Creates a in-memory data input for the given buffer. */ MemoryDataTransfer(final String filename, final Buffer data) throws IOException { final ChannelDataInput input = new ChannelDataInput(filename, this, (data instanceof ByteBuffer) ? (ByteBuffer) data : null, true); if (data instanceof ByteBuffer) reader = input.new BytesReader ( null); else if (data instanceof ShortBuffer) reader = input.new ShortsReader ( (ShortBuffer) data); else if (data instanceof IntBuffer) reader = input.new IntsReader ( (IntBuffer) data); else if (data instanceof LongBuffer) reader = input.new LongsReader ( (LongBuffer) data); else if (data instanceof FloatBuffer) reader = input.new FloatsReader ( (FloatBuffer) data); else if (data instanceof DoubleBuffer) reader = input.new DoublesReader((DoubleBuffer) data); else if (data instanceof CharBuffer) reader = input.new CharsReader ( (CharBuffer) data); else throw new IllegalArgumentException(Errors.format(Errors.Keys.UnknownType_1, Classes.getClass(data))); }
/** * Tests the {@link ChannelDataInput#readString(int, Charset)} method. * * @throws IOException should never happen since we read and write in memory only. */ @Test public void testReadString() throws IOException { final String expected = "お元気ですか"; final byte[] array = expected.getBytes("UTF-8"); assertEquals(expected.length()*3, array.length); // Sanity check. final ChannelDataInput input = new ChannelDataInput("testReadString", new DripByteChannel(array, random, 1, 32), ByteBuffer.allocate(array.length + 4), false); assertEquals(expected, input.readString(array.length, StandardCharsets.UTF_8)); assertFalse(input.buffer.hasRemaining()); }
final ByteArrayChannel channel = new ByteArrayChannel(array, true); final ByteBuffer buffer = ByteBuffer.allocate(random.nextInt(20) + 20).order(ByteOrder.nativeOrder()); final ChannelDataInput input = new ChannelDataInput("HyperRectangle in channel", channel, buffer, false); reader = new HyperRectangleReader(Numbers.SHORT, input, origin); } else {
/** * Tests {@link ChannelDataInput#seek(long)} on a channel that do not implement * {@link java.nio.channels.SeekableByteChannel}. * * @throws IOException should never happen since we read and write in memory only. */ @Test public void testSeekOnForwardOnlyChannel() throws IOException { int length = random.nextInt(2048) + 1024; final byte[] array = createRandomArray(length); length -= Long.BYTES; // Safety against buffer underflow. final ByteBuffer buffer = ByteBuffer.wrap(array); final ChannelDataInput input = new ChannelDataInput("testSeekOnForwardOnlyChannel", new DripByteChannel(array, random, 1, 2048), ByteBuffer.allocate(random.nextInt(64) + 16), false); int position = 0; while (position < length) { input.seek(position); assertEquals("getStreamPosition()", position, input.getStreamPosition()); assertEquals(buffer.getLong(position), input.readLong()); position += random.nextInt(128); } }
final byte[] array = createRandomArray(length); final ByteBuffer buffer = ByteBuffer.allocate(random.nextInt(64) + 16); final ChannelDataInput input = new ChannelDataInput("testPrefetch", new DripByteChannel(array, random, 1, 64), buffer, false); int position = 0;
asDataInput = new ChannelImageInputStream(name, channel, buffer, false); } else { asDataInput = new ChannelDataInput(name, channel, buffer, false);
asDataInput = new ChannelImageInputStream(name, channel, buffer, false); } else { asDataInput = new ChannelDataInput(name, channel, buffer, false);