标签: inputstream

支持mark()和reset()的轻量级java.io.InputStream实现

美好的一天,

目前,我们正在使用ByteArrayInputStream来实现可重置的InputStream.我的问题是它消耗了大量内存(它加载了它在内存中表示的所有字节,这与其他一些InputStream实现不同).

我的问题是,是否有任何较轻的InputStream实现支持mark()和read()?

我也尝试在公共场所搜索,但我没有看到任何.

谢谢,弗兰兹

java inputstream reset

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

在Java中将网页内容读入字符串的最佳方法是什么?

我有以下Java代码来获取给定URL的HTML页面的全部内容.这可以以更有效的方式完成吗?欢迎任何改进.

public static String getHTML(final String url) throws IOException {
    if (url == null || url.length() == 0) {
        throw new IllegalArgumentException("url cannot be null or empty");
    }

    final HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    final BufferedReader buf = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    final StringBuilder page = new StringBuilder();
    final String lineEnd = System.getProperty("line.separator");
    String line;
    try {
        while (true) {
            line = buf.readLine();
            if (line == null) {
                break;
            }
            page.append(line).append(lineEnd);
        }
    } finally {
        buf.close();
    }

    return page.toString();
} …
Run Code Online (Sandbox Code Playgroud)

java string optimization inputstream micro-optimization

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

用Java实现keepalive

我正在构建一个客户端 - 服务器应用程序,我必须实现一个keepalive机制,以检测客户端是否崩溃.我在客户端和服务器端都有单独的线程.客户端线程发送"ping"然后休眠3秒,而服务器读取BufferedInputStream并检查是否接收到ping,如果是,则使ping计数器等于零,否则它将计数器递增+1,服务器线程然后休眠持续3秒,如果ping计数器达到3,它会将客户端声明为已死.

问题是,当服务器读取输入流时,它是一个阻塞调用,并且它会一直阻塞,直到收到下一个ping,无论它有多延迟,所以服务器永远不会检测到错过的ping.

任何建议,以便我可以读取流的当前值,如果传入流上没有任何内容,它不会阻止.

谢谢,

java inputstream keep-alive

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

xDocReport-不可能为输入流创建报告

我无法解决这个问题。来源是:

        try {
        // 1) Load Docx file by filling Velocity template engine and cache it to the registry
        InputStream in = new FileInputStream("/test.docx");
        IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in,TemplateEngineKind.Velocity);

        // 2) Create context Java model
        IContext context = report.createContext();
        context.put("user", variables.get("user"));

        // 3) Generate report by merging Java model with the Docx
        OutputStream out = new FileOutputStream(new File("/test_Out.docx"));
        report.process(context, out);

      } catch (IOException e) {
        e.printStackTrace();
      } catch (XDocReportException e) {
        e.printStackTrace();
      }
Run Code Online (Sandbox Code Playgroud)

当我尝试执行此代码时,出现错误:

09:03:15,608 ERROR [stderr] (http--127.0.0.1-8080-1) fr.opensagres.xdocreport.core.XDocReportException: Impossible to …
Run Code Online (Sandbox Code Playgroud)

java inputstream report seam3 xdocreport

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

安全关闭来自其他线程的线程读取套接字输入流

我有一个线程,它in从套接字的输入流中读取了BufferedReader 。它在以下所示的while循环中运行。我还有一个out位于单独线程中的PrintWriter 。

/*One thread*/
try {
    while ((line = in.readLine()) != null) {
        //do stuff
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

/*Second thread*/
try {
    out.println("stuff");
} catch (InterruptedException e) {
    e.printStackTrace();
} finally {
    out.close();
}
Run Code Online (Sandbox Code Playgroud)

当用户请求断开连接(正在单独的线程中处理)时,我想安全地关闭正在从流中读取的这些线程。

当我尝试关闭socket(静态变量)时,我相信它也应该关闭输入和输出流。

/*Third thread trying to stop the other threads so it can execute something*/
try {
    socket.close();
} catch (IOException …
Run Code Online (Sandbox Code Playgroud)

java sockets multithreading inputstream

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

从java InputStream打开图像文件

我正在尝试使用我运行程序的计算机的默认图像查看器打开一个打包在.jar文件中的图像文件.

我已经找到了许多关于如何使用InputStream访问打包在jar中的文件的答案,但是如何使用该InputStream打开这些文件?

InputStream imageStream = Test.class.getClass().getResourceAsStream("/test/DSC_6283.jpg");
Run Code Online (Sandbox Code Playgroud)

我可以将其转换为Image,ImageIcon或者BufferedImage如何在默认图像查看器中进一步打开图像?

我的班级名称是"测试",我想要访问的图像是 C:\Users\Pranav\Documents\NetBeansProjects\Test\src\test\DSC_6283.jpg

任何帮助,将不胜感激.

java image inputstream javax.imageio

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

如何将InputStream提供给CSVReader?

我想转换InputStreamCSVReader.当我使用下面的代码时

CSVReader reader = new CSVReader(new FileReader(ADDRESS_FILE));
Run Code Online (Sandbox Code Playgroud)

它工作正常,但是,如果我从类路径中获取文件,那么此代码不起作用.

我如何将a转换InputStreamCSVReader

这不起作用原因InputStream不是字符串类型:

InputStream input = ImportCsv.class.getResourceAsStream("/dataUpload/staff.csv");
CSVReader reader = new CSVReader(new FileReader(input));
Run Code Online (Sandbox Code Playgroud)

java csv inputstream

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

基本java代码出错

这是一些基本的java代码:

package javaapplication32;

import java.io.*;

public class JavaApplication32 {
    public static void main(String[] args)throws Exception {
        try{
            out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("dec.dat")));
            in = new DataInputStream(new BufferedInputStream(new FileInputStream("enc.dat")));

            String enc=in.readUTF();
            System.out.println(enc);
        }catch(EOFException e){
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)

我收到错误,它找不到符号'in'或'out'

java compiler-errors inputstream outputstream

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

如何对使用Scanner和InputStream的方法进行单元测试?

所以这是代码:

public int foo(InputStream in) {
    int counter = 0;
    Scanner scanner = new Scanner(in);
    while(scanner.hasNextLine()) {
        counter++;
        scanner.nextLine();
    }
    return counter;
}
Run Code Online (Sandbox Code Playgroud)

通常,我会将FileInputStream传递给此方法,但是我希望能够在不访问物理文件的情况下对其进行测试。

我应该模拟File对象吗?如何实施

@Test
public void shouldReturnThree(){

}
Run Code Online (Sandbox Code Playgroud)

java unit-testing inputstream fileinputstream

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

Java序列化:在try或finally块中关闭流?

我正在查看Java序列化文章,并且在try块中而不是finally块中关闭流的示例中偶然发现了很多次.有人可以向我解释为什么会这样吗?

例:

import java.io.*;
public class DeserializeDemo {

    public static void main(String [] args) {
      Employee e = null;
      try {
         FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         e = (Employee) in.readObject();
         in.close();
         fileIn.close();
      } catch(IOException i) {
         i.printStackTrace();
         return;
      } catch(ClassNotFoundException c) {
         System.out.println("Employee class not found");
         c.printStackTrace();
         return;
      }

      System.out.println("Deserialized Employee...");
      System.out.println("Name: " + e.name);
      System.out.println("Address: " + e.address);
      System.out.println("SSN: " + e.SSN);
      System.out.println("Number: " + e.number);
   }
}
Run Code Online (Sandbox Code Playgroud)

资料来源:http://www.tutorialspoint.com/java/java_serialization.htm

java serialization inputstream

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