[jOOQ/jOOQ#9584] Add Source.readString()

This commit is contained in:
Lukas Eder 2019-11-21 13:16:03 +01:00
parent 4652fb2c08
commit b383030db2
2 changed files with 32 additions and 25 deletions

View File

@ -37,6 +37,8 @@
*/
package org.jooq;
import static org.jooq.tools.jdbc.JDBCUtils.safeClose;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
@ -45,6 +47,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
@ -209,6 +212,33 @@ public final class Source {
}
}
/**
* Read the entire {@link #reader()} into a String, for convenience.
*
* @throws IOException When something goes wrong creating a reader from this
* source.
*/
public final String readString() throws IOException {
StringWriter w = new StringWriter();
Reader r = null;
try {
r = reader();
char[] buffer = new char[8192];
int read;
while ((read = r.read(buffer, 0, 8192)) >= 0)
w.write(buffer, 0, read);
}
catch (java.io.IOException e) {
throw new IOException("Could not read source", e);
}
finally {
safeClose(r);
}
return w.toString();
}
private final Reader inputStreamReader(InputStream is) throws UnsupportedEncodingException {
if (charsetName != null)
return new InputStreamReader(is, charsetName);

View File

@ -39,11 +39,6 @@ package org.jooq.impl;
import static org.jooq.SQLDialect.DEFAULT;
import static org.jooq.tools.StringUtils.defaultIfNull;
import static org.jooq.tools.jdbc.JDBCUtils.safeClose;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import org.jooq.Configuration;
import org.jooq.Meta;
@ -71,26 +66,8 @@ final class SourceMetaProvider implements MetaProvider {
@Override
public final Meta provide() {
if (sources.length > 0) {
StringWriter w = new StringWriter();
Reader r = null;
try {
r = sources[0].reader();
char[] buffer = new char[8192];
int nRead;
while ((nRead = r.read(buffer, 0, 8192)) >= 0) {
w.write(buffer, 0, nRead);
}
}
catch (IOException e) {
throw new org.jooq.exception.IOException("Could not read source", e);
}
finally {
safeClose(r);
}
String s = w.toString();
sources[0] = Source.of(w.toString());
String s = sources[0].readString();
sources[0] = Source.of(s);
// TODO: Implement more thorough and reusable "isXML()" check in MiniJAXB
if (s.startsWith("<?xml") || s.startsWith("<information_schema") || s.startsWith("<!--"))