Merlin's Magic: the input and output of Merlin's new I/O buffer

Source: Internet
Author: User
Tags reset

Java 2 Platform Standard Edition (Java 2 Platform Standard edition,j2se) 1.4 has made significant changes to the I/O processing capabilities of the Java platform. Not only does it continue to support streaming I/O operations on previous J2SE distributions with streaming links, but Merlin also adds new features-called New I/O classes (NIO)-that are now in the Java.nio package.

I/O performs input and output operations to transfer data from the file or system console to or from the application. (For additional information on Java I/O, see Resources).

Buffer base

The abstract buffer class is the basis of the Java.nio packet support buffer. The Buffer works like a randomaccessfile in memory for reading and writing basic data types. Like Randomaccessfile, using Buffer, the next operation (read/write) takes place at a current location. Either of these operations will change that position, so reading after the write operation does not read what you just wrote, but reads the data after what you just wrote. Buffer provides four indication methods for accessing the linear structure (from highest to lowest):

Capacity (): Indicates the size of the buffer

Limit (): Tell you how many bytes have been filled in the buffer so far, or let you use: limit (int newlimit) to change this limit

Position (): tells you the current location to perform the next read/write operation

Mark (): Remember a location to reset it later with reset ()

The basic operation of the buffer is get () and put (), however, these methods are specific to each data type in the child class. To illustrate this, let's look at a simple example that demonstrates reading and writing a character from the same buffer. In Listing 1, the flip () method swaps the limits and positions, then places the position to 0 and discards the tag so you can read the data that you just wrote:

Listing 1. Read/write examples

import java.nio.*;
...
CharBuffer buff = ...;
buff.put('A');
buff.flip();
char c = buff.get();
System.out.println("An A: " + c);

Now let's look at some specific Buffer subclasses.

Buffer type

Merlin has 7 specific types of Buffer, each of which corresponds to a basic data type (excluding Boolean):

Bytebuffer

Charbuffer

DoubleBuffer

Floatbuffer

Intbuffer

Longbuffer

Shortbuffer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.