【问题标题】:How do I write HAR data to a null Writer or OutputStream in Java?如何在 Java 中将 HAR 数据写入 null Writer 或 OutputStream?
【发布时间】:2013-11-25 04:33:37
【问题描述】:

当我通过 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 也是如此。在我尝试writeTo() 之前应该将harWriter 设置为一些非空值吗?我该如何做到这一点?

看,堆栈跟踪:

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)

【问题讨论】:

    标签: java string java-io outputstream writer


    【解决方案1】:

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

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

    【讨论】:

    • 但是您认为会发生什么?写一些数据……无处可去……你怎么能把数据再拿出来?
    • 我认为 null 会被正在写入该对象的数据所取代。没想到这么牵强。
    • 所以,您认为它会将 null 追溯到它所来自的变量,并为您填充一个新的 Writer。好吧,让我打消你这个念头。 null 不知道它在 Java 中的来源。也不是我知道的任何其他语言。这只是虚无。没有数据。就是这样。
    • 我不是这么想的,这并不重要。我显然对什么是作家一开始就不够了解。我认为 Writer 是其他类型的数据对象,我可以将这个 HAR 对象转换成它,然后可以转过来并把它本身转换成一个字符串。你一直很有帮助。谢谢你打消了我愚蠢的想法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 2011-02-12
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    相关资源
    最近更新 更多