简体   繁体   English

InputStream.read(byte)方法如何工作?

[英]How could InputStream.read(byte) method ever work?

I have question about one method of InputStream class, because it doesn't seem to me it could ever work. 我对InputStream类的一种方法有疑问,因为在我看来,它永远无法正常工作。

Let we have something like this: 让我们有这样的事情:

InputStream is;
byte[] b = new byte[64];

is.read(b);
// and now the byte array b contains data comming through InputStream???

I would understand if usage of the .read() method would look something like this: 我会理解.read()方法的用法是否看起来像这样:

b = is.read();

Because the read method would be returning byte array. 因为read方法将返回字节数组。

But how can the real method write something to its argument and make it visible outside of itself? 但是,真正的方法如何在其参数中写入内容并使它在自身之外可见?

It's like I would have this: 就像我会这样:

String myString = "myText";

public void myMethod(String s) {
  s = "abc123";
}

myMethod(myString);
// and now is the content of myString equal to "abc123" instead of "myText" ???
// ANSWER: no!

Thanks for your replies. 多谢您的回覆。

Everything except primitives types are objects in java(including array). 除了基本类型以外的所有东西都是java中的对象(包括数组)。 The objects are passed by copy of reference from one method to another. 对象通过引用的副本从一种方法传递到另一种方法。 So if the called method makes changes to the object passed to it, method is eventually making changes to the same object which was passed to it. 因此,如果调用的方法对传递给它的对象进行了更改,则该方法最终将对传递给它的对象进行更改。 Hence, changes are reflected to the calling method as well. 因此,更改也会反映到调用方法中。

You need to learn about objects and how are they passed between method calls to understand this in detail. 您需要了解对象以及如何在方法调用之间传递对象,以详细了解此内容。 Please refer this link for better understanding. 请参考此链接以获得更好的理解。

Because the read method would be returning byte array. 因为read方法将返回字节数组。

Eh? Where did you read that? 你在哪里读的? InputStream 's .read() method returns an integer . InputStream.read()方法返回一个整数

how can the real method write something to its argument and make it visible outside of itself? 真正的方法如何在其参数中写入内容并使它在其外部可见?

Because you pass in a reference to an array where the .read(byte[]) will write . 因为您传递对.read(byte[])将写入的数组的引用 And the return value of this method is the number of bytes actually written to the byte array passed as an argument. 该方法的返回值是实际写入作为参数传递的字节数组的字节数。

This code works: 此代码有效:

public void writeOneToFirstElement(final int[] array)
{
    array[0] = 1;
}

final int[] foo = { 0 };
writeOneToFirstElement(foo);
System.out.println(foo[0]); // prints 1

THe Array is just a reference to the object and cause the adress where the data resides doesnt change on modifications it can work that way Explained here: http://www.javaworld.com/javaqa/2000-05/03-qa-0526-pass.html 数组仅是对对象的引用,并且导致数据所驻留的地址在修改时不会发生变化,因此可以按这种方式工作。此处解释: http : //www.javaworld.com/javaqa/2000-05/03-qa-0526 -pass.html
Edit: Typo 编辑:错别字

byte array as well as String are reference types ..When you pass them as argument there reference are copied and they all refer to the same object.. byte arrayString都是引用类型 ..当您将它们作为参数传递时,引用将被复制,并且它们都引用同一对象。


For example, a remote is like a reference to a TV .When you pass the remote to another person,he's still able to access the TV 例如, remote就像TV的参考。当您将遥控器传递给另一个人时,他仍然可以访问TV

The objects are passed by copy of reference from one method to another. 对象通过引用的副本从一种方法传递到另一种方法。 So if the called method makes changes to the object passed to it, method is eventually making changes to the same object which was passed to it. 因此,如果调用的方法对传递给它的对象进行了更改,则该方法最终将对传递给它的对象进行更改。 Hence, changes are reflected to the calling method as well. 因此,更改也会反映到调用方法中。 For example replace the String in the parameter with String[] and check the output. 例如替换String与所述参数String[]和检查输出。

public static void main(String[] args) 
{
 String[] myString =  {"myText"};
 myMethod(myString);
 System.out.println(myString[0]);
}

public void myMethod(String[] s) {
  s[0] = "abc123";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM