// Path: http-builder-ng-core/src/main/java/groovyx/net/http/util/IoUtils.java
// public class IoUtils {
// 
//     /**
//      * Reads all bytes from the stream into a byte array.
//      *
//      * @param inputStream the {@link InputStream}
//      * @return the array of bytes from the stream
//      * @throws IOException if there is a problem reading the stream
//      */
//     public static byte[] streamToBytes(final InputStream inputStream) throws IOException {
//         return streamToBytes(inputStream, true);
//     }
// 
//     public static byte[] streamToBytes(final InputStream inputStream, boolean close) throws IOException {
//         try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
//             final byte[] bytes = new byte[2_048];
// 
//             int read;
//             while ((read = inputStream.read(bytes)) != -1) {
//                 outputStream.write(bytes, 0, read);
//             }
// 
//             return outputStream.toByteArray();
// 
//         } finally {
//             if (close) inputStream.close();
//         }
//     }
// 
//     /**
//      * Safely copies the contents of the {@link BufferedInputStream} to a {@link String} and resets the stream.
//      * This method is generally only useful for testing and logging purposes.
//      *
//      * @param inputStream the BufferedInputStream
//      * @return a String copy of the stream contents
//      * @throws IOException           if something goes wrong with the stream
//      * @throws IllegalStateException if the stream cannot be reset
//      */
//     public static String copyAsString(final BufferedInputStream inputStream) throws IOException, IllegalStateException {
//         if (inputStream == null) return null;
// 
//         try {
//             inputStream.mark(Integer.MAX_VALUE);
//             return new String(streamToBytes(inputStream, false));
//         } finally {
//             try {
//                 inputStream.reset();
//             } catch (IOException ioe) {
//                 throw new IllegalStateException("Unable to reset stream - original stream content may be corrupted");
//             }
//         }
//     }
// 
//     /**
//      * Transfers the contents of the {@link InputStream} into the {@link OutputStream}, optionally closing the stream.
//      *
//      * @param istream the input stream
//      * @param ostream the output stream
//      * @param close   whether or not to close the output stream
//      */
//     public static void transfer(final InputStream istream, final OutputStream ostream, final boolean close) {
//         try {
//             final byte[] bytes = new byte[2_048];
//             int read;
//             while ((read = istream.read(bytes)) != -1) {
//                 ostream.write(bytes, 0, read);
//             }
//         } catch (IOException e) {
//             throw new TransportingException(e);
//         } finally {
//             if (close) {
//                 try {
//                     ostream.close();
//                 } catch (IOException ioe) {
//                     throw new TransportingException(ioe);
//                 }
//             }
//         }
//     }
// }


import groovy.json.JsonBuilder;
import groovy.json.JsonSlurper;
import groovy.lang.Closure;
import groovy.lang.GString;
import groovy.lang.Writable;
import groovy.util.XmlSlurper;
import groovy.util.slurpersupport.GPathResult;
import groovy.xml.StreamingMarkupBuilder;
import groovyx.net.http.util.IoUtils;
import org.apache.xml.resolver.Catalog;
import org.apache.xml.resolver.CatalogManager;
import org.apache.xml.resolver.tools.CatalogResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
         * This CatalogResolver is static to avoid the overhead of re-parsing the catalog definition file every time.  Unfortunately, there's no
         * way to share a single Catalog instance between resolvers.  The {@link Catalog} class is technically not thread-safe, but as long as you
         * do not parse catalog files while using the resolver, it should be fine.
         */
        public static CatalogResolver catalogResolver;

        static {
            CatalogManager catalogManager = new CatalogManager();
            catalogManager.setIgnoreMissingProperties(true);
            catalogManager.setUseStaticCatalog(false);
            catalogManager.setRelativeCatalogs(true);

            try {
                catalogResolver = new CatalogResolver(catalogManager);
                catalogResolver.getCatalog().parseCatalog(NativeHandlers.class.getResource("/catalog/html.xml"));
            } catch (IOException ex) {
                if (log.isWarnEnabled()) {
                    log.warn("Could not resolve default XML catalog", ex);
                }
            }
        }

        /**
         * Standard parser for raw bytes.
         *
         * @param fromServer Backend indenpendent representation of data returned from http server
         * @return Raw bytes of body returned from http server
         */
        public static byte[] streamToBytes(final ChainedHttpConfig config, final FromServer fromServer) {
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Path: http-builder-ng-core/src/main/java/groovyx/net/http/util/IoUtils.java
// public class IoUtils {
// 
//     /**
//      * Reads all bytes from the stream into a byte array.
//      *
//      * @param inputStream the {@link InputStream}
//      * @return the array of bytes from the stream
//      * @throws IOException if there is a problem reading the stream
//      */
//     public static byte[] streamToBytes(final InputStream inputStream) throws IOException {
//         return streamToBytes(inputStream, true);
//     }
// 
//     public static byte[] streamToBytes(final InputStream inputStream, boolean close) throws IOException {
//         try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
//             final byte[] bytes = new byte[2_048];
// 
//             int read;
//             while ((read = inputStream.read(bytes)) != -1) {
//                 outputStream.write(bytes, 0, read);
//             }
// 
//             return outputStream.toByteArray();
// 
//         } finally {
//             if (close) inputStream.close();
//         }
//     }
// 
//     /**
//      * Safely copies the contents of the {@link BufferedInputStream} to a {@link String} and resets the stream.
//      * This method is generally only useful for testing and logging purposes.
//      *
//      * @param inputStream the BufferedInputStream
//      * @return a String copy of the stream contents
//      * @throws IOException           if something goes wrong with the stream
//      * @throws IllegalStateException if the stream cannot be reset
//      */
//     public static String copyAsString(final BufferedInputStream inputStream) throws IOException, IllegalStateException {
//         if (inputStream == null) return null;
// 
//         try {
//             inputStream.mark(Integer.MAX_VALUE);
//             return new String(streamToBytes(inputStream, false));
//         } finally {
//             try {
//                 inputStream.reset();
//             } catch (IOException ioe) {
//                 throw new IllegalStateException("Unable to reset stream - original stream content may be corrupted");
//             }
//         }
//     }
// 
//     /**
//      * Transfers the contents of the {@link InputStream} into the {@link OutputStream}, optionally closing the stream.
//      *
//      * @param istream the input stream
//      * @param ostream the output stream
//      * @param close   whether or not to close the output stream
//      */
//     public static void transfer(final InputStream istream, final OutputStream ostream, final boolean close) {
//         try {
//             final byte[] bytes = new byte[2_048];
//             int read;
//             while ((read = istream.read(bytes)) != -1) {
//                 ostream.write(bytes, 0, read);
//             }
//         } catch (IOException e) {
//             throw new TransportingException(e);
//         } finally {
//             if (close) {
//                 try {
//                     ostream.close();
//                 } catch (IOException ioe) {
//                     throw new TransportingException(ioe);
//                 }
//             }
//         }
//     }
// }


// Path: http-builder-ng-core/src/main/java/groovyx/net/http/NativeHandlers.java
import groovy.json.JsonBuilder;
import groovy.json.JsonSlurper;
import groovy.lang.Closure;
import groovy.lang.GString;
import groovy.lang.Writable;
import groovy.util.XmlSlurper;
import groovy.util.slurpersupport.GPathResult;
import groovy.xml.StreamingMarkupBuilder;
import groovyx.net.http.util.IoUtils;
import org.apache.xml.resolver.Catalog;
import org.apache.xml.resolver.CatalogManager;
import org.apache.xml.resolver.tools.CatalogResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

         * This CatalogResolver is static to avoid the overhead of re-parsing the catalog definition file every time.  Unfortunately, there's no
         * way to share a single Catalog instance between resolvers.  The {@link Catalog} class is technically not thread-safe, but as long as you
         * do not parse catalog files while using the resolver, it should be fine.
         */
        public static CatalogResolver catalogResolver;

        static {
            CatalogManager catalogManager = new CatalogManager();
            catalogManager.setIgnoreMissingProperties(true);
            catalogManager.setUseStaticCatalog(false);
            catalogManager.setRelativeCatalogs(true);

            try {
                catalogResolver = new CatalogResolver(catalogManager);
                catalogResolver.getCatalog().parseCatalog(NativeHandlers.class.getResource("/catalog/html.xml"));
            } catch (IOException ex) {
                if (log.isWarnEnabled()) {
                    log.warn("Could not resolve default XML catalog", ex);
                }
            }
        }

        /**
         * Standard parser for raw bytes.
         *
         * @param fromServer Backend indenpendent representation of data returned from http server
         * @return Raw bytes of body returned from http server
         */
        public static byte[] streamToBytes(final ChainedHttpConfig config, final FromServer fromServer) {
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            IoUtils.transfer(fromServer.getInputStream(), baos, true);