标签: inputstream

使用标准Java库获取Writer的InputStream

我有一个将数据写入 OutputStream 但需要将 OutputStream 的内容作为 InputStream 返回的方法

public InputStream getInputStreamOfData(type param) {
    // ..... data 
    OutputStreamWriter writer = new OutputStreamWriter();
    writer.write(data);
    // convert writer object to an InputStream 
}
Run Code Online (Sandbox Code Playgroud)

我遇到了一些库来执行此操作,例如 IOUtils 和其他基于线程的方法。有没有一种简单的方法可以使用标准 Java 库来实现这一点?我想将编写器中的内容作为 InputStream 返回以供调用方法使用。

谢谢!

java inputstream outputstream stream

3
推荐指数
1
解决办法
9736
查看次数

将 InputStream 与 MultipartEntityBuilder 结合使用:apache 错误

我正在使用 MultipartEntityBuilder,我想在服务器上发送图像。我有图像 Uri。图像可能是本地的,也可能不是本地的,所以我获取输入流并以这种方式发送:

HttpClient httpclient = new DefaultHttpClient();
JSONObject result;
HttpPost httppost = new HttpPost("http://www.ezduki.ru/api/content/add/image/");
InputStream iStream = con.getContentResolver().openInputStream(imageUri);
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addTextBody("token", code);
multipartEntity.addBinaryBody("file", iStream);
httppost.setEntity(multipartEntity.build());
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
result = new JSONObject(EntityUtils.toString(entity));
Run Code Online (Sandbox Code Playgroud)

其中 con 是我的主要活动上下文(代码在 AsyncTask 中运行)。我正在尝试发送本地文件,结果从网络服务器收到错误,这是来自网络服务器的日志:

[2013 年 12 月 13 日星期五 10:01:03] [错误] [客户端 93.157.241.232] (70014)找到文件结尾:mod_wsgi (pid=28449):无法获取 Bucket brigade 的请求。[2013 年 12 月 13 日星期五 15:01:03] [错误] 错误:django.request:内部服务器错误:/api/content/add/image/ [2013 年 12 月 13 日星期五 15:01:03] [错误] 回溯(大多数最近的通话最后一次):[Fri Dec 13 …

android http inputstream

3
推荐指数
1
解决办法
9417
查看次数

Java 8 - 将 List<byte[]> 合并到 byte[] 的最有效方法

我有一个库,它返回一些二进制数据作为二进制数组列表。这些 byte[] 需要合并到一个 InputStream 中。

这是我当前的实现:

public static InputStream foo(List<byte[]> binary) {
    byte[] streamArray = null;
    binary.forEach(bin -> {
        org.apache.commons.lang.ArrayUtils.addAll(streamArray, bin);
    });
    return new ByteArrayInputStream(streamArray);
}
Run Code Online (Sandbox Code Playgroud)

但这对CPU来说是相当密集的。有没有更好的办法?

感谢所有的答案。我做了一个性能测试。这些是我的结果:

  • 函数:'NicolasFilotto' => 100 次调用平均耗时 68,04 毫秒
  • 函数:'NicolasFilottoEstSize' => 100 次调用平均 65,24 毫秒
  • 函数:'NicolasFilottoSequenceInputStream' => 100 次调用平均耗时 63,09 毫秒
  • 函数:'Saka1029_1' => 100 次调用平均 63,06 毫秒
  • 函数:'Saka1029_2' => 100 次调用的平均时间为 0.79 毫秒
  • 函数:'Coco' => 10 次调用平均 541,60 毫秒

我不确定“Saka1029_2”是否测量正确......

这是执行函数:

private static double execute(Callable<InputStream> funct, int times) throws Exception {
    List<Long> executions = new …
Run Code Online (Sandbox Code Playgroud)

java performance inputstream stream

3
推荐指数
1
解决办法
3672
查看次数

无法将文件流作为函数参数传递?

这是我的作业代码。每当我尝试编译时,由于“ios_base.h”中的某些内容,我的读取函数出现错误,我不确定该怎么做和/或我的代码是否执行了获取文件并将其元素移动到单独的文件中的预期功能名称和平均值相邻的文件。

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>

using namespace std;

struct Student
{
    string fname;
    string lname;
    double average;
};

int read(ifstream, Student s[]);

void print(ofstream fout, Student s[], int amount);


int main()
{
    const int size = 10;
    ifstream fin;
    ofstream fout;
    string inputFile;
    string outputFile;
    Student s[size];

    cout << "Enter input filename: ";
    cin >> inputFile;
    cout << "Enter output filename: ";
    cin >> outputFile;
    cout << endl;

    fin.open(inputFile.c_str());
    fout.open(outputFile.c_str());

    read(fin , s);
    print(fout, s, read(fin, …
Run Code Online (Sandbox Code Playgroud)

c++ string inputstream file filestream

3
推荐指数
1
解决办法
2512
查看次数

使用 Vert.x 逐行读取文件(AsyncFile 和 RecordParser 帮助)

我正在尝试使用 Vert.x 从文件系统读取一个大文件并逐行处理它。从核心文档来看,我认为做到这一点的方法是通过 anAsyncFile和 a RecordParser。理想情况下,我想要Pump数据(以避免背压),但RecordParser不是WriteStream

AsyncFile asyncFile = vertx.fileSystem().openBlocking(/*path and options*/);

RecordParser recordParser = RecordParser.newDelimited("\n", bufferedLine -> {
  // Do something per line
});

Pump.pump(asyncFile, recordParser).start(); // Error - RecordParser cannot be converted to WriteStream
Run Code Online (Sandbox Code Playgroud)

所以我想我必须自己抽奶?我尝试过类似的东西:

RecordParser recordParser = RecordParser.newDelimited("\n", bufferedLine -> {
  // Do something per line
  // I can see this code get run
})
.exceptionHandler(cause -> {
  // Do I need this? What are the repercussions if …
Run Code Online (Sandbox Code Playgroud)

asynchronous inputstream vert.x

3
推荐指数
1
解决办法
3619
查看次数

Files.readAllBytes() 读取文件后是否关闭输入流?

这个java方法在读取文件后是否关闭输入流?

Files.readAllBytes(Paths.get("文件"))

java file-io inputstream

3
推荐指数
1
解决办法
4145
查看次数

如何在控制器之前重写SpringBoot中的InputStream?

在 Spring Boot 到达控制器之前,如何覆盖 @RequestBody 内容?

  1. 我知道有WebMvcConfigurerHandlerInterceptorAdapter类在控制器之前处理请求。

  2. 我也用谷歌搜索了RequestBodyAdviceAdapter

有几个链接不适用于 Spring Boot。

如何多次读取 request.getInputStream()

如何在 Spring Boot 中到达控制器之前修改请求正文

现在我可以将输入流读入字符串,进行一些修改并设置回控制器的输入流吗?

inputstream spring-boot

3
推荐指数
1
解决办法
2514
查看次数

尝试以 pdf 形式查看数据,pdf 为空白

我试图在下一个选项卡中打开 pdf 文件,它打开但始终为空白。我正在从 springboot 中的文件夹中调用 pdf 文件。数据确实显示在控制台日志中。

弹簧代码:

 @RequestMapping(value = "/report", method = RequestMethod.GET)
    void getFile(HttpServletResponse response) throws IOException {

        String fileName = "test123.pdf";
        String path = "TrainingDocuments/SuperPartnerUser/" + fileName;

        File file = new File(path);
        FileInputStream inputStream = new FileInputStream(file);

        response.setContentType("application/pdf");
        response.setContentLength((int) file.length());
        response.setHeader("Content-Disposition", "inline;filename=\"" + fileName + "\"");

        FileCopyUtils.copy(inputStream, response.getOutputStream());

    }
Run Code Online (Sandbox Code Playgroud)

反应代码:

function download(filename, text) {
        var element = document.createElement('a');
        element.setAttribute('href', 'data:application/pdf;charset=utf-8,' + encodeURIComponent(text));
         element.setAttribute('target','_blank');
       
        element.style.display = 'none';
        document.body.appendChild(element);
       
        element.click();
       
        document.body.removeChild(element);
        }

    function test () {
        Api(`tempFileDownload/report`, 'Get',"",3).then((data) => …
Run Code Online (Sandbox Code Playgroud)

pdf inputstream outputstream reactjs spring-boot

3
推荐指数
1
解决办法
3208
查看次数

调用的InputStream.read方法太快了

我正在尝试从蓝牙套接字读取InputStream数据,并且该方法在开始时执行它想要做的事情.但由于某种原因,以后它不会读取所有内容.

这是我现在使用的方法:

public int read(byte[] b, int off, int len)
Run Code Online (Sandbox Code Playgroud)

当我检查字节数组时,它的结尾部分是下一部分数据的开头.这意味着即使在读完之前也会再次调用read方法.有谁知道如何处理这个问题?

java inputstream

2
推荐指数
1
解决办法
518
查看次数

Java ProgressMonitorInputStream使用现有的JProgressBar

我正在玩Java的ProgressMonitorInputStream来监视数据流经BufferedInputStream.这是我正在尝试的代码:

InputStream in = new BufferedInputStream(
     new ProgressMonitorInputStream(
     new JFrame(),"Scanning",new FileInputStream(dir.getSearchInputFile())));
Run Code Online (Sandbox Code Playgroud)

这非常合适,并弹出一个新的JFrame窗口,其中包含一个显示输入流进度的进度条.

有没有让ProgressMonitorInputstream更新另一个JFrame中存在的现有JProgressBar?

我尝试了各种方法,例如使用构造函数传递JProgressBar,或尝试在参数中指定帧.每次我尝试,我只是得到一个新的JFrame.

我错了吗?

任何投入将不胜感激.

谢谢

java swing inputstream progressmonitor progress-bar

2
推荐指数
1
解决办法
1207
查看次数