read first 100 bytes from SocketChannel - Java java.nio.channels

Java examples for java.nio.channels:SocketChannel

Description

read first 100 bytes from SocketChannel

Demo Code


//package com.java2s;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class Main {
    /**/*  w w w .j  a va  2s  . c o  m*/
     * read first 100 bytes
     * 
     * @param channel
     * @return
     * @throws IOException
     */
    public static String readTop100(SocketChannel channel)
            throws IOException {
        int capacity = 100;
        byte[] fileBytes = new byte[100];
        String text = "";
        ByteBuffer buffer = ByteBuffer.allocate(capacity);
        if (channel.read(buffer) > 0) {
            buffer.flip();
            buffer.get(fileBytes);
            buffer.clear();
        }
        text = new String(fileBytes).trim();
        return text;
    }
}

Related Tutorials