Who holds a lot of empty dance "- see Java, C # big competition (1)

zhaozj2021-02-17  50

As one of the reasons why the programming language is so popular, Java is that it solves the common I / O, network operations in other languages, and the C # language has also adopted similar methods similar to Java, providing some libraries. Complete the operation of I / O and networks and hide its complex implementation methods. This article will focus on discussing the universal usage of some templates related to I / O, network operations in C # and some templates of these libraries. In order to facilitate the familiar programmakers who are familiar with Java to better grasp C #, the readers can compare the two languages. In this article, we also give the same function in Java implementation methods.

Understanding flow operation

The streams in Java and C # are common to read data from consoles, file systems, or networks or write data to these devices. In these two languages, if the program needs to move or operate the data of many bytes, it will be more Many intensive flow operations.

Java provides two abstract classes: java.io.inputstream and java.io.outputStream, including some methods that allow program read or write data. C # combines these two classes into one: system.io.stream, with a two objects used for read flow data, a stream object in C # needs to test the Canread, canwrite properties Testing it, write capabilities.

Synchronous I / O operation

Synchronous I / O operation is very similar in these two languages, both java.io.inputstream and java.io.outputstream and java.io.outputstream in Java, or the system.io.Stream in C # every time only one byte The method of operation operation also has a method of operating a batch of data. (C # lacks the ability to operate on a byte array, only the byte array can be operated by offset / length parameters.)

Table 1: Synchronous I / O operation in Java and C #

Java C #

Read a byte:

READ () method in java.io.inputstream

ReadByte () method in system.io.stream

Reading byte arrays:

Java.io.inputStream in Read (Byte [] b) method

There is no similar method, and can be implemented by using an offset / length parameter.

A part of the byte array:

READ (Byte [] B, INT OFF, INT LEN method in java.io.inputstream

Read (Byte [] Buffer, Int Offset, Int Length, System.io.Stream

Write a byte:

Write (INT B) method in java.io.outputstream

WritebyTe (Byte Value) method in System.io.Stream

Write a complete byte array:

Write in java.io.outputstream (Byte [] b) method

No special method ━━ Method for using required offset / length pair parameters

Write a part of a byte array:

Write in java.io.OutputStream (Byte [] B, INT OFF, INT LEN) method

Write in System.io.Stream (Byte [] Buffer, Int Offset, INT Length) method

Java programmers need to pay attention to: Don't forget ioException. Unlike Java, the C # compiler does not require processing exceptions when compiling.

Java lacks a formal approach to completing asynchronous I / O operations, it does not read, write, then check the results. One of the most "similar" analog is a java.lang.Thread thread when the synchronization is operated, so that the thread causes a side placement (after some instructions are taken out, it is temporarily not executed, placed on one side) or complete the inspection work. There is a built-in asynchronous I / O operation command in the C # library. For example, to perform an asynchronous READ (BYTE [] B) operation that can be checked in Java, the following is a possible implementation method:

File: / / Save the variable of the side placement of the read operation

Int Read; File: / / Save Read result

IOEXCEPTION EXCEPTION; / / Save possible exception

Object Wait ...

// Need to keep some of the values ​​before the end

File: // InputStream Variable IS Related to Read Packages

New Thread (New Runnable () {

Public void run () {

Try {

Read is.read ();

} catch (ioexception error) {

EXCEPTION ERROR;

} finally {

Synchronized (wait) {

File: // Wake Other Processes Waiting for this variable

// Execute a READ operation

Wait.notifyall ();

}

// Call check method

Callback ();

}

}

}))) .Start ();

This will cause the value of the READ or the abnormality of the read operation being stored in the READ and Exception, and the other process dependent on the variable Wait may continue to wait or complete the check.

C # provides two ways to encapsulate all of the above functions, the role of Beginread is similar to read, but it operates two or more variables, an AsyncCallback variable and a status object, return one can be slightly The IASYNCRESULT object used to check the asynchronous readout process. The usage of standard Beginread is similar to the following usage:

IASYNCRESULT IAR SBS.BEGINREAD (Buffer, 0, 1,

New asyncCallback (= Callback), NULL;

The usage with the Callback method is as follows:

Public Void Callback (IASYNCRESULT IAR)

To check how many bytes that actually read, you can call the endread method with the IASYNCRESULT object parameters. What needs to be remembered is that the call to the endread will be blocked before BeginRead execution. To check the status of the READ without blocking, you can check the ISCOMPLETED attribute of the returned IASYNCRESULT variable. It is also important to note that the content in the buffer is unreliable before the asynchronous READ is completed.

转载请注明原文地址:https://www.9cbs.com/read-29931.html

New Post(0)