1
1
package de .sstoehr .harreader ;
2
2
3
3
import java .io .File ;
4
- import java .io .IOException ;
4
+ import java .io .InputStream ;
5
5
6
6
import com .fasterxml .jackson .databind .ObjectMapper ;
7
7
10
10
11
11
public class HarReader extends AbstractHarIO {
12
12
13
+ private static final HarReaderMode DEFAULT_READER_MODE = HarReaderMode .STRICT ;
14
+
13
15
public HarReader () {
14
16
super ();
15
17
}
@@ -19,29 +21,68 @@ public HarReader(MapperFactory mapperFactory) {
19
21
}
20
22
21
23
public Har readFromFile (File har ) throws HarReaderException {
22
- return this .readFromFile (har , HarReaderMode . STRICT );
24
+ return this .readFromFile (har , DEFAULT_READER_MODE );
23
25
}
24
26
25
27
public Har readFromFile (File har , HarReaderMode mode ) throws HarReaderException {
26
- ObjectMapper mapper = getMapperFactory ().instance (mode );
27
- try {
28
- return mapper .readValue (har , Har .class );
29
- } catch (IOException e ) {
30
- throw new HarReaderException (e );
31
- }
28
+ return wrap (mode , mapper -> mapper .readValue (har , Har .class ));
29
+ }
30
+
31
+ /**
32
+ * Deserialize HAR from the given {@link InputStream}. {@link InputStream} is closed after reading.
33
+ *
34
+ * @param harInputStream {@link InputStream} to read HAR from.
35
+ * @return HAR deserialized from {@link InputStream}
36
+ * @throws HarReaderException if a low-level I/O problem occurs
37
+ */
38
+ public Har readFromInputStream (InputStream harInputStream ) throws HarReaderException {
39
+ return this .readFromInputStream (harInputStream , DEFAULT_READER_MODE );
40
+ }
41
+
42
+ /**
43
+ * Deserialize HAR from the given {@link InputStream}. {@link InputStream} is closed after reading.
44
+ *
45
+ * @param harInputStream {@link InputStream} to read HAR from.
46
+ * @param mode Reading mode
47
+ * @return HAR deserialized from {@link InputStream}
48
+ * @throws HarReaderException if a low-level I/O problem occurs
49
+ */
50
+ public Har readFromInputStream (InputStream harInputStream , HarReaderMode mode ) throws HarReaderException {
51
+ return wrap (mode , mapper -> mapper .readValue (harInputStream , Har .class ));
32
52
}
33
53
34
54
public Har readFromString (String har ) throws HarReaderException {
35
- return this .readFromString (har , HarReaderMode . STRICT );
55
+ return this .readFromString (har , DEFAULT_READER_MODE );
36
56
}
37
57
38
58
public Har readFromString (String har , HarReaderMode mode ) throws HarReaderException {
39
- ObjectMapper mapper = getMapperFactory ().instance (mode );
40
- try {
41
- return mapper .readValue (har , Har .class );
42
- } catch (IOException e ) {
43
- throw new HarReaderException (e );
44
- }
59
+ return wrap (mode , mapper -> mapper .readValue (har , Har .class ));
45
60
}
46
61
62
+ /**
63
+ * Deserialize HAR from the given byte array.
64
+ *
65
+ * @param bytes Byte array to deserialize HAR from
66
+ * @return HAR deserialized from byte array
67
+ * @throws HarReaderException if a low-level I/O problem occurs
68
+ */
69
+ public Har readFromBytes (byte [] bytes ) throws HarReaderException {
70
+ return this .readFromBytes (bytes , DEFAULT_READER_MODE );
71
+ }
72
+
73
+ /**
74
+ * Deserialize HAR from the given byte array.
75
+ *
76
+ * @param bytes Byte array to deserialize HAR from
77
+ * @param mode Reading mode
78
+ * @return HAR deserialized from byte array
79
+ * @throws HarReaderException if a low-level I/O problem occurs
80
+ */
81
+ public Har readFromBytes (byte [] bytes , HarReaderMode mode ) throws HarReaderException {
82
+ return wrap (mode , mapper -> mapper .readValue (bytes , Har .class ));
83
+ }
84
+
85
+ private <T > T wrap (HarReaderMode mode , IOFunction <ObjectMapper , T > consumer ) throws HarReaderException {
86
+ return wrap (getMapperFactory ().instance (mode ), consumer , HarReaderException ::new );
87
+ }
47
88
}
0 commit comments