从ServerRequest中提取InputStream

huangapple go评论91阅读模式
英文:

Extracting InputStream from ServerRequest

问题

我一直想从reactive.function.server.ServerRequest的主体中提取一个inputStream,但一直没有成功。到目前为止,我已经尝试了一些我在这里和其他地方找到的方法,但最终都遇到了相同的问题,即 ->

给定这段摘录:

从ServerRequest中提取InputStream

我在检查时得到了这个inputStream,显然是不正确的:

从ServerRequest中提取InputStream

从ServerRequest中提取InputStream

是否有任何想法,是否可以在我的情况下使用PipedInputStream或其他任何对象来实现这种转换?

英文:

I have been wanting to extract an inputStream from a reactive.function.server.ServerRequest body but to no avail. I have so far tried out some of the ways I found here and elsewhere and ended up with the same issue all over again which is that ->

Given this excerpt:

从ServerRequest中提取InputStream

I get this inputStream upon inspection, which is clearly not right:

从ServerRequest中提取InputStream

从ServerRequest中提取InputStream

Any ideas if it is actually possible to achieve such a conversion in my case using PipedInputStream or indeed any other object?

答案1

得分: 2

一个 DataBuffer 本身可以完成这项工作。它有一个 asInputStream 方法。

然而,你可能正在问错了问题:

几乎所有的 InputStream,特别是来自网络连接的 inputstream,本质上都是阻塞的概念,而你正在使用响应式编程,这意味着__如果你阻塞了,你的代码基本上是有问题的__。

换句话说,作为一个一般的经验法则,'我使用响应式!' 和 '我想要一个inputstream!' 是互斥的。你...不需要一个inputstream。

如果一定要,你想要一个有点奇特的独角兽:一个能保证不阻塞的InputStream。实际上只有一种方法可以创建这样的流,那就是将所有的数据都加载到内存中(希望它不是特别大的数据量!),然后创建一个只提供已经加载到内存中的字节的inputstream:这样的流不会阻塞。ByteArrayInputStream 就是这样一种流的示例。可以假设 DataBufferasInputStream 方法也是另一个示例。

英文:

a DataBuffer can itself do the job. It has an asInputStream method.

However, you may be asking the wrong question:

Almost all InputStreams, and certainly the inputstream from a network connection, fundamentally is a blocking concept, and you're using reactive, which means your code is fundamentally broken if you block.

In other words, as a general rule of thumb, 'I use reactive!' and 'I want an inputstream!' are mutually exclusive. You.. don't want an inputstream.

If you must, you want a bit of an exotic unicorn: An InputStream that guarantees that it doesn't block. There's really only one such way to create such a beast, which is to pump ALLLLL the data into memory (let's hope it's not a particularly large amount of data!) and then make an inputstream that just provides the bytes already loaded in memory: Such a stream does not block. ByteArrayInputStream is one example of such a stream. Presumably the asInputStream method of DataBuffer is another.

huangapple
  • 本文由 发表于 2020年8月13日 21:39:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63396474.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定