如何在Java中将HAR数据写入空Writer或OutputStream?

时间:2013-11-12 20:23:12

标签: java string java-io outputstream writer

当我通过WebDriver浏览我们正在测试的视频播放器应用程序时,我正在运行Browsermob代理,因此我可以阅读我们的Web服务调用的标题和内容。具体来说,我想确保在驱动程序点击给定链接时传递正确的视频文件名。

我想将BrowserMob生成的HAR对象转换为字符串,以便我可以在字符串中搜索video_filename.flv或其他内容。代理服务器具有writeTo()功能,可将其内容转储为fileOutputStreamWriter。现在,愚蠢的是,我将结果转储到一个文件,然后将文件直接摄入一个字符串并解析它:

    //...lots of WebDriver activity up here with BMP listening
    //...

    //get the HAR data
    Har har = server.getHar();

    //Output HAR to a file
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream("C:\\output.txt");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        har.writeTo(fos);
    } catch (IOException e) {
        e.printStackTrace();
    }

    String harString = readFile("C:\\output.txt", StandardCharsets.UTF_8);

    System.out.println(harString);

这很有效。

当我用WriterOutputStream尝试同样的事情时,在我甚至担心将这些类型之一转换为字符串之前,我会抱怨这些是空指针。考虑一下:

    //get the HAR data
    Har har = server.getHar();

    //Output HAR to a Writer
    Writer harWriter = null;
    try {
        har.writeTo(harWriter);
    } catch (IOException e) {
        e.printStackTrace();
    }

返回空指针异常。

同样适用于OutputStream。在尝试harWriter之前,应该将writeTo()设置为某个非空值吗?我该如何做到这一点?

看,堆栈跟踪:

java.lang.NullPointerException
at org.codehaus.jackson.impl.WriterBasedGenerator._flushBuffer(WriterBasedGenerator.java:1187)
at org.codehaus.jackson.impl.WriterBasedGenerator.close(WriterBasedGenerator.java:837)
at org.codehaus.jackson.map.ObjectMapper._configAndWriteValue(ObjectMapper.java:2004)
at org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1581)
at net.lightbody.bmp.core.har.Har.writeTo(Har.java:30)
at com.videoplayer.CorrectVideosAreSentToThePlayer.teardown(CorrectVideosAreSentToThePlayer.java:152)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

1 个答案:

答案 0 :(得分:0)

是的,当然如果你尝试写入null,你会得到一个NullPointerException!为什么你认为这会起作用?

你需要写一个StringWriter或类似的东西。那会给你一个字符串。