Java Code Examples for java.nio.channels.ByteChannel#read()
The following examples show how to use
java.nio.channels.ByteChannel#read() .
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: ChannelHelper.java From libcommon with Apache License 2.0 | 6 votes |
/** * ByteChannelからdouble配列を読み込む * @param channel * @return * @throws IOException */ public static double[] readDoubleArray(@NonNull final ByteChannel channel) throws IOException { final int n = readInt(channel); final ByteBuffer buf = ByteBuffer.allocate(n * 8).order(ByteOrder.BIG_ENDIAN); final int readBytes = channel.read(buf); if (readBytes != n * 8) throw new IOException(); buf.clear(); final DoubleBuffer result = buf.asDoubleBuffer(); if (result.hasArray()) { return result.array(); } else { final double[] b = new double[n]; result.get(b); return b; } }
Example 2
Source File: ChannelHelper.java From libcommon with Apache License 2.0 | 6 votes |
/** * ByteChannelからfloat配列を読み込む * @param channel * @return * @throws IOException */ public static float[] readFloatArray(@NonNull final ByteChannel channel) throws IOException { final int n = readInt(channel); final ByteBuffer buf = ByteBuffer.allocate(n * 4).order(ByteOrder.BIG_ENDIAN); final int readBytes = channel.read(buf); if (readBytes != n * 4) throw new IOException(); buf.clear(); final FloatBuffer result = buf.asFloatBuffer(); if (result.hasArray()) { return result.array(); } else { final float[] b = new float[n]; result.get(b); return b; } }
Example 3
Source File: ChannelHelper.java From libcommon with Apache License 2.0 | 6 votes |
/** * ByteChannelからint配列を読み込む * @param channel * @return * @throws IOException */ public static int[] readIntArray(@NonNull final ByteChannel channel) throws IOException { final int n = readInt(channel); final ByteBuffer buf = ByteBuffer.allocate(n * 4).order(ByteOrder.BIG_ENDIAN); final int readBytes = channel.read(buf); if (readBytes != n * 4) throw new IOException(); buf.clear(); final IntBuffer result = buf.asIntBuffer(); if (result.hasArray()) { return result.array(); } else { final int[] b = new int[n]; result.get(b); return b; } }
Example 4
Source File: FileReadingNioServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 6 votes |
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String indexHtmlPath = getServletContext().getRealPath("index.html"); File indexHtml = new File(indexHtmlPath); RandomAccessFile raf = new RandomAccessFile(indexHtml, "r"); ByteChannel channel = raf.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(8192); while (channel.read(buffer) != -1) { buffer.flip(); int len = buffer.remaining(); byte[] bytes = new byte[len]; buffer.get(bytes); buffer.flip(); response.getOutputStream().write(bytes, 0, len); } channel.close(); }
Example 5
Source File: ChannelHelper.java From libcommon with Apache License 2.0 | 5 votes |
/** * ByteChannelからStringを読み込む * @param channel * @return * @throws IOException */ public static String readString(@NonNull final ByteChannel channel) throws IOException { final int bytes = readInt(channel); final byte[] buf = new byte[bytes]; final ByteBuffer b = ByteBuffer.wrap(buf); final int readBytes = channel.read(b); if (readBytes != bytes) throw new IOException(); return new String(buf, CharsetsUtils.UTF8); }
Example 6
Source File: ChannelHelper.java From libcommon with Apache License 2.0 | 5 votes |
/** * ByteChannelからByteBufferを読み込む * @param channel * @param readBuf * @param canReAllocate trueなら 指定したreadBufのremainingが読み込むサイズよりも小さい場合は * IOExceptionを投げる、falseなら必要な分を確保し直す * @return 読み込めた時はpositionは元のまま(読んだデータの先頭), limitをデータの最後に変更して返す * canReAllocate=falseなら元のreadBufとは異なるByteBufferを返すかもしれない * IOExceptionを投げた時は内容・position,limitは不定 * @throws IOException */ public static ByteBuffer readByteBuffer(@NonNull final ByteChannel channel, @Nullable final ByteBuffer readBuf, final boolean canReAllocate) throws IOException { final int n = readInt(channel); final int pos = readBuf != null ? readBuf.position() : 0; ByteBuffer buf = readBuf; if ((buf == null) || (buf.remaining() < n)) { if (canReAllocate) { if (buf == null) { buf = ByteBuffer.allocateDirect(n); } else { final ByteBuffer temp = ByteBuffer.allocateDirect( readBuf.limit() + n); buf.flip(); temp.put(buf); buf = temp; } } else { throw new IOException(); } } buf.limit(pos + n); final int readBytes = channel.read(buf); if (readBytes != n) throw new IOException(); buf.position(pos); buf.limit(pos + n); return buf; }
Example 7
Source File: FileSystemHttpFile.java From armeria with Apache License 2.0 | 5 votes |
@Override protected int read(ByteChannel src, ByteBuf dst) throws IOException { if (src instanceof ScatteringByteChannel) { return dst.writeBytes((ScatteringByteChannel) src, dst.writableBytes()); } final int readBytes = src.read(dst.nioBuffer(dst.writerIndex(), dst.writableBytes())); if (readBytes > 0) { dst.writerIndex(dst.writerIndex() + readBytes); } return readBytes; }
Example 8
Source File: ChannelDataOutputTest.java From sis with Apache License 2.0 | 5 votes |
/** * May be invoked after {@link #initialize(String, int, int)} for replacing the seekable byte channel * by a non-seekable one. Used for testing different code paths in {@link ChannelDataOutput}. */ private void nonSeekableChannel() throws IOException { final ByteChannel channel = (ByteChannel) testedStream.channel; testedStream = new ChannelDataOutput(testedStream.filename, new ByteChannel() { @Override public boolean isOpen() {return channel.isOpen();} @Override public int read(ByteBuffer dst) throws IOException {return channel.read(dst);} @Override public int write(ByteBuffer src) throws IOException {return channel.write(src);} @Override public void close() throws IOException {channel.close();} }, testedStream.buffer); }
Example 9
Source File: ObjectSocketChannelStream.java From database with GNU General Public License v2.0 | 5 votes |
public static InputStream newInputStream(final ByteChannel channel) { final ByteBuffer buf = ByteBuffer.allocate(2048); return new InputStream() { public synchronized int read() throws IOException { if (!buf.hasRemaining()) { channel.read(buf); } return buf.get(); } public synchronized int read(byte[] bytes, int off, int len) throws IOException { int rem = buf.remaining(); while (rem < len) { if (rem > 0) { buf.get(bytes, off, rem); off += rem; len -= rem; } channel.read(buf); rem = buf.remaining(); } buf.get(bytes, off, len); return len; } }; }
Example 10
Source File: ChannelHelper.java From libcommon with Apache License 2.0 | 5 votes |
/** * ByteChannelからbyte配列を読み込む * @param channel * @return * @throws IOException */ public static byte[] readByteArray(@NonNull final ByteChannel channel) throws IOException { final int n = readInt(channel); final byte[] result = new byte[n]; final ByteBuffer buf = ByteBuffer.wrap(result); final int readBytes = channel.read(buf); if (readBytes != n) throw new IOException(); return result; }
Example 11
Source File: ChannelHelper.java From libcommon with Apache License 2.0 | 5 votes |
/** * ByteChannelからboolean配列を読み込む * @param channel * @return * @throws IOException */ public static boolean[] readBooleanArray(@NonNull final ByteChannel channel) throws IOException { final int n = readInt(channel); final ByteBuffer buf = ByteBuffer.allocate(n); final int readBytes = channel.read(buf); if (readBytes != n) throw new IOException(); buf.clear(); final boolean[] result = new boolean[n]; for (int i = 0; i < n; i++) { result[i] = buf.get() != 0; } return result; }
Example 12
Source File: ChannelHelper.java From libcommon with Apache License 2.0 | 5 votes |
/** * ByteChannelからdoubleを読み込む * @param channel * @return * @throws IOException */ public static double readDouble(@NonNull final ByteChannel channel, @Nullable final ByteBuffer work) throws IOException { final ByteBuffer buf = checkBuffer(work, 8); final int readBytes = channel.read(buf); if (readBytes != 8) throw new IOException(); buf.clear(); return buf.getDouble(); }
Example 13
Source File: ChannelHelper.java From libcommon with Apache License 2.0 | 5 votes |
/** * ByteChannelからfloatを読み込む * @param channel * @return * @throws IOException */ public static float readFloat(@NonNull final ByteChannel channel, @Nullable final ByteBuffer work) throws IOException { final ByteBuffer buf = checkBuffer(work, 4); final int readBytes = channel.read(buf); if (readBytes != 4) throw new IOException(); buf.clear(); return buf.getFloat(); }
Example 14
Source File: ChannelHelper.java From libcommon with Apache License 2.0 | 5 votes |
/** * ByteChannelからfloatを読み込む * @param channel * @return * @throws IOException */ public static float readFloat(@NonNull final ByteChannel channel) throws IOException { final ByteBuffer buf = ByteBuffer.allocate(4); final int readBytes = channel.read(buf); if (readBytes != 4) throw new IOException(); buf.clear(); return buf.getFloat(); }
Example 15
Source File: ChannelHelper.java From libcommon with Apache License 2.0 | 5 votes |
/** * ByteChannelからintを読み込む * @param channel * @param work * @return * @throws IOException */ public static int readInt(@NonNull final ByteChannel channel, @Nullable final ByteBuffer work) throws IOException { final ByteBuffer buf = checkBuffer(work, 4); final int readBytes = channel.read(buf); if (readBytes != 4) throw new IOException(); buf.clear(); return buf.getInt(); }
Example 16
Source File: ChannelHelper.java From libcommon with Apache License 2.0 | 5 votes |
/** * ByteChannelからshortを読み込む * @param channel * @param work * @return * @throws IOException */ public static short readShort(@NonNull final ByteChannel channel, @Nullable final ByteBuffer work) throws IOException { final ByteBuffer buf = checkBuffer(work, 2); final int readBytes = channel.read(buf); if (readBytes != 2) throw new IOException(); buf.clear(); return buf.getShort(); }
Example 17
Source File: ChannelHelper.java From libcommon with Apache License 2.0 | 5 votes |
/** * ByteChannelからcharを読み込む * @param channel * @param work * @return * @throws IOException */ public static char readChar(@NonNull final ByteChannel channel, @Nullable final ByteBuffer work) throws IOException { final ByteBuffer buf = checkBuffer(work, 2); final int readBytes = channel.read(buf); if (readBytes != 2) throw new IOException(); buf.clear(); return buf.getChar(); }
Example 18
Source File: ChannelHelper.java From libcommon with Apache License 2.0 | 5 votes |
/** * ByteChannelからbyeを読み込む * @param channel * @return * @throws IOException */ public static byte readByte(@NonNull final ByteChannel channel, @Nullable final ByteBuffer work) throws IOException { final ByteBuffer buf = checkBuffer(work, 1); final int readBytes = channel.read(buf); if (readBytes != 1) throw new IOException(); buf.clear(); return buf.get(); }
Example 19
Source File: ChannelHelper.java From libcommon with Apache License 2.0 | 5 votes |
/** * ByteChannelからbooleanを読み込む * @param channel * @param work * @return * @throws IOException */ public static boolean readBoolean( @NonNull final ByteChannel channel, @Nullable final ByteBuffer work) throws IOException { final ByteBuffer buf = checkBuffer(work, 1); final int readBytes = channel.read(buf); if (readBytes != 1) throw new IOException(); buf.clear(); return buf.get() != 0; }
Example 20
Source File: NioProcessor.java From neoscada with Eclipse Public License 1.0 | 4 votes |
@Override protected int read(NioSession session, IoBuffer buf) throws Exception { ByteChannel channel = session.getChannel(); return channel.read(buf.buf()); }