From b383030db26bd4181cd22aa06b8a25be15fd6296 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Thu, 21 Nov 2019 13:16:03 +0100 Subject: [PATCH] [jOOQ/jOOQ#9584] Add Source.readString() --- jOOQ/src/main/java/org/jooq/Source.java | 30 +++++++++++++++++++ .../org/jooq/impl/SourceMetaProvider.java | 27 ++--------------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/Source.java b/jOOQ/src/main/java/org/jooq/Source.java index 5460d255ef..0c1e8a7880 100644 --- a/jOOQ/src/main/java/org/jooq/Source.java +++ b/jOOQ/src/main/java/org/jooq/Source.java @@ -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); diff --git a/jOOQ/src/main/java/org/jooq/impl/SourceMetaProvider.java b/jOOQ/src/main/java/org/jooq/impl/SourceMetaProvider.java index 47a6c3e002..711641f484 100644 --- a/jOOQ/src/main/java/org/jooq/impl/SourceMetaProvider.java +++ b/jOOQ/src/main/java/org/jooq/impl/SourceMetaProvider.java @@ -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("