简体   繁体   English

是否有内置功能可从设定阅读器读取?

[英]Is there a built-in function to read from set reader?

Nashorn allows us to write to the writer which the engine uses via print() function. Nashorn允许我们通过print()函数写给引擎使用的writer。 Is there a way to read from the reader using a built-in function? 有没有一种使用内置功能从阅读器读取内容的方法?
Example: 例:

Socket input=new Socket("stackoverflow.com", 1234);  
Socket output=new Socket("stackoverflow.com", 4321);  
engine.getContext().setWriter(new InputStreamReader(input.getInputStream())); //Changing to Socket input's reader from default one, which should be System.in.  
engine.getContext().setWriter(new OutputStreamWriter(s.getOutputStream())); //Changing from System.out to Socket output's output stream.

In that case, if I use print("hello"), Socket output will receive the message. 在这种情况下,如果我使用print(“ hello”),则Socket输出将收到该消息。 But how can I read what Socket input sends? 但是,如何读取Socket输入发送的内容?

When you use setReader and setWriter on the ScriptContext, you are setting the default input and output streams for the Javascript context. 在ScriptContext上使用setReader和setWriter时,将设置Javascript上下文的默认输入和输出流。 So, what you are doing here is configuring Javascript to read from and write to socket streams. 因此,您在这里所做的是配置Javascript以读取和写入套接字流。

How those streams are made available to the Javascript code will depend on the JS environment / libraries you are using. 如何将这些流提供给Javascript代码将取决于您使用的JS环境/库。 However, with Node.js, you can see them as process.stdin and process.stdout . 但是,使用Node.js,您可以将它们看到为process.stdinprocess.stdout

FWIW, access to stdin / stdout is not supported in the standard ECMAScript libraries. FWIW,标准ECMAScript库中不支持对stdin / stdout的访问。

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

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