Skip to content

Commit 85757b9

Browse files
committed
One additional piece for #680: implement Path-methods in BinaryTSFactory too
1 parent ab47708 commit 85757b9

File tree

3 files changed

+185
-27
lines changed

3 files changed

+185
-27
lines changed

src/main/java/com/fasterxml/jackson/core/base/BinaryTSFactory.java

+34-26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.*;
44
import java.net.URL;
5+
import java.nio.file.Path;
56

67
import com.fasterxml.jackson.core.*;
78
import com.fasterxml.jackson.core.io.IOContext;
@@ -63,19 +64,35 @@ public boolean canHandleBinaryNatively() {
6364
*/
6465

6566
@Override
66-
public JsonParser createParser(ObjectReadContext readCtxt, File f) throws JacksonException {
67+
public JsonParser createParser(ObjectReadContext readCtxt,
68+
File f) throws JacksonException
69+
{
6770
final InputStream in = _fileInputStream(f);
6871
// true, since we create InputStream from File
6972
final IOContext ioCtxt = _createContext(f, true);
70-
return _createParser(readCtxt, ioCtxt, _decorate(ioCtxt, in));
73+
return _createParser(readCtxt, ioCtxt,
74+
_decorate(ioCtxt, in));
75+
}
76+
77+
@Override
78+
public JsonParser createParser(ObjectReadContext readCtxt,
79+
Path p) throws JacksonException
80+
{
81+
// true, since we create InputStream from Path
82+
IOContext ioCtxt = _createContext(p, true);
83+
return _createParser(readCtxt, ioCtxt,
84+
_decorate(ioCtxt, _pathInputStream(p)));
7185
}
7286

7387
@Override
74-
public JsonParser createParser(ObjectReadContext readCtxt, URL url) throws JacksonException {
88+
public JsonParser createParser(ObjectReadContext readCtxt,
89+
URL url) throws JacksonException
90+
{
7591
// true, since we create InputStream from URL
7692
IOContext ioCtxt = _createContext(url, true);
7793
InputStream in = _optimizedStreamFromURL(url);
78-
return _createParser(readCtxt, ioCtxt, _decorate(ioCtxt, in));
94+
return _createParser(readCtxt, ioCtxt,
95+
_decorate(ioCtxt, in));
7996
}
8097

8198
@Override
@@ -121,7 +138,7 @@ public JsonParser createParser(ObjectReadContext readCtxt,
121138
IOContext ioCtxt = _createContext(in, false);
122139
return _createParser(readCtxt, ioCtxt, _decorate(ioCtxt, in));
123140
}
124-
141+
125142
protected abstract JsonParser _createParser(ObjectReadContext readCtxt,
126143
IOContext ioCtxt, InputStream in) throws JacksonException;
127144

@@ -137,26 +154,6 @@ protected abstract JsonParser _createParser(ObjectReadContext readCtxt,
137154
/**********************************************************************
138155
*/
139156

140-
/*
141-
@Override
142-
public JsonGenerator createGenerator(OutputStream out, JsonEncoding enc)
143-
throws JacksonException
144-
{
145-
// false -> we won't manage the stream unless explicitly directed to
146-
IOContext ioCtxt = _createContext(out, false, enc);
147-
return _createGenerator(EMPTY_WRITE_CONTEXT, ioCtxt, _decorate(ioCtxt, out));
148-
}
149-
150-
@Override
151-
public JsonGenerator createGenerator(File f, JsonEncoding enc) throws JacksonException
152-
{
153-
OutputStream out = new FileOutputStream(f);
154-
// true -> yes, we have to manage the stream since we created it
155-
IOContext ioCtxt = _createContext(out, true, enc);
156-
return _createGenerator(EMPTY_WRITE_CONTEXT, ioCtxt, _decorate(ioCtxt, out));
157-
}
158-
*/
159-
160157
@Override
161158
public JsonGenerator createGenerator(ObjectWriteContext writeCtxt,
162159
OutputStream out, JsonEncoding enc)
@@ -169,7 +166,8 @@ public JsonGenerator createGenerator(ObjectWriteContext writeCtxt,
169166

170167
@Override
171168
public JsonGenerator createGenerator(ObjectWriteContext writeCtxt,
172-
Writer w) throws JacksonException {
169+
Writer w) throws JacksonException
170+
{
173171
return _nonByteTarget();
174172
}
175173

@@ -183,6 +181,16 @@ public JsonGenerator createGenerator(ObjectWriteContext writeCtxt,
183181
return _createGenerator(writeCtxt, ioCtxt, _decorate(ioCtxt, out));
184182
}
185183

184+
@Override
185+
public JsonGenerator createGenerator(ObjectWriteContext writeCtxt,
186+
Path p, JsonEncoding enc)
187+
throws JacksonException
188+
{
189+
final OutputStream out = _pathOutputStream(p);
190+
final IOContext ioCtxt = _createContext(p, true, enc);
191+
return _createGenerator(writeCtxt, ioCtxt, _decorate(ioCtxt, out));
192+
}
193+
186194
/*
187195
/**********************************************************************
188196
/* Factory methods: abstract, for sub-classes to implement

src/main/java/com/fasterxml/jackson/core/base/TextualTSFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public JsonParser createParser(ObjectReadContext readCtxt,
184184
IOContext ioCtxt = _createContext(in, false);
185185
return _createParser(readCtxt, ioCtxt, _decorate(ioCtxt, in));
186186
}
187-
187+
188188
protected abstract JsonParser _createParser(ObjectReadContext readCtxt,
189189
IOContext ctxt, InputStream in) throws JacksonException;
190190

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package com.fasterxml.jackson.core.base;
2+
3+
import java.io.DataInput;
4+
import java.io.InputStream;
5+
import java.io.OutputStream;
6+
import java.io.Reader;
7+
import java.io.Writer;
8+
9+
import com.fasterxml.jackson.core.BaseTest;
10+
import com.fasterxml.jackson.core.FormatSchema;
11+
import com.fasterxml.jackson.core.JacksonException;
12+
import com.fasterxml.jackson.core.JsonGenerator;
13+
import com.fasterxml.jackson.core.JsonParser;
14+
import com.fasterxml.jackson.core.ObjectReadContext;
15+
import com.fasterxml.jackson.core.ObjectWriteContext;
16+
import com.fasterxml.jackson.core.TokenStreamFactory;
17+
import com.fasterxml.jackson.core.Version;
18+
import com.fasterxml.jackson.core.io.IOContext;
19+
20+
// Bit different "test" class, used to check that intermediate base types
21+
// (textual, binary format bases) are complete enough. This is not done
22+
// via test methods but just by having minimal definitions present.
23+
//
24+
// In future might add some actual tests too
25+
@SuppressWarnings("serial")
26+
public class FactoryBaseImplsTest extends BaseTest
27+
{
28+
static class ToyBinaryFormatFactory
29+
extends BinaryTSFactory
30+
{
31+
public ToyBinaryFormatFactory() { super(0, 0); }
32+
33+
@Override
34+
protected JsonParser _createParser(ObjectReadContext readCtxt,
35+
IOContext ioCtxt, InputStream in) throws JacksonException {
36+
return null;
37+
}
38+
39+
@Override
40+
protected JsonParser _createParser(ObjectReadContext readCtxt,
41+
IOContext ioCtxt, byte[] data, int offset, int len) throws JacksonException {
42+
return null;
43+
}
44+
45+
@Override
46+
protected JsonParser _createParser(ObjectReadContext readCtxt,
47+
IOContext ioCtxt, DataInput input) throws JacksonException {
48+
return null;
49+
}
50+
51+
@Override
52+
protected JsonGenerator _createGenerator(ObjectWriteContext writeCtxt,
53+
IOContext ioCtxt, OutputStream out) throws JacksonException {
54+
return null;
55+
}
56+
57+
@Override
58+
public TokenStreamFactory copy() { return this; }
59+
@Override
60+
public TokenStreamFactory snapshot() { return this; }
61+
@Override
62+
public TSFBuilder<?, ?> rebuild() { return null; }
63+
64+
@Override
65+
public boolean canParseAsync() { return false; }
66+
@Override
67+
public boolean canUseSchema(FormatSchema schema) { return false; }
68+
69+
@Override
70+
public String getFormatName() { return null; }
71+
72+
@Override
73+
public Version version() { return Version.unknownVersion(); }
74+
}
75+
76+
static class ToyTextualFormatFactory
77+
extends TextualTSFactory
78+
{
79+
public ToyTextualFormatFactory() { super(0, 0); }
80+
81+
@Override
82+
protected JsonParser _createParser(ObjectReadContext readCtxt,
83+
IOContext ctxt, InputStream in) throws JacksonException {
84+
return null;
85+
}
86+
87+
@Override
88+
protected JsonParser _createParser(ObjectReadContext readCtxt,
89+
IOContext ctxt, Reader r) throws JacksonException {
90+
return null;
91+
}
92+
93+
@Override
94+
protected JsonParser _createParser(ObjectReadContext readCtxt,
95+
IOContext ctxt,
96+
byte[] data, int offset, int len) throws JacksonException {
97+
return null;
98+
}
99+
100+
@Override
101+
protected JsonParser _createParser(ObjectReadContext readCtxt,
102+
IOContext ctxt, char[] data, int offset, int len, boolean recyclable)
103+
throws JacksonException {
104+
return null;
105+
}
106+
107+
@Override
108+
protected JsonParser _createParser(ObjectReadContext readCtxt,
109+
IOContext ctxt, DataInput input) throws JacksonException {
110+
return null;
111+
}
112+
113+
@Override
114+
protected JsonGenerator _createGenerator(ObjectWriteContext writeCtxt,
115+
IOContext ioCtxt, Writer out) throws JacksonException {
116+
return null;
117+
}
118+
119+
@Override
120+
protected JsonGenerator _createUTF8Generator(ObjectWriteContext writeCtxt,
121+
IOContext ioCtxt, OutputStream out) throws JacksonException {
122+
return null;
123+
}
124+
125+
@Override
126+
public TokenStreamFactory copy() { return this; }
127+
@Override
128+
public TokenStreamFactory snapshot() { return this; }
129+
@Override
130+
public TSFBuilder<?, ?> rebuild() { return null; }
131+
132+
@Override
133+
public boolean canParseAsync() { return false; }
134+
@Override
135+
public boolean canUseSchema(FormatSchema schema) { return false; }
136+
137+
@Override
138+
public String getFormatName() { return null; }
139+
140+
@Override
141+
public Version version() { return Version.unknownVersion(); }
142+
}
143+
144+
public void testBogus() {
145+
// no real tests but need one "test" method to avoid junit fail
146+
147+
assertNotNull(new ToyBinaryFormatFactory());
148+
assertNotNull(new ToyTextualFormatFactory());
149+
}
150+
}

0 commit comments

Comments
 (0)