当我需要使用它两次时如何重新打开关闭的InputStream

发布于 2024-12-10 06:06:54 字数 711 浏览 0 评论 0原文

我目前正在使用 InpuStream 从我的服务器获取 JSON 响应。

我需要做两件事:

  1. 解析它并在屏幕上显示值
  2. 将此提要保存在 SDCard 文件上

这在一一使用这两种方法时完全没有问题。

解析是用 GSON 进行的:

Gson gson = new Gson();
Reader reader = new InputStreamReader (myInputStream);
Result result = gson.FrmJson(reader, Result.class)

复制到 SDCard 是用 进行的

FileOutputStream f (...) f.write (buffer)

两者都经过测试。

问题是一旦解析完成,我想写入 SDCard,但它中断了。 我知道我的 InputStream 已关闭,这就是问题所在。

这里有一些接近我的问题:How to Cache InputStream for Multiple Use

有没有办法改进该解决方案并提供我们可以使用的东西?

I am currently using an InpuStream to get a JSON response from my server.

I need to do 2 things with:

  1. Parsing it and displaying the values on the screen
  2. Save this feed on SDCard file

That gives me no issues at all when using these 2 methods one by one.

The parsing is made with GSON:

Gson gson = new Gson();
Reader reader = new InputStreamReader (myInputStream);
Result result = gson.FrmJson(reader, Result.class)

and the copy to SDCard is made with

FileOutputStream f (...) f.write (buffer)

Both of them have been tested.

TYhe problem is once the parsing is done, I want to write to SDCard and it breaks.
I understand that my InputStream is closed, and that's the issue.

There is something close to my question here: How to Cache InputStream for Multiple Use

Is there a way to improve that solution and provide something that we can use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漫雪独思 2024-12-17 06:06:54

我可能会使用 ByteArrayOutputStream 将输入流排入 byte[],然后每次需要时根据结果创建一个新的 ByteArrayInputStream重读流。

像这样的东西:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while ((n = myInputStream.read(buf)) >= 0)
    baos.write(buf, 0, n);
byte[] content = baos.toByteArray();

InputStream is1 = new ByteArrayInputStream(content);
... use is1 ...

InputStream is2 = new ByteArrayInputStream(content);
... use is2 ...

相关且可能有用的问题和答案:

I would probably drain the input stream into a byte[] using ByteArrayOutputStream and then create a new ByteArrayInputStream based on the result every time I need to reread the stream.

Something like this:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while ((n = myInputStream.read(buf)) >= 0)
    baos.write(buf, 0, n);
byte[] content = baos.toByteArray();

InputStream is1 = new ByteArrayInputStream(content);
... use is1 ...

InputStream is2 = new ByteArrayInputStream(content);
... use is2 ...

Related, and possibly useful, questions and answers:

半岛未凉 2024-12-17 06:06:54

或者,我发现了这个实现它的好方法:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class CopyInputStream
{
    private InputStream _is;
    private ByteArrayOutputStream _copy = new ByteArrayOutputStream();

    /**
     * 
     */
    public CopyInputStream(InputStream is)
    {
        _is = is;

        try
        {
            copy();
        }
        catch(IOException ex)
        {
            // do nothing
        }
    }

    private int copy() throws IOException
    {
        int read = 0;
        int chunk = 0;
        byte[] data = new byte[256];

        while(-1 != (chunk = _is.read(data)))
        {
            read += data.length;
            _copy.write(data, 0, chunk);
        }

        return read;
    }

    public InputStream getCopy()
    {
        return (InputStream)new ByteArrayInputStream(_copy.toByteArray());
    }
}

我用

CopyInputStream cis = new CopyInputStream(input);
InputStream input1 = cis.getCopy();

Alternatively, I found this great way to achieve it:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class CopyInputStream
{
    private InputStream _is;
    private ByteArrayOutputStream _copy = new ByteArrayOutputStream();

    /**
     * 
     */
    public CopyInputStream(InputStream is)
    {
        _is = is;

        try
        {
            copy();
        }
        catch(IOException ex)
        {
            // do nothing
        }
    }

    private int copy() throws IOException
    {
        int read = 0;
        int chunk = 0;
        byte[] data = new byte[256];

        while(-1 != (chunk = _is.read(data)))
        {
            read += data.length;
            _copy.write(data, 0, chunk);
        }

        return read;
    }

    public InputStream getCopy()
    {
        return (InputStream)new ByteArrayInputStream(_copy.toByteArray());
    }
}

And I call it with

CopyInputStream cis = new CopyInputStream(input);
InputStream input1 = cis.getCopy();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文