3.9 Scatter and Gather

  • Scattering ** Refers to the “Scattering” of data read from a Channel into multiple buffers.

Note: Buffers are filled with data read from channels in Buffer order.

  • ** Gathering Writes (Gathering Writes) ** refers to “Gathering” data from multiple buffers into a Channel

Note: Write data between position and limit to Channel in buffer order.

    /** * scatter and gather */
    @Test
    public void test04(a) {
        RandomAccessFile raf1 = null;
        RandomAccessFile raf2 = null;
        FileChannel readChannel = null;
        FileChannel writeChannel = null;
        try {
            raf1 = new RandomAccessFile("1.txt"."rw");
            //1. Obtain the channel
            readChannel = raf1.getChannel();

            //2. Allocate a buffer of the specified size
            ByteBuffer bb1 = ByteBuffer.allocate(100);
            ByteBuffer bb2 = ByteBuffer.allocate(1024);

            //3. Scatter reads
            ByteBuffer[] bbs = {bb1, bb2};
            readChannel.read(bbs);

            for (ByteBuffer bb : bbs) {
                bb.flip();
            }
            System.out.println(new String(bbs[0].array(), 0, bbs[0].limit()));
            System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
            System.out.println(new String(bbs[1].array(), 0, bbs[1].limit()));

            //4. Aggregate write
            raf2 = new RandomAccessFile("2.txt"."rw");
            writeChannel = raf2.getChannel();
            writeChannel.write(bbs);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(raf1 ! =null) {
                try {
                    raf1.close();
                } catch(IOException e) { e.printStackTrace(); }}if(raf2 ! =null) {
                try {
                    raf2.close();
                } catch(IOException e) { e.printStackTrace(); }}if(readChannel ! =null) {
                try {
                    readChannel.close();
                } catch(IOException e) { e.printStackTrace(); }}if(writeChannel ! =null) {
                try {
                    writeChannel.close();
                } catch(IOException e) { e.printStackTrace(); }}}}/**
     * 字符集
     */
    @Test
    public void test05(a) {
        Charset cs1 = Charset.forName("GBK");
        // Get the encoder
        CharsetEncoder ce = cs1.newEncoder();
        // Get the decoder
        CharsetDecoder cd = cs1.newDecoder();

        CharBuffer cb = CharBuffer.allocate(1024);
        cb.put("Ha ha");
        cb.flip();
        try {
            / / code
            ByteBuffer bb = ce.encode(cb);

            for (int i = 0; i < cb.length(); i++) {
                System.out.println(bb.get());
            }

            / / decoding
            bb.flip();
            CharBuffer cb2 = cd.decode(bb);
            System.out.println(cb2.toString());

            Charset cs2 = Charset.forName("GBK");
            System.out.println("-- -- -- -- -- -- -- -- -- -- -- --");
            bb.flip();
            CharBuffer cBuf3 = cs2.decode(bb);
            System.out.println(cBuf3.toString());
        } catch(CharacterCodingException e) { e.printStackTrace(); }}Copy the code

3.10 Common Methods of FileChannel