read file from socket channel - Java java.nio.channels

Java examples for java.nio.channels:SocketChannel

Description

read file from socket channel

Demo Code


//package com.java2s;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class Main {
    /**//from w w w.  j av a 2s  .c  o  m
     * read file from socket channel
     * 
     * @param channel
     * @param length
     * @return
     * @throws IOException
     */
    public static byte[] readFile(SocketChannel channel, Integer length)
            throws IOException {
        ByteBuffer dataBuffer = ByteBuffer.allocate(1024);
        int contentLength = 0;
        int size = -1;
        byte[] bytes = null;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        while ((size = channel.read(dataBuffer)) >= 0) {
            contentLength += size;
            dataBuffer.flip();
            bytes = new byte[size];
            dataBuffer.get(bytes);
            byteArrayOutputStream.write(bytes);
            dataBuffer.clear();
            if (contentLength >= length) {
                break;
            }
        }

        byte[] byteArray = byteArrayOutputStream.toByteArray();
        byteArrayOutputStream.close();
        return byteArray;
    }

    /**
     * send str
     * 
     * @param channel
     * @param str
     * @throws Exception
     */
    public static void write(SocketChannel channel, String str)
            throws Exception {
        channel.write(ByteBuffer.wrap(str.getBytes()));
    }

    /**
     * send str
     * 
     * @param channel
     * @param contents
     * @throws Exception
     */
    public static void write(SocketChannel channel, byte[] contents)
            throws Exception {
        channel.write(ByteBuffer.wrap(contents));
    }
}

Related Tutorials