Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic knowledge points of streaming in Netty

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what are the basic knowledge points of mid-stream in Netty". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. The difference in meaning among IO, NIO and AIO

1. Synchronous blocking Istroke O (BIO block io)

Before jdk1.4, there was an io stream, and the server implementation mode is to connect one thread at a time, that is, when the client has a connection request, the server needs to start a thread to process it. If the connection does nothing, it will cause unnecessary thread overhead, which can be improved through the thread pool mechanism. BIO is suitable for architectures with a small number of connections and a fixed number of connections, which requires high server resources and concurrency is limited to applications.

Second, synchronous non-blocking Istroke O (NIO)

Java NIO (New IO) is a new IO API introduced since Java version 1.4, which can replace the standard Java IO API. NIO has the same function and purpose as the original IO, but in a completely different way. NIO supports buffer-oriented, channel-based IO operations. NIO will read and write documents in a more efficient way.

Third, asynchronous non-blocking IBG O (AIO)

Asynchronous non-blocking IO O, the server implementation mode is an effective request for a thread, the client request is completed by the operating system, and then informs the server to use its startup thread to process. AIO is suitable for architectures with a large number of connections and long connections (re-operation), such as photo album server, fully calling OS to participate in concurrent operations, programming is more complex, and jdk1.7 begins to support it.

IV. The difference between synchronous and asynchronous

Synchronization: one can only go to junior high school after finishing primary school, go to high school after junior high school, and finally go to college. Step by step, this is synchronization. Asynchronism: one can listen to music or play games at the same time, and the two movements can be carried out at the same time.

5. The difference between NIO and IO

2. The core of NIO

1. Definition

Client (client) and server (server), sending data client to server, or server to client, is equivalent to how people from Shanghai Railway Station are transported to Beijing Railway Station, or from Beijing Station to Shanghai Railway Station. Channel: railway tracks between Beijing and Shanghai buffer: trains (there can be multiple types of trains)

2.1 Buffer (buffer)

Responsible for data access in Java NIO, buffers are arrays, which are used to store data of different data types. According to different data types, buffers of response types are provided: ByteBuffer, CharBuffer, ShortBuffer, IntBuffer, LongBuffer, FloatBuffer, DoubleBuffer, and buffer is obtained by allocate ().

2.2 Channel (Channel)

Channel is similar to the stream of transmission, except that Channel itself can not directly access the data, Channel can only interact with Buffer, in short, Channel is responsible for transmission and Buffer is responsible for storage. 3. Path and Paths

1. Path

BooleanendsWith (Stringpath) determines whether to end booleanstartsWith with a path path (Stringpath) determines whether to start with a path path boolean isAbsolute () determines whether it is an absolute path PathgetFileName () returns the path name of the specified index location idx returned by the file name Path getName (int idx) associated with the calling Path object intgetNameCount () returns the number of elements behind the Path root directory PathgetParent () returns the Path object contains the entire path Does not contain the file path specified by the Path object PathgetRoot () returns the root path of the calling Path object Path resolve (Path p) resolves the relative path to the absolute path PathtoAbsolutePath () as the absolute path returns the string representation of the calling Path object by calling the Path object StringtoString ()

II. Paths

A tool class used by java.nio.file.Files to manipulate files or directories.

Method to describe Path get (String first, String... More) the get () method provided by Paths is used to get the Path object, which is used to concatenate multiple strings into a path Path copy (Path src, Path dest, CopyOption. How) file copy Path createDirectory (Path path, FileAttribute... Attr) create a directory Path createFile (Path path, FileAttribute... Arr) create a file void delete (Path path) delete a file Path move (Path src, Path dest, CopyOption...how) move the src to the dest location  long size (Path path): return the path specified file size boolean exists (Path path, LinkOption... Opts) to determine whether the file exists boolean isDirectory (Path path, LinkOption... Opts) determines whether it is a directory boolean isExecutable (Path path) determines whether it is an executable file boolean isHidden (Path path) determines whether it is a hidden file boolean isReadable (Path path) determines whether a file is readable boolean isWritable (Path path) determines whether a file is writable boolean notExists (Path path, LinkOption. Opts) determine whether the file does not exist

Third, Files operation content method

SeekableByteChannel newByteChannel (Path path, OpenOption...how)

Gets the connection to the specified file, and how specifies how to open it.

DirectoryStream newDirectoryStream (Path path) Open the directory specified by path InputStream newInputStream (Path path, OpenOption...how) to get InputStream objects OutputStream newOutputStream (Path path, OpenOption...how) get OutputStream objects 4, use IO read and write operations

1. Read

@ Testpublic void readeFile () {FileInputStream input = null; String result = ""; try {/ / 1. Instantiate an input stream object input = new FileInputStream ("/ Users/tentsuuhou/Desktop/ subtext .txt") according to path; / / 2. Returns an estimate of the remaining bytes bytes in this input stream that can be read; int size = input.available (); / / 3. Create a byte array byte [] array = new byte [size] from the bytes of the input stream; / / 4. Read the data into the byte array input.read (array); / / 5. Create a new string based on the obtained byte array, and then output result = new String (array); System.out.println (result);} catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();} finally {if (input! = null) {try {input.close () } catch (IOException e) {e.printStackTrace ();}

2. Write

@ Testpublic void writeFile () {FileOutputStream out = null; String content = "I love open source China"; try {/ / 1. Create the output stream object out = new FileOutputStream ("/ Users/tentsuuhou/Desktop/777.txt") according to the path; / / 2. Convert String strings into byte arrays; byte [] b = content.getBytes (); / / 3. Output the byte array out.write (b);} catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace ();} finally {if (out! = null) {try {out.close ();} catch (IOException e) {e.printStackTrace () This is the end of the content of "what are the basic knowledge points of streaming in Netty". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report