diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt index 4712f94b3..dfeb4cb6d 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt @@ -118,12 +118,12 @@ object ChapterProvider { val matcher = AppPattern.imgPattern.matcher(text) if (matcher.find()) { matcher.group(1)?.let { src -> - if (!book.isEpub()) { + //if (!book.isEpub()) { durY = setTypeImage( book, bookChapter, src, durY, textPages, book.getImageStyle() ) - } + //} } } else { val isTitle = index == 0 diff --git a/epublib/src/main/java/me/ag2s/epublib/epub/EpubWriter.java b/epublib/src/main/java/me/ag2s/epublib/epub/EpubWriter.java index 11d716b68..c421d5a9f 100644 --- a/epublib/src/main/java/me/ag2s/epublib/epub/EpubWriter.java +++ b/epublib/src/main/java/me/ag2s/epublib/epub/EpubWriter.java @@ -102,6 +102,7 @@ public class EpubWriter { try { resultStream.putNextEntry(new ZipEntry("OEBPS/" + resource.getHref())); InputStream inputStream = resource.getInputStream(); + IOUtil.copy(inputStream, resultStream); inputStream.close(); } catch (Exception e) { diff --git a/epublib/src/main/java/me/ag2s/epublib/util/IOUtil.java b/epublib/src/main/java/me/ag2s/epublib/util/IOUtil.java index 8bb4bf8cc..c5d57d64f 100644 --- a/epublib/src/main/java/me/ag2s/epublib/util/IOUtil.java +++ b/epublib/src/main/java/me/ag2s/epublib/util/IOUtil.java @@ -1,165 +1,961 @@ package me.ag2s.epublib.util; -import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; +import java.io.Closeable; +import java.io.EOFException; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.OutputStream; +import java.io.OutputStreamWriter; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; +import java.net.HttpURLConnection; +import java.net.URLConnection; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.channels.ReadableByteChannel; +import java.nio.charset.Charset; + +import me.ag2s.epublib.util.commons.io.IOConsumer; /** * Most of the functions herein are re-implementations of the ones in * apache io IOUtils. - * + *
* The reason for re-implementing this is that the functions are fairly simple * and using my own implementation saves the inclusion of a 200Kb jar file. */ public class IOUtil { - public static final int IO_COPY_BUFFER_SIZE = 1024 * 4; - - /** - * Gets the contents of the Reader as a byte[], with the given character encoding. - * - * @param in g - * @param encoding g - * @return the contents of the Reader as a byte[], with the given character encoding. - * @throws IOException g - */ - public static byte[] toByteArray(Reader in, String encoding) - throws IOException { - StringWriter out = new StringWriter(); - copy(in, out); - out.flush(); - return out.toString().getBytes(encoding); - } - - /** - * Returns the contents of the InputStream as a byte[] - * - * @param in f - * @return the contents of the InputStream as a byte[] - * @throws IOException f - */ - public static byte[] toByteArray(InputStream in) throws IOException { - ByteArrayOutputStream result = new ByteArrayOutputStream(); - copy(in, result); - result.flush(); - return result.toByteArray(); - } - - /** - * Reads data from the InputStream, using the specified buffer size. - * - * This is meant for situations where memory is tight, since - * it prevents buffer expansion. - * - * @param in the stream to read data from - * @param size the size of the array to create - * @return the array, or null - * @throws IOException f - */ - public static byte[] toByteArray(InputStream in, int size) - throws IOException { - - try { - ByteArrayOutputStream result; - - if (size > 0) { - result = new ByteArrayOutputStream(size); - } else { - result = new ByteArrayOutputStream(); - } - - copy(in, result); - result.flush(); - return result.toByteArray(); - } catch (OutOfMemoryError error) { - //Return null so it gets loaded lazily. - return null; - } - - } - - - /** - * if totalNrRead < 0 then totalNrRead is returned, if - * (nrRead + totalNrRead) < Integer.MAX_VALUE then nrRead + totalNrRead - * is returned, -1 otherwise. - * - * @param nrRead f - * @param totalNrNread f - * @return if totalNrRead < 0 then totalNrRead is returned, if - * (nrRead + totalNrRead) < Integer.MAX_VALUE then nrRead + totalNrRead - * is returned, -1 otherwise. - */ - protected static int calcNewNrReadSize(int nrRead, int totalNrNread) { - if (totalNrNread < 0) { - return totalNrNread; - } - if (totalNrNread > (Integer.MAX_VALUE - nrRead)) { - return -1; - } else { - return (totalNrNread + nrRead); - } - } - - /** - * Copies the contents of the InputStream to the OutputStream. - * - * @param in f - * @param out f - * @return the nr of bytes read, or -1 if the amount > Integer.MAX_VALUE - * @throws IOException f - */ - public static int copy(InputStream in, OutputStream out) - throws IOException { - byte[] buffer = new byte[IO_COPY_BUFFER_SIZE]; - int readSize ; - int result = 0; - while ((readSize = in.read(buffer)) >= 0) { - out.write(buffer, 0, readSize); - result = calcNewNrReadSize(readSize, result); - } - out.flush(); - return result; - } - - /** - * Copies the contents of the Reader to the Writer. - * - * @param in f - * @param out f - * @return the nr of characters read, or -1 if the amount > Integer.MAX_VALUE - * @throws IOException f - */ - public static int copy(Reader in, Writer out) throws IOException { - char[] buffer = new char[IO_COPY_BUFFER_SIZE]; - int readSize; - int result = 0; - while ((readSize = in.read(buffer)) >= 0) { - out.write(buffer, 0, readSize); - result = calcNewNrReadSize(readSize, result); - } - out.flush(); - return result; - } - - public static String Stream2String(InputStream inputStream) { - String str; - try { - BufferedInputStream bis = new BufferedInputStream(inputStream); - ByteArrayOutputStream buf = new ByteArrayOutputStream(); - for (int result = bis.read(); result != -1; result = bis.read()) { - buf.write((byte) result); - } - str=buf.toString("UTF-8"); - }catch (Exception e){ - str=null; - } -// StandardCharsets.UTF_8.name() > JDK 7 - return str; - } + /** + * Represents the end-of-file (or stream). + * + * @since 2.5 (made public) + */ + public static final int EOF = -1; + + + public static final int DEFAULT_BUFFER_SIZE = 1024 * 8; + private static final byte[] SKIP_BYTE_BUFFER = new byte[DEFAULT_BUFFER_SIZE]; + + // Allocated in the relevant skip method if necessary. + /* + * These buffers are static and are shared between threads. + * This is possible because the buffers are write-only - the contents are never read. + * + * N.B. there is no need to synchronize when creating these because: + * - we don't care if the buffer is created multiple times (the data is ignored) + * - we always use the same size buffer, so if it it is recreated it will still be OK + * (if the buffer size were variable, we would need to synch. to ensure some other thread + * did not create a smaller one) + */ + private static char[] SKIP_CHAR_BUFFER; + + /** + * Gets the contents of the Reader as a byte[], with the given character encoding. + * + * @param in g + * @param encoding g + * @return the contents of the Reader as a byte[], with the given character encoding. + * @throws IOException g + */ + public static byte[] toByteArray(Reader in, String encoding) + throws IOException { + StringWriter out = new StringWriter(); + copy(in, out); + out.flush(); + return out.toString().getBytes(encoding); + } + + /** + * Returns the contents of the InputStream as a byte[] + * + * @param in f + * @return the contents of the InputStream as a byte[] + * @throws IOException f + */ + public static byte[] toByteArray(InputStream in) throws IOException { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + copy(in, result); + result.flush(); + return result.toByteArray(); + } + + + /** + * Reads data from the InputStream, using the specified buffer size. + *
+ * This is meant for situations where memory is tight, since
+ * it prevents buffer expansion.
+ *
+ * @param in the stream to read data from
+ * @param size the size of the array to create
+ * @return the array, or null
+ * @throws IOException f
+ */
+ public static byte[] toByteArray(InputStream in, int size)
+ throws IOException {
+
+ try {
+ ByteArrayOutputStream result;
+
+ if (size > 0) {
+ result = new ByteArrayOutputStream(size);
+ } else {
+ result = new ByteArrayOutputStream();
+ }
+
+ copy(in, result);
+ result.flush();
+ return result.toByteArray();
+ } catch (OutOfMemoryError error) {
+ //Return null so it gets loaded lazily.
+ return null;
+ }
+
+ }
+
+
+ /**
+ * if totalNrRead < 0 then totalNrRead is returned, if
+ * (nrRead + totalNrRead) < Integer.MAX_VALUE then nrRead + totalNrRead
+ * is returned, -1 otherwise.
+ *
+ * @param nrRead f
+ * @param totalNrNread f
+ * @return if totalNrRead < 0 then totalNrRead is returned, if
+ * (nrRead + totalNrRead) < Integer.MAX_VALUE then nrRead + totalNrRead
+ * is returned, -1 otherwise.
+ */
+ protected static int calcNewNrReadSize(int nrRead, int totalNrNread) {
+ if (totalNrNread < 0) {
+ return totalNrNread;
+ }
+ if (totalNrNread > (Integer.MAX_VALUE - nrRead)) {
+ return -1;
+ } else {
+ return (totalNrNread + nrRead);
+ }
+ }
+
+ //
+ public static void copy(InputStream in, OutputStream result) throws IOException {
+ int buffer=in.available();
+ if(buffer>IOUtil.DEFAULT_BUFFER_SIZE||buffer==0){
+ buffer=IOUtil.DEFAULT_BUFFER_SIZE;
+ }
+ copy(in, result,buffer);
+ }
+
+ /**
+ * Copies bytes from an InputStream
to an OutputStream
using an internal buffer of the
+ * given size.
+ *
+ * This method buffers the input internally, so there is no need to use a BufferedInputStream
.
+ *
InputStream
to read from
+ * @param output the OutputStream
to write to
+ * @param bufferSize the bufferSize used to copy from the input to the output
+ * @return the number of bytes copied. or {@code 0} if {@code input is null}.
+ * @throws NullPointerException if the output is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.5
+ */
+ public static long copy(final InputStream input, final OutputStream output, final int bufferSize)
+ throws IOException {
+ return copyLarge(input, output, new byte[bufferSize]);
+ }
+
+ /**
+ * Copies bytes from an InputStream
to chars on a
+ * Writer
using the default character encoding of the platform.
+ *
+ * This method buffers the input internally, so there is no need to use a
+ * BufferedInputStream
.
+ *
+ * This method uses {@link InputStreamReader}.
+ *
+ * @param input the InputStream
to read from
+ * @param output the Writer
to write to
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ * @deprecated 2.5 use {@link #copy(InputStream, Writer, Charset)} instead
+ */
+ @Deprecated
+ public static void copy(final InputStream input, final Writer output)
+ throws IOException {
+ copy(input, output, Charset.defaultCharset());
+ }
+
+ /**
+ * Copies bytes from an InputStream
to chars on a
+ * Writer
using the specified character encoding.
+ *
+ * This method buffers the input internally, so there is no need to use a
+ * BufferedInputStream
.
+ *
+ * This method uses {@link InputStreamReader}.
+ *
+ * @param input the InputStream
to read from
+ * @param output the Writer
to write to
+ * @param inputCharset the charset to use for the input stream, null means platform default
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.3
+ */
+ public static void copy(final InputStream input, final Writer output, final Charset inputCharset)
+ throws IOException {
+ final InputStreamReader in = new InputStreamReader(input, inputCharset.name());
+ copy(in, output);
+ }
+
+ /**
+ * Copies bytes from an InputStream
to chars on a
+ * Writer
using the specified character encoding.
+ *
+ * This method buffers the input internally, so there is no need to use a
+ * BufferedInputStream
.
+ *
+ * Character encoding names can be found at + * IANA. + *
+ * This method uses {@link InputStreamReader}.
+ *
+ * @param input the InputStream
to read from
+ * @param output the Writer
to write to
+ * @param inputCharsetName the name of the requested charset for the InputStream, null means platform default
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io
+ * .UnsupportedEncodingException} in version 2.2 if the
+ * encoding is not supported.
+ * @since 1.1
+ */
+ public static void copy(final InputStream input, final Writer output, final String inputCharsetName)
+ throws IOException {
+ copy(input, output, Charset.forName(inputCharsetName));
+ }
+
+ /**
+ * Copies chars from a Reader
to a Appendable
.
+ *
+ * This method buffers the input internally, so there is no need to use a
+ * BufferedReader
.
+ *
+ * Large streams (over 2GB) will return a chars copied value of
+ * -1
after the copy has completed since the correct
+ * number of chars cannot be returned as an int. For large streams
+ * use the copyLarge(Reader, Writer)
method.
+ *
+ * @param input the Reader
to read from
+ * @param output the Appendable
to write to
+ * @return the number of characters copied, or -1 if > Integer.MAX_VALUE
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.7
+ */
+ public static long copy(final Reader input, final Appendable output) throws IOException {
+ return copy(input, output, CharBuffer.allocate(DEFAULT_BUFFER_SIZE));
+ }
+
+ /**
+ * Copies chars from a Reader
to an Appendable
.
+ *
+ * This method uses the provided buffer, so there is no need to use a
+ * BufferedReader
.
+ *
Reader
to read from
+ * @param output the Appendable
to write to
+ * @param buffer the buffer to be used for the copy
+ * @return the number of characters copied
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.7
+ */
+ public static long copy(final Reader input, final Appendable output, final CharBuffer buffer) throws IOException {
+ long count = 0;
+ int n;
+ while (EOF != (n = input.read(buffer))) {
+ buffer.flip();
+ output.append(buffer, 0, n);
+ count += n;
+ }
+ return count;
+ }
+
+ /**
+ * Copies chars from a Reader
to bytes on an
+ * OutputStream
using the default character encoding of the
+ * platform, and calling flush.
+ *
+ * This method buffers the input internally, so there is no need to use a
+ * BufferedReader
.
+ *
+ * Due to the implementation of OutputStreamWriter, this method performs a + * flush. + *
+ * This method uses {@link OutputStreamWriter}.
+ *
+ * @param input the Reader
to read from
+ * @param output the OutputStream
to write to
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ * @deprecated 2.5 use {@link #copy(Reader, OutputStream, Charset)} instead
+ */
+ @Deprecated
+ public static void copy(final Reader input, final OutputStream output)
+ throws IOException {
+ copy(input, output, Charset.defaultCharset());
+ }
+
+ /**
+ * Copies chars from a Reader
to bytes on an
+ * OutputStream
using the specified character encoding, and
+ * calling flush.
+ *
+ * This method buffers the input internally, so there is no need to use a
+ * BufferedReader
.
+ *
+ * Due to the implementation of OutputStreamWriter, this method performs a + * flush. + *
+ *+ * This method uses {@link OutputStreamWriter}. + *
+ * + * @param input theReader
to read from
+ * @param output the OutputStream
to write to
+ * @param outputCharset the charset to use for the OutputStream, null means platform default
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.3
+ */
+ public static void copy(final Reader input, final OutputStream output, final Charset outputCharset)
+ throws IOException {
+ final OutputStreamWriter out = new OutputStreamWriter(output, outputCharset.name());
+ copy(input, out);
+ // XXX Unless anyone is planning on rewriting OutputStreamWriter,
+ // we have to flush here.
+ out.flush();
+ }
+
+ /**
+ * Copies chars from a Reader
to bytes on an
+ * OutputStream
using the specified character encoding, and
+ * calling flush.
+ *
+ * This method buffers the input internally, so there is no need to use a
+ * BufferedReader
.
+ *
+ * Character encoding names can be found at + * IANA. + *
+ * Due to the implementation of OutputStreamWriter, this method performs a + * flush. + *
+ * This method uses {@link OutputStreamWriter}.
+ *
+ * @param input the Reader
to read from
+ * @param output the OutputStream
to write to
+ * @param outputCharsetName the name of the requested charset for the OutputStream, null means platform default
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io
+ * .UnsupportedEncodingException} in version 2.2 if the
+ * encoding is not supported.
+ * @since 1.1
+ */
+ public static void copy(final Reader input, final OutputStream output, final String outputCharsetName)
+ throws IOException {
+ copy(input, output, Charset.forName(outputCharsetName));
+ }
+
+ /**
+ * Copies chars from a Reader
to a Writer
.
+ *
+ * This method buffers the input internally, so there is no need to use a
+ * BufferedReader
.
+ *
+ * Large streams (over 2GB) will return a chars copied value of
+ * -1
after the copy has completed since the correct
+ * number of chars cannot be returned as an int. For large streams
+ * use the copyLarge(Reader, Writer)
method.
+ *
+ * @param input the Reader
to read from
+ * @param output the Writer
to write to
+ * @return the number of characters copied, or -1 if > Integer.MAX_VALUE
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.1
+ */
+ public static int copy(final Reader input, final Writer output) throws IOException {
+ final long count = copyLarge(input, output);
+ if (count > Integer.MAX_VALUE) {
+ return -1;
+ }
+ return (int) count;
+ }
+
+ /**
+ * Copies bytes from a large (over 2GB) InputStream
to an
+ * OutputStream
.
+ *
+ * This method buffers the input internally, so there is no need to use a
+ * BufferedInputStream
.
+ *
+ * The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}. + *
+ * + * @param input theInputStream
to read from
+ * @param output the OutputStream
to write to
+ * @return the number of bytes copied. or {@code 0} if {@code input is null}.
+ * @throws NullPointerException if the output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.3
+ */
+ public static long copyLarge(final InputStream input, final OutputStream output)
+ throws IOException {
+ return copy(input, output, DEFAULT_BUFFER_SIZE);
+ }
+
+ /**
+ * Copies bytes from a large (over 2GB) InputStream
to an
+ * OutputStream
.
+ *
+ * This method uses the provided buffer, so there is no need to use a
+ * BufferedInputStream
.
+ *
InputStream
to read from
+ * @param output the OutputStream
to write to
+ * @param buffer the buffer to use for the copy
+ * @return the number of bytes copied. or {@code 0} if {@code input is null}.
+ * @throws IOException if an I/O error occurs
+ * @since 2.2
+ */
+ public static long copyLarge(final InputStream input, final OutputStream output, final byte[] buffer)
+ throws IOException {
+ long count = 0;
+ if (input != null) {
+ int n;
+ while (EOF != (n = input.read(buffer))) {
+ output.write(buffer, 0, n);
+ count += n;
+ }
+ }
+ return count;
+ }
+
+ /**
+ * Copies some or all bytes from a large (over 2GB) InputStream
to an
+ * OutputStream
, optionally skipping input bytes.
+ *
+ * This method buffers the input internally, so there is no need to use a
+ * BufferedInputStream
.
+ *
+ * Note that the implementation uses {@link #skip(InputStream, long)}. + * This means that the method may be considerably less efficient than using the actual skip implementation, + * this is done to guarantee that the correct number of characters are skipped. + *
+ * The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}. + * + * @param input theInputStream
to read from
+ * @param output the OutputStream
to write to
+ * @param inputOffset : number of bytes to skip from input before copying
+ * -ve values are ignored
+ * @param length : number of bytes to copy. -ve means all
+ * @return the number of bytes copied
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.2
+ */
+ public static long copyLarge(final InputStream input, final OutputStream output, final long inputOffset,
+ final long length) throws IOException {
+ return copyLarge(input, output, inputOffset, length, new byte[DEFAULT_BUFFER_SIZE]);
+ }
+
+ /**
+ * Copies some or all bytes from a large (over 2GB) InputStream
to an
+ * OutputStream
, optionally skipping input bytes.
+ *
+ * This method uses the provided buffer, so there is no need to use a
+ * BufferedInputStream
.
+ *
+ * Note that the implementation uses {@link #skip(InputStream, long)}. + * This means that the method may be considerably less efficient than using the actual skip implementation, + * this is done to guarantee that the correct number of characters are skipped. + *
+ * + * @param input theInputStream
to read from
+ * @param output the OutputStream
to write to
+ * @param inputOffset : number of bytes to skip from input before copying
+ * -ve values are ignored
+ * @param length : number of bytes to copy. -ve means all
+ * @param buffer the buffer to use for the copy
+ * @return the number of bytes copied
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.2
+ */
+ public static long copyLarge(final InputStream input, final OutputStream output,
+ final long inputOffset, final long length, final byte[] buffer) throws IOException {
+ if (inputOffset > 0) {
+ skipFully(input, inputOffset);
+ }
+ if (length == 0) {
+ return 0;
+ }
+ final int bufferLength = buffer.length;
+ int bytesToRead = bufferLength;
+ if (length > 0 && length < bufferLength) {
+ bytesToRead = (int) length;
+ }
+ int read;
+ long totalRead = 0;
+ while (bytesToRead > 0 && EOF != (read = input.read(buffer, 0, bytesToRead))) {
+ output.write(buffer, 0, read);
+ totalRead += read;
+ if (length > 0) { // only adjust length if not reading to the end
+ // Note the cast must work because buffer.length is an integer
+ bytesToRead = (int) Math.min(length - totalRead, bufferLength);
+ }
+ }
+ return totalRead;
+ }
+
+ /**
+ * Copies chars from a large (over 2GB) Reader
to a Writer
.
+ *
+ * This method buffers the input internally, so there is no need to use a
+ * BufferedReader
.
+ *
+ * The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}.
+ *
+ * @param input the Reader
to read from
+ * @param output the Writer
to write to
+ * @return the number of characters copied
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 1.3
+ */
+ public static long copyLarge(final Reader input, final Writer output) throws IOException {
+ return copyLarge(input, output, new char[DEFAULT_BUFFER_SIZE]);
+ }
+
+ /**
+ * Copies chars from a large (over 2GB) Reader
to a Writer
.
+ *
+ * This method uses the provided buffer, so there is no need to use a
+ * BufferedReader
.
+ *
+ *
+ * @param input the Reader
to read from
+ * @param output the Writer
to write to
+ * @param buffer the buffer to be used for the copy
+ * @return the number of characters copied
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.2
+ */
+ public static long copyLarge(final Reader input, final Writer output, final char[] buffer) throws IOException {
+ long count = 0;
+ int n;
+ while (EOF != (n = input.read(buffer))) {
+ output.write(buffer, 0, n);
+ count += n;
+ }
+ return count;
+ }
+
+ /**
+ * Copies some or all chars from a large (over 2GB) InputStream
to an
+ * OutputStream
, optionally skipping input chars.
+ *
+ * This method buffers the input internally, so there is no need to use a
+ * BufferedReader
.
+ *
+ * The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}.
+ *
+ * @param input the Reader
to read from
+ * @param output the Writer
to write to
+ * @param inputOffset : number of chars to skip from input before copying
+ * -ve values are ignored
+ * @param length : number of chars to copy. -ve means all
+ * @return the number of chars copied
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.2
+ */
+ public static long copyLarge(final Reader input, final Writer output, final long inputOffset, final long length)
+ throws IOException {
+ return copyLarge(input, output, inputOffset, length, new char[DEFAULT_BUFFER_SIZE]);
+ }
+
+ /**
+ * Copies some or all chars from a large (over 2GB) InputStream
to an
+ * OutputStream
, optionally skipping input chars.
+ *
+ * This method uses the provided buffer, so there is no need to use a
+ * BufferedReader
.
+ *
+ *
+ * @param input the Reader
to read from
+ * @param output the Writer
to write to
+ * @param inputOffset : number of chars to skip from input before copying
+ * -ve values are ignored
+ * @param length : number of chars to copy. -ve means all
+ * @param buffer the buffer to be used for the copy
+ * @return the number of chars copied
+ * @throws NullPointerException if the input or output is null
+ * @throws IOException if an I/O error occurs
+ * @since 2.2
+ */
+ public static long copyLarge(final Reader input, final Writer output, final long inputOffset, final long length,
+ final char[] buffer)
+ throws IOException {
+ if (inputOffset > 0) {
+ skipFully(input, inputOffset);
+ }
+ if (length == 0) {
+ return 0;
+ }
+ int bytesToRead = buffer.length;
+ if (length > 0 && length < buffer.length) {
+ bytesToRead = (int) length;
+ }
+ int read;
+ long totalRead = 0;
+ while (bytesToRead > 0 && EOF != (read = input.read(buffer, 0, bytesToRead))) {
+ output.write(buffer, 0, read);
+ totalRead += read;
+ if (length > 0) { // only adjust length if not reading to the end
+ // Note the cast must work because buffer.length is an integer
+ bytesToRead = (int) Math.min(length - totalRead, buffer.length);
+ }
+ }
+ return totalRead;
+ }
+
+ /**
+ * Skips bytes from an input byte stream.
+ * This implementation guarantees that it will read as many bytes
+ * as possible before giving up; this may not always be the case for
+ * skip() implementations in subclasses of {@link InputStream}.
+ *
+ * Note that the implementation uses {@link InputStream#read(byte[], int, int)} rather + * than delegating to {@link InputStream#skip(long)}. + * This means that the method may be considerably less efficient than using the actual skip implementation, + * this is done to guarantee that the correct number of bytes are skipped. + *
+ * + * @param input byte stream to skip + * @param toSkip number of bytes to skip. + * @return number of bytes actually skipped. + * @throws IOException if there is a problem reading the file + * @throws IllegalArgumentException if toSkip is negative + * @see InputStream#skip(long) + * @see IO-203 - Add skipFully() method for InputStreams + * @since 2.0 + */ + public static long skip(final InputStream input, final long toSkip) throws IOException { + if (toSkip < 0) { + throw new IllegalArgumentException("Skip count must be non-negative, actual: " + toSkip); + } + /* + * N.B. no need to synchronize access to SKIP_BYTE_BUFFER: - we don't care if the buffer is created multiple + * times (the data is ignored) - we always use the same size buffer, so if it it is recreated it will still be + * OK (if the buffer size were variable, we would need to synch. to ensure some other thread did not create a + * smaller one) + */ + long remain = toSkip; + while (remain > 0) { + // See https://issues.apache.org/jira/browse/IO-203 for why we use read() rather than delegating to skip() + final long n = input.read(SKIP_BYTE_BUFFER, 0, (int) Math.min(remain, SKIP_BYTE_BUFFER.length)); + if (n < 0) { // EOF + break; + } + remain -= n; + } + return toSkip - remain; + } + + /** + * Skips bytes from a ReadableByteChannel. + * This implementation guarantees that it will read as many bytes + * as possible before giving up. + * + * @param input ReadableByteChannel to skip + * @param toSkip number of bytes to skip. + * @return number of bytes actually skipped. + * @throws IOException if there is a problem reading the ReadableByteChannel + * @throws IllegalArgumentException if toSkip is negative + * @since 2.5 + */ + public static long skip(final ReadableByteChannel input, final long toSkip) throws IOException { + if (toSkip < 0) { + throw new IllegalArgumentException("Skip count must be non-negative, actual: " + toSkip); + } + final ByteBuffer skipByteBuffer = ByteBuffer.allocate((int) Math.min(toSkip, SKIP_BYTE_BUFFER.length)); + long remain = toSkip; + while (remain > 0) { + skipByteBuffer.position(0); + skipByteBuffer.limit((int) Math.min(remain, SKIP_BYTE_BUFFER.length)); + final int n = input.read(skipByteBuffer); + if (n == EOF) { + break; + } + remain -= n; + } + return toSkip - remain; + } + + /** + * Skips characters from an input character stream. + * This implementation guarantees that it will read as many characters + * as possible before giving up; this may not always be the case for + * skip() implementations in subclasses of {@link Reader}. + *+ * Note that the implementation uses {@link Reader#read(char[], int, int)} rather + * than delegating to {@link Reader#skip(long)}. + * This means that the method may be considerably less efficient than using the actual skip implementation, + * this is done to guarantee that the correct number of characters are skipped. + *
+ * + * @param input character stream to skip + * @param toSkip number of characters to skip. + * @return number of characters actually skipped. + * @throws IOException if there is a problem reading the file + * @throws IllegalArgumentException if toSkip is negative + * @see Reader#skip(long) + * @see IO-203 - Add skipFully() method for InputStreams + * @since 2.0 + */ + public static long skip(final Reader input, final long toSkip) throws IOException { + if (toSkip < 0) { + throw new IllegalArgumentException("Skip count must be non-negative, actual: " + toSkip); + } + /* + * N.B. no need to synchronize this because: - we don't care if the buffer is created multiple times (the data + * is ignored) - we always use the same size buffer, so if it it is recreated it will still be OK (if the buffer + * size were variable, we would need to synch. to ensure some other thread did not create a smaller one) + */ + if (SKIP_CHAR_BUFFER == null) { + SKIP_CHAR_BUFFER = new char[SKIP_BYTE_BUFFER.length]; + } + long remain = toSkip; + while (remain > 0) { + // See https://issues.apache.org/jira/browse/IO-203 for why we use read() rather than delegating to skip() + final long n = input.read(SKIP_CHAR_BUFFER, 0, (int) Math.min(remain, SKIP_BYTE_BUFFER.length)); + if (n < 0) { // EOF + break; + } + remain -= n; + } + return toSkip - remain; + } + + /** + * Skips the requested number of bytes or fail if there are not enough left. + *+ * This allows for the possibility that {@link InputStream#skip(long)} may + * not skip as many bytes as requested (most likely because of reaching EOF). + *
+ * Note that the implementation uses {@link #skip(InputStream, long)}. + * This means that the method may be considerably less efficient than using the actual skip implementation, + * this is done to guarantee that the correct number of characters are skipped. + *
+ * + * @param input stream to skip + * @param toSkip the number of bytes to skip + * @throws IOException if there is a problem reading the file + * @throws IllegalArgumentException if toSkip is negative + * @throws EOFException if the number of bytes skipped was incorrect + * @see InputStream#skip(long) + * @since 2.0 + */ + public static void skipFully(final InputStream input, final long toSkip) throws IOException { + if (toSkip < 0) { + throw new IllegalArgumentException("Bytes to skip must not be negative: " + toSkip); + } + final long skipped = skip(input, toSkip); + if (skipped != toSkip) { + throw new EOFException("Bytes to skip: " + toSkip + " actual: " + skipped); + } + } + + /** + * Skips the requested number of bytes or fail if there are not enough left. + * + * @param input ReadableByteChannel to skip + * @param toSkip the number of bytes to skip + * @throws IOException if there is a problem reading the ReadableByteChannel + * @throws IllegalArgumentException if toSkip is negative + * @throws EOFException if the number of bytes skipped was incorrect + * @since 2.5 + */ + public static void skipFully(final ReadableByteChannel input, final long toSkip) throws IOException { + if (toSkip < 0) { + throw new IllegalArgumentException("Bytes to skip must not be negative: " + toSkip); + } + final long skipped = skip(input, toSkip); + if (skipped != toSkip) { + throw new EOFException("Bytes to skip: " + toSkip + " actual: " + skipped); + } + } + + /** + * Skips the requested number of characters or fail if there are not enough left. + *+ * This allows for the possibility that {@link Reader#skip(long)} may + * not skip as many characters as requested (most likely because of reaching EOF). + *
+ * Note that the implementation uses {@link #skip(Reader, long)}. + * This means that the method may be considerably less efficient than using the actual skip implementation, + * this is done to guarantee that the correct number of characters are skipped. + *
+ * + * @param input stream to skip + * @param toSkip the number of characters to skip + * @throws IOException if there is a problem reading the file + * @throws IllegalArgumentException if toSkip is negative + * @throws EOFException if the number of characters skipped was incorrect + * @see Reader#skip(long) + * @since 2.0 + */ + public static void skipFully(final Reader input, final long toSkip) throws IOException { + final long skipped = skip(input, toSkip); + if (skipped != toSkip) { + throw new EOFException("Chars to skip: " + toSkip + " actual: " + skipped); + } + } + + /** + * Returns the length of the given array in a null-safe manner. + * + * @param array an array or null + * @return the array length -- or 0 if the given array is null. + * @since 2.7 + */ + public static int length(final byte[] array) { + return array == null ? 0 : array.length; + } + + /** + * Returns the length of the given array in a null-safe manner. + * + * @param array an array or null + * @return the array length -- or 0 if the given array is null. + * @since 2.7 + */ + public static int length(final char[] array) { + return array == null ? 0 : array.length; + } + + /** + * Returns the length of the given CharSequence in a null-safe manner. + * + * @param csq a CharSequence or null + * @return the CharSequence length -- or 0 if the given CharSequence is null. + * @since 2.7 + */ + public static int length(final CharSequence csq) { + return csq == null ? 0 : csq.length(); + } + + /** + * Returns the length of the given array in a null-safe manner. + * + * @param array an array or null + * @return the array length -- or 0 if the given array is null. + * @since 2.7 + */ + public static int length(final Object[] array) { + return array == null ? 0 : array.length; + } + + /** + * Closes the given {@link Closeable} as a null-safe operation. + * + * @param closeable The resource to close, may be null. + * @throws IOException if an I/O error occurs. + * @since 2.7 + */ + public static void close(final Closeable closeable) throws IOException { + if (closeable != null) { + closeable.close(); + } + } + + /** + * Closes the given {@link Closeable} as a null-safe operation. + * + * @param closeables The resource(s) to close, may be null. + * @throws IOException if an I/O error occurs. + * @since 2.8.0 + */ + public static void close(final Closeable... closeables) throws IOException { + if (closeables != null) { + for (final Closeable closeable : closeables) { + close(closeable); + } + } + } + + /** + * Closes the given {@link Closeable} as a null-safe operation. + * + * @param closeable The resource to close, may be null. + * @param consumer Consume the IOException thrown by {@link Closeable#close()}. + * @throws IOException if an I/O error occurs. + * @since 2.7 + */ + public static void close(final Closeable closeable, final IOConsumer- * BOMInputStream bomIn = new BOMInputStream(in); - * if (bomIn.hasBOM()) { - * // has a UTF-8 BOM - * } + * BOMInputStream bomIn = new BOMInputStream(in); + * if (bomIn.hasBOM()) { + * // has a UTF-8 BOM + * } ** - *
- * boolean include = true; - * BOMInputStream bomIn = new BOMInputStream(in, include); - * if (bomIn.hasBOM()) { - * // has a UTF-8 BOM - * } + * boolean include = true; + * BOMInputStream bomIn = new BOMInputStream(in, include); + * if (bomIn.hasBOM()) { + * // has a UTF-8 BOM + * } ** - *
- * BOMInputStream bomIn = new BOMInputStream(in, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE); - * if (bomIn.hasBOM() == false) { - * // No BOM found - * } else if (bomIn.hasBOM(ByteOrderMark.UTF_16LE)) { - * // has a UTF-16LE BOM - * } else if (bomIn.hasBOM(ByteOrderMark.UTF_16BE)) { - * // has a UTF-16BE BOM - * } + * BOMInputStream bomIn = new BOMInputStream(in, + * ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, + * ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE + * ); + * if (bomIn.hasBOM() == false) { + * // No BOM found + * } else if (bomIn.hasBOM(ByteOrderMark.UTF_16LE)) { + * // has a UTF-16LE BOM + * } else if (bomIn.hasBOM(ByteOrderMark.UTF_16BE)) { + * // has a UTF-16BE BOM + * } else if (bomIn.hasBOM(ByteOrderMark.UTF_32LE)) { + * // has a UTF-32LE BOM + * } else if (bomIn.hasBOM(ByteOrderMark.UTF_32BE)) { + * // has a UTF-32BE BOM + * } ** * @see ByteOrderMark * @see Wikipedia - Byte Order Mark - * @version $Revision: 1052095 $ $Date: 2010-12-22 23:03:20 +0000 (Wed, 22 Dec 2010) $ - * @since Commons IO 2.0 + * @since 2.0 */ public class BOMInputStream extends ProxyInputStream { private final boolean include; + /** + * BOMs are sorted from longest to shortest. + */ private final List
read()
method,
- * either returning a valid byte or -1 to indicate that the initial bytes
- * have been processed already.
+ * This method reads and either preserves or skips the first bytes in the stream. It behaves like the single-byte
+ * read()
method, either returning a valid byte or -1 to indicate that the initial bytes have been
+ * processed already.
+ *
* @return the byte read (excluding BOM) or -1 if the end of stream
- * @throws IOException if an I/O error occurs
+ * @throws IOException
+ * if an I/O error occurs
*/
private int readFirstBytes() throws IOException {
getBOM();
- return (fbIndex < fbLength) ? firstBytes[fbIndex++] : -1;
+ return fbIndex < fbLength ? firstBytes[fbIndex++] : EOF;
}
/**
@@ -224,7 +275,7 @@ public class BOMInputStream extends ProxyInputStream {
* @return The matched BOM or null if none matched
*/
private ByteOrderMark find() {
- for (ByteOrderMark bom : boms) {
+ for (final ByteOrderMark bom : boms) {
if (matches(bom)) {
return bom;
}
@@ -235,13 +286,15 @@ public class BOMInputStream extends ProxyInputStream {
/**
* Check if the bytes match a BOM.
*
- * @param bom The BOM
+ * @param bom
+ * The BOM
* @return true if the bytes match the bom, otherwise false
*/
- private boolean matches(ByteOrderMark bom) {
- if (bom.length() != fbLength) {
- return false;
- }
+ private boolean matches(final ByteOrderMark bom) {
+ // if (bom.length() != fbLength) {
+ // return false;
+ // }
+ // firstBytes may be bigger than the BOM bytes
for (int i = 0; i < bom.length(); i++) {
if (bom.get(i) != firstBytes[i]) {
return false;
@@ -250,36 +303,41 @@ public class BOMInputStream extends ProxyInputStream {
return true;
}
- //----------------------------------------------------------------------------
- // Implementation of InputStream
- //----------------------------------------------------------------------------
+ // ----------------------------------------------------------------------------
+ // Implementation of InputStream
+ // ----------------------------------------------------------------------------
/**
- * Invokes the delegate's read()
method, detecting and
- * optionally skipping BOM.
+ * Invokes the delegate's read()
method, detecting and optionally skipping BOM.
+ *
* @return the byte read (excluding BOM) or -1 if the end of stream
- * @throws IOException if an I/O error occurs
+ * @throws IOException
+ * if an I/O error occurs
*/
@Override
public int read() throws IOException {
- int b = readFirstBytes();
- return (b >= 0) ? b : in.read();
+ final int b = readFirstBytes();
+ return b >= 0 ? b : in.read();
}
/**
- * Invokes the delegate's read(byte[], int, int)
method, detecting
- * and optionally skipping BOM.
- * @param buf the buffer to read the bytes into
- * @param off The start offset
- * @param len The number of bytes to read (excluding BOM)
+ * Invokes the delegate's read(byte[], int, int)
method, detecting and optionally skipping BOM.
+ *
+ * @param buf
+ * the buffer to read the bytes into
+ * @param off
+ * The start offset
+ * @param len
+ * The number of bytes to read (excluding BOM)
* @return the number of bytes read or -1 if the end of stream
- * @throws IOException if an I/O error occurs
+ * @throws IOException
+ * if an I/O error occurs
*/
@Override
- public int read(byte[] buf, int off, int len) throws IOException {
+ public int read(final byte[] buf, int off, int len) throws IOException {
int firstCount = 0;
int b = 0;
- while ((len > 0) && (b >= 0)) {
+ while (len > 0 && b >= 0) {
b = readFirstBytes();
if (b >= 0) {
buf[off++] = (byte) (b & 0xFF);
@@ -287,37 +345,42 @@ public class BOMInputStream extends ProxyInputStream {
firstCount++;
}
}
- int secondCount = in.read(buf, off, len);
- return (secondCount < 0) ? (firstCount > 0 ? firstCount : -1) : firstCount + secondCount;
+ final int secondCount = in.read(buf, off, len);
+ return secondCount < 0 ? firstCount > 0 ? firstCount : EOF : firstCount + secondCount;
}
/**
- * Invokes the delegate's read(byte[])
method, detecting and
- * optionally skipping BOM.
- * @param buf the buffer to read the bytes into
- * @return the number of bytes read (excluding BOM)
- * or -1 if the end of stream
- * @throws IOException if an I/O error occurs
+ * Invokes the delegate's read(byte[])
method, detecting and optionally skipping BOM.
+ *
+ * @param buf
+ * the buffer to read the bytes into
+ * @return the number of bytes read (excluding BOM) or -1 if the end of stream
+ * @throws IOException
+ * if an I/O error occurs
*/
@Override
- public int read(byte[] buf) throws IOException {
+ public int read(final byte[] buf) throws IOException {
return read(buf, 0, buf.length);
}
/**
* Invokes the delegate's mark(int)
method.
- * @param readlimit read ahead limit
+ *
+ * @param readlimit
+ * read ahead limit
*/
@Override
- public synchronized void mark(int readlimit) {
+ public synchronized void mark(final int readlimit) {
markFbIndex = fbIndex;
- markedAtStart = (firstBytes == null);
+ markedAtStart = firstBytes == null;
in.mark(readlimit);
}
/**
* Invokes the delegate's reset()
method.
- * @throws IOException if an I/O error occurs
+ *
+ * @throws IOException
+ * if an I/O error occurs
*/
@Override
public synchronized void reset() throws IOException {
@@ -330,17 +393,20 @@ public class BOMInputStream extends ProxyInputStream {
}
/**
- * Invokes the delegate's skip(long)
method, detecting
- * and optionallyskipping BOM.
- * @param n the number of bytes to skip
+ * Invokes the delegate's skip(long)
method, detecting and optionally skipping BOM.
+ *
+ * @param n
+ * the number of bytes to skip
* @return the number of bytes to skipped or -1 if the end of stream
- * @throws IOException if an I/O error occurs
+ * @throws IOException
+ * if an I/O error occurs
*/
@Override
- public long skip(long n) throws IOException {
- while ((n > 0) && (readFirstBytes() >= 0)) {
- n--;
+ public long skip(final long n) throws IOException {
+ int skipped = 0;
+ while ((n > skipped) && (readFirstBytes() >= 0)) {
+ skipped++;
}
- return in.skip(n);
+ return in.skip(n - skipped) + skipped;
}
}
diff --git a/epublib/src/main/java/me/ag2s/epublib/util/commons/io/ByteOrderMark.java b/epublib/src/main/java/me/ag2s/epublib/util/commons/io/ByteOrderMark.java
index 5ec600f3b..3fdb6b8af 100644
--- a/epublib/src/main/java/me/ag2s/epublib/util/commons/io/ByteOrderMark.java
+++ b/epublib/src/main/java/me/ag2s/epublib/util/commons/io/ByteOrderMark.java
@@ -18,15 +18,16 @@ package me.ag2s.epublib.util.commons.io;
*/
import java.io.Serializable;
+import java.util.Locale;
/**
- * Byte Order Mark (BOM) representation -
- * see {@link BOMInputStream}.
+ * Byte Order Mark (BOM) representation - see {@link BOMInputStream}.
*
* @see BOMInputStream
- * @see Wikipedia - Byte Order Mark
- * @version $Id: ByteOrderMark.java 1005099 2010-10-06 16:13:01Z niallp $
- * @since Commons IO 2.0
+ * @see Wikipedia: Byte Order Mark
+ * @see W3C: Autodetection of Character Encodings
+ * (Non-Normative)
+ * @since 2.0
*/
public class ByteOrderMark implements Serializable {
@@ -34,11 +35,33 @@ public class ByteOrderMark implements Serializable {
/** UTF-8 BOM */
public static final ByteOrderMark UTF_8 = new ByteOrderMark("UTF-8", 0xEF, 0xBB, 0xBF);
- /** UTF-16BE BOM (Big Endian) */
+
+ /** UTF-16BE BOM (Big-Endian) */
public static final ByteOrderMark UTF_16BE = new ByteOrderMark("UTF-16BE", 0xFE, 0xFF);
- /** UTF-16LE BOM (Little Endian) */
+
+ /** UTF-16LE BOM (Little-Endian) */
public static final ByteOrderMark UTF_16LE = new ByteOrderMark("UTF-16LE", 0xFF, 0xFE);
+ /**
+ * UTF-32BE BOM (Big-Endian)
+ * @since 2.2
+ */
+ public static final ByteOrderMark UTF_32BE = new ByteOrderMark("UTF-32BE", 0x00, 0x00, 0xFE, 0xFF);
+
+ /**
+ * UTF-32LE BOM (Little-Endian)
+ * @since 2.2
+ */
+ public static final ByteOrderMark UTF_32LE = new ByteOrderMark("UTF-32LE", 0xFF, 0xFE, 0x00, 0x00);
+
+ /**
+ * Unicode BOM character; external form depends on the encoding.
+ * @see Byte Order Mark (BOM) FAQ
+ * @since 2.5
+ */
+ @SuppressWarnings("unused")
+ public static final char UTF_BOM = '\uFEFF';
+
private final String charsetName;
private final int[] bytes;
@@ -52,8 +75,8 @@ public class ByteOrderMark implements Serializable {
* @throws IllegalArgumentException if the bytes are null or zero
* length
*/
- public ByteOrderMark(String charsetName, int... bytes) {
- if (charsetName == null || charsetName.length() == 0) {
+ public ByteOrderMark(final String charsetName, final int... bytes) {
+ if (charsetName == null || charsetName.isEmpty()) {
throw new IllegalArgumentException("No charsetName specified");
}
if (bytes == null || bytes.length == 0) {
@@ -88,7 +111,7 @@ public class ByteOrderMark implements Serializable {
* @param pos The position
* @return The specified byte
*/
- public int get(int pos) {
+ public int get(final int pos) {
return bytes[pos];
}
@@ -98,7 +121,7 @@ public class ByteOrderMark implements Serializable {
* @return a copy of the BOM's bytes
*/
public byte[] getBytes() {
- byte[] copy = new byte[bytes.length];
+ final byte[] copy = new byte[bytes.length];
for (int i = 0; i < bytes.length; i++) {
copy[i] = (byte)bytes[i];
}
@@ -113,11 +136,11 @@ public class ByteOrderMark implements Serializable {
* false
*/
@Override
- public boolean equals(Object obj) {
+ public boolean equals(final Object obj) {
if (!(obj instanceof ByteOrderMark)) {
return false;
}
- ByteOrderMark bom = (ByteOrderMark)obj;
+ final ByteOrderMark bom = (ByteOrderMark)obj;
if (bytes.length != bom.length()) {
return false;
}
@@ -133,12 +156,12 @@ public class ByteOrderMark implements Serializable {
* Return the hashcode for this BOM.
*
* @return the hashcode for this BOM.
- * @see Object#hashCode()
+ * @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
int hashCode = getClass().hashCode();
- for (int b : bytes) {
+ for (final int b : bytes) {
hashCode += b;
}
return hashCode;
@@ -152,7 +175,7 @@ public class ByteOrderMark implements Serializable {
@Override
@SuppressWarnings("NullableProblems")
public String toString() {
- StringBuilder builder = new StringBuilder();
+ final StringBuilder builder = new StringBuilder();
builder.append(getClass().getSimpleName());
builder.append('[');
builder.append(charsetName);
@@ -162,10 +185,10 @@ public class ByteOrderMark implements Serializable {
builder.append(",");
}
builder.append("0x");
- builder.append(Integer.toHexString(0xFF & bytes[i]).toUpperCase());
+ builder.append(Integer.toHexString(0xFF & bytes[i]).toUpperCase(Locale.ROOT));
}
builder.append(']');
return builder.toString();
}
-}
+}
\ No newline at end of file
diff --git a/epublib/src/main/java/me/ag2s/epublib/util/commons/io/IOConsumer.java b/epublib/src/main/java/me/ag2s/epublib/util/commons/io/IOConsumer.java
new file mode 100644
index 000000000..b4e024f71
--- /dev/null
+++ b/epublib/src/main/java/me/ag2s/epublib/util/commons/io/IOConsumer.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package me.ag2s.epublib.util.commons.io;
+
+import java.io.IOException;
+import java.util.Objects;
+import java.util.function.Consumer;
+
+/**
+ * Like {@link Consumer} but throws {@link IOException}.
+ *
+ * @param * See the protected methods for ways in which a subclass can easily decorate * a stream with custom pre-, post- or error processing functionality. - * - * @author Stephen Colebourne - * @version $Id: ProxyInputStream.java 934041 2010-04-14 17:37:24Z jukka $ + *
*/ public abstract class ProxyInputStream extends FilterInputStream { /** * Constructs a new ProxyInputStream. * - * @param proxy the InputStream to delegate to + * @param proxy the InputStream to delegate to */ - public ProxyInputStream(InputStream proxy) { + public ProxyInputStream(final InputStream proxy) { super(proxy); // the proxy is stored in a protected superclass variable named 'in' } /** * Invokes the delegate'sread()
method.
+ *
* @return the byte read or -1 if the end of stream
* @throws IOException if an I/O error occurs
*/
@@ -57,36 +63,38 @@ public abstract class ProxyInputStream extends FilterInputStream {
public int read() throws IOException {
try {
beforeRead(1);
- int b = in.read();
- afterRead(b != -1 ? 1 : -1);
+ final int b = in.read();
+ afterRead(b != EOF ? 1 : EOF);
return b;
- } catch (IOException e) {
+ } catch (final IOException e) {
handleIOException(e);
- return -1;
+ return EOF;
}
}
/**
* Invokes the delegate's read(byte[])
method.
+ *
* @param bts the buffer to read the bytes into
- * @return the number of bytes read or -1 if the end of stream
+ * @return the number of bytes read or EOF if the end of stream
* @throws IOException if an I/O error occurs
*/
@Override
- public int read(byte[] bts) throws IOException {
+ public int read(final byte[] bts) throws IOException {
try {
- beforeRead(bts != null ? bts.length : 0);
- int n = in.read(bts);
+ beforeRead(IOUtil.length(bts));
+ final int n = in.read(bts);
afterRead(n);
return n;
- } catch (IOException e) {
+ } catch (final IOException e) {
handleIOException(e);
- return -1;
+ return EOF;
}
}
/**
* Invokes the delegate's read(byte[], int, int)
method.
+ *
* @param bts the buffer to read the bytes into
* @param off The start offset
* @param len The number of bytes to read
@@ -94,29 +102,30 @@ public abstract class ProxyInputStream extends FilterInputStream {
* @throws IOException if an I/O error occurs
*/
@Override
- public int read(byte[] bts, int off, int len) throws IOException {
+ public int read(final byte[] bts, final int off, final int len) throws IOException {
try {
beforeRead(len);
- int n = in.read(bts, off, len);
+ final int n = in.read(bts, off, len);
afterRead(n);
return n;
- } catch (IOException e) {
+ } catch (final IOException e) {
handleIOException(e);
- return -1;
+ return EOF;
}
}
/**
* Invokes the delegate's skip(long)
method.
+ *
* @param ln the number of bytes to skip
* @return the actual number of bytes skipped
* @throws IOException if an I/O error occurs
*/
@Override
- public long skip(long ln) throws IOException {
+ public long skip(final long ln) throws IOException {
try {
return in.skip(ln);
- } catch (IOException e) {
+ } catch (final IOException e) {
handleIOException(e);
return 0;
}
@@ -124,6 +133,7 @@ public abstract class ProxyInputStream extends FilterInputStream {
/**
* Invokes the delegate's available()
method.
+ *
* @return the number of available bytes
* @throws IOException if an I/O error occurs
*/
@@ -131,7 +141,7 @@ public abstract class ProxyInputStream extends FilterInputStream {
public int available() throws IOException {
try {
return super.available();
- } catch (IOException e) {
+ } catch (final IOException e) {
handleIOException(e);
return 0;
}
@@ -139,41 +149,41 @@ public abstract class ProxyInputStream extends FilterInputStream {
/**
* Invokes the delegate's close()
method.
+ *
* @throws IOException if an I/O error occurs
*/
@Override
public void close() throws IOException {
- try {
- in.close();
- } catch (IOException e) {
- handleIOException(e);
- }
+ IOUtil.close(in, this::handleIOException);
}
/**
* Invokes the delegate's mark(int)
method.
+ *
* @param readlimit read ahead limit
*/
@Override
- public synchronized void mark(int readlimit) {
+ public synchronized void mark(final int readlimit) {
in.mark(readlimit);
}
/**
* Invokes the delegate's reset()
method.
+ *
* @throws IOException if an I/O error occurs
*/
@Override
public synchronized void reset() throws IOException {
try {
in.reset();
- } catch (IOException e) {
+ } catch (final IOException e) {
handleIOException(e);
}
}
/**
* Invokes the delegate's markSupported()
method.
+ *
* @return true if mark is supported, otherwise false
*/
@Override
@@ -195,11 +205,13 @@ public abstract class ProxyInputStream extends FilterInputStream {
* {@link #reset()}. You need to explicitly override those methods if
* you want to add pre-processing steps also to them.
*
- * @since Commons IO 2.0
* @param n number of bytes that the caller asked to be read
+ * @since 2.0
*/
@SuppressWarnings("unused")
- protected void beforeRead(int n) {
+
+ protected void beforeRead(final int n) {
+ // no-op
}
/**
@@ -215,23 +227,25 @@ public abstract class ProxyInputStream extends FilterInputStream {
* {@link #reset()}. You need to explicitly override those methods if
* you want to add post-processing steps also to them.
*
- * @since Commons IO 2.0
* @param n number of bytes read, or -1 if the end of stream was reached
+ * @since 2.0
*/
@SuppressWarnings("unused")
- protected void afterRead(int n) {
+ protected void afterRead(final int n) {
+ // no-op
}
/**
* Handle any IOExceptions thrown.
* * This method provides a point to implement custom exception - * handling. The default behaviour is to re-throw the exception. + * handling. The default behavior is to re-throw the exception. + * * @param e The IOException thrown * @throws IOException if an I/O error occurs - * @since Commons IO 2.0 + * @since 2.0 */ - protected void handleIOException(IOException e) throws IOException { + protected void handleIOException(final IOException e) throws IOException { throw e; } diff --git a/epublib/src/main/java/me/ag2s/epublib/util/commons/io/XmlStreamReader.java b/epublib/src/main/java/me/ag2s/epublib/util/commons/io/XmlStreamReader.java index bd0980932..3499354be 100644 --- a/epublib/src/main/java/me/ag2s/epublib/util/commons/io/XmlStreamReader.java +++ b/epublib/src/main/java/me/ag2s/epublib/util/commons/io/XmlStreamReader.java @@ -30,42 +30,48 @@ import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.text.MessageFormat; +import java.util.Locale; import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; +import me.ag2s.epublib.util.IOUtil; + /** - * Character stream that handles all the necessary Voodo to figure out the + * Character stream that handles all the necessary Voodoo to figure out the * charset encoding of the XML document within the stream. *
* IMPORTANT: This class is not related in any way to the org.xml.sax.XMLReader. * This one IS a character stream. + *
** All this has to be done without consuming characters from the stream, if not * the XML parser will not recognized the document as a valid XML. This is not * 100% true, but it's close enough (UTF-8 BOM is not handled by all parsers * right now, XmlStreamReader handles it and things work in all parsers). + *
** The XmlStreamReader class handles the charset encoding of XML documents in * Files, raw streams and HTTP streams by offering a wide set of constructors. + *
** By default the charset encoding detection is lenient, the constructor with - * the lenient flag can be used for an script (following HTTP MIME and XML + * the lenient flag can be used for a script (following HTTP MIME and XML * specifications). All this is nicely explained by Mark Pilgrim in his blog, * Determining the character encoding of a feed. + *
** Originally developed for ROME under * Apache License 2.0. + *
* - * @author Alejandro Abdelnur - * @version $Id: XmlStreamReader.java 1052161 2010-12-23 03:12:09Z niallp $ - * @see "org.apache.commons.io.output.XmlStreamWriter" - * @since Commons IO 2.0 + * //@seerr XmlStreamWriter + * @since 2.0 */ public class XmlStreamReader extends Reader { - private static final int BUFFER_SIZE = 4096; + private static final int BUFFER_SIZE = IOUtil.DEFAULT_BUFFER_SIZE; private static final String UTF_8 = "UTF-8"; @@ -75,23 +81,36 @@ public class XmlStreamReader extends Reader { private static final String UTF_16LE = "UTF-16LE"; + private static final String UTF_32BE = "UTF-32BE"; + + private static final String UTF_32LE = "UTF-32LE"; + private static final String UTF_16 = "UTF-16"; + private static final String UTF_32 = "UTF-32"; + private static final String EBCDIC = "CP1047"; private static final ByteOrderMark[] BOMS = new ByteOrderMark[] { - ByteOrderMark.UTF_8, - ByteOrderMark.UTF_16BE, - ByteOrderMark.UTF_16LE + ByteOrderMark.UTF_8, + ByteOrderMark.UTF_16BE, + ByteOrderMark.UTF_16LE, + ByteOrderMark.UTF_32BE, + ByteOrderMark.UTF_32LE }; + + // UTF_16LE and UTF_32LE have the same two starting BOM bytes. private static final ByteOrderMark[] XML_GUESS_BYTES = new ByteOrderMark[] { - new ByteOrderMark(UTF_8, 0x3C, 0x3F, 0x78, 0x6D), - new ByteOrderMark(UTF_16BE, 0x00, 0x3C, 0x00, 0x3F), - new ByteOrderMark(UTF_16LE, 0x3C, 0x00, 0x3F, 0x00), - new ByteOrderMark(EBCDIC, 0x4C, 0x6F, 0xA7, 0x94) + new ByteOrderMark(UTF_8, 0x3C, 0x3F, 0x78, 0x6D), + new ByteOrderMark(UTF_16BE, 0x00, 0x3C, 0x00, 0x3F), + new ByteOrderMark(UTF_16LE, 0x3C, 0x00, 0x3F, 0x00), + new ByteOrderMark(UTF_32BE, 0x00, 0x00, 0x00, 0x3C, + 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x6D), + new ByteOrderMark(UTF_32LE, 0x3C, 0x00, 0x00, 0x00, + 0x3F, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x6D, 0x00, 0x00, 0x00), + new ByteOrderMark(EBCDIC, 0x4C, 0x6F, 0xA7, 0x94) }; - private final Reader reader; private final String encoding; @@ -106,7 +125,6 @@ public class XmlStreamReader extends Reader { * * @return the default encoding to use. */ - @SuppressWarnings("unused") public String getDefaultEncoding() { return defaultEncoding; } @@ -124,8 +142,8 @@ public class XmlStreamReader extends Reader { * @throws IOException thrown if there is a problem reading the file. */ @SuppressWarnings("unused") - public XmlStreamReader(File file) throws IOException { - this(new FileInputStream(file)); + public XmlStreamReader(final File file) throws IOException { + this(new FileInputStream(Objects.requireNonNull(file))); } /** @@ -136,11 +154,11 @@ public class XmlStreamReader extends Reader { * It does a lenient charset encoding detection, check the constructor with * the lenient parameter for details. * - * @param is InputStream to create a Reader from. + * @param inputStream InputStream to create a Reader from. * @throws IOException thrown if there is a problem reading the stream. */ - public XmlStreamReader(InputStream is) throws IOException { - this(is, true); + public XmlStreamReader(final InputStream inputStream) throws IOException { + this(inputStream, true); } /** @@ -163,15 +181,15 @@ public class XmlStreamReader extends Reader { * If lenient detection is indicated an XmlStreamReaderException is never * thrown. * - * @param is InputStream to create a Reader from. + * @param inputStream InputStream to create a Reader from. * @param lenient indicates if the charset encoding detection should be * relaxed. * @throws IOException thrown if there is a problem reading the stream. * @throws XmlStreamReaderException thrown if the charset encoding could not * be determined according to the specs. */ - public XmlStreamReader(InputStream is, boolean lenient) throws IOException { - this(is, lenient, null); + public XmlStreamReader(final InputStream inputStream, final boolean lenient) throws IOException { + this(inputStream, lenient, null); } /** @@ -194,7 +212,7 @@ public class XmlStreamReader extends Reader { * If lenient detection is indicated an XmlStreamReaderException is never * thrown. * - * @param is InputStream to create a Reader from. + * @param inputStream InputStream to create a Reader from. * @param lenient indicates if the charset encoding detection should be * relaxed. * @param defaultEncoding The default encoding @@ -202,10 +220,12 @@ public class XmlStreamReader extends Reader { * @throws XmlStreamReaderException thrown if the charset encoding could not * be determined according to the specs. */ - public XmlStreamReader(InputStream is, boolean lenient, String defaultEncoding) throws IOException { + public XmlStreamReader(final InputStream inputStream, final boolean lenient, final String defaultEncoding) + throws IOException { + Objects.requireNonNull(inputStream, "inputStream"); this.defaultEncoding = defaultEncoding; - BOMInputStream bom = new BOMInputStream(new BufferedInputStream(is, BUFFER_SIZE), false, BOMS); - BOMInputStream pis = new BOMInputStream(bom, true, XML_GUESS_BYTES); + final BOMInputStream bom = new BOMInputStream(new BufferedInputStream(inputStream, BUFFER_SIZE), false, BOMS); + final BOMInputStream pis = new BOMInputStream(bom, true, XML_GUESS_BYTES); this.encoding = doRawStream(bom, pis, lenient); this.reader = new InputStreamReader(pis, encoding); } @@ -228,8 +248,8 @@ public class XmlStreamReader extends Reader { * the URL. */ @SuppressWarnings("unused") - public XmlStreamReader(URL url) throws IOException { - this(url.openConnection(), null); + public XmlStreamReader(final URL url) throws IOException { + this(Objects.requireNonNull(url, "url").openConnection(), null); } /** @@ -251,24 +271,24 @@ public class XmlStreamReader extends Reader { * @throws IOException thrown if there is a problem reading the stream of * the URLConnection. */ - public XmlStreamReader(URLConnection conn, String defaultEncoding) throws IOException { + public XmlStreamReader(final URLConnection conn, final String defaultEncoding) throws IOException { + Objects.requireNonNull(conn, "conm"); this.defaultEncoding = defaultEncoding; - @SuppressWarnings("unused") - boolean lenient = true; - String contentType = conn.getContentType(); - InputStream is = conn.getInputStream(); - BOMInputStream bom = new BOMInputStream(new BufferedInputStream(is, BUFFER_SIZE), false, BOMS); - BOMInputStream pis = new BOMInputStream(bom, true, XML_GUESS_BYTES); + final boolean lenient = true; + final String contentType = conn.getContentType(); + final InputStream inputStream = conn.getInputStream(); + final BOMInputStream bom = new BOMInputStream(new BufferedInputStream(inputStream, BUFFER_SIZE), false, BOMS); + final BOMInputStream pis = new BOMInputStream(bom, true, XML_GUESS_BYTES); if (conn instanceof HttpURLConnection || contentType != null) { - this.encoding = doHttpStream(bom, pis, contentType, true); + this.encoding = processHttpStream(bom, pis, contentType, lenient); } else { - this.encoding = doRawStream(bom, pis, true); + this.encoding = doRawStream(bom, pis, lenient); } this.reader = new InputStreamReader(pis, encoding); } /** - * Creates a Reader using an InputStream an the associated content-type + * Creates a Reader using an InputStream and the associated content-type * header. ** First it checks if the stream has BOM. If there is not BOM checks the @@ -279,18 +299,18 @@ public class XmlStreamReader extends Reader { * It does a lenient charset encoding detection, check the constructor with * the lenient parameter for details. * - * @param is InputStream to create the reader from. + * @param inputStream InputStream to create the reader from. * @param httpContentType content-type header to use for the resolution of * the charset encoding. * @throws IOException thrown if there is a problem reading the file. */ - public XmlStreamReader(InputStream is, String httpContentType) + public XmlStreamReader(final InputStream inputStream, final String httpContentType) throws IOException { - this(is, httpContentType, true); + this(inputStream, httpContentType, true); } /** - * Creates a Reader using an InputStream an the associated content-type + * Creates a Reader using an InputStream and the associated content-type * header. This constructor is lenient regarding the encoding detection. *
* First it checks if the stream has BOM. If there is not BOM checks the @@ -313,7 +333,7 @@ public class XmlStreamReader extends Reader { * If lenient detection is indicated an XmlStreamReaderException is never * thrown. * - * @param is InputStream to create the reader from. + * @param inputStream InputStream to create the reader from. * @param httpContentType content-type header to use for the resolution of * the charset encoding. * @param lenient indicates if the charset encoding detection should be @@ -323,17 +343,18 @@ public class XmlStreamReader extends Reader { * @throws XmlStreamReaderException thrown if the charset encoding could not * be determined according to the specs. */ - public XmlStreamReader(InputStream is, String httpContentType, - boolean lenient, String defaultEncoding) throws IOException { + public XmlStreamReader(final InputStream inputStream, final String httpContentType, + final boolean lenient, final String defaultEncoding) throws IOException { + Objects.requireNonNull(inputStream, "inputStream"); this.defaultEncoding = defaultEncoding; - BOMInputStream bom = new BOMInputStream(new BufferedInputStream(is, BUFFER_SIZE), false, BOMS); - BOMInputStream pis = new BOMInputStream(bom, true, XML_GUESS_BYTES); - this.encoding = doHttpStream(bom, pis, httpContentType, lenient); + final BOMInputStream bom = new BOMInputStream(new BufferedInputStream(inputStream, BUFFER_SIZE), false, BOMS); + final BOMInputStream pis = new BOMInputStream(bom, true, XML_GUESS_BYTES); + this.encoding = processHttpStream(bom, pis, httpContentType, lenient); this.reader = new InputStreamReader(pis, encoding); } /** - * Creates a Reader using an InputStream an the associated content-type + * Creates a Reader using an InputStream and the associated content-type * header. This constructor is lenient regarding the encoding detection. *
* First it checks if the stream has BOM. If there is not BOM checks the @@ -356,7 +377,7 @@ public class XmlStreamReader extends Reader { * If lenient detection is indicated an XmlStreamReaderException is never * thrown. * - * @param is InputStream to create the reader from. + * @param inputStream InputStream to create the reader from. * @param httpContentType content-type header to use for the resolution of * the charset encoding. * @param lenient indicates if the charset encoding detection should be @@ -365,9 +386,9 @@ public class XmlStreamReader extends Reader { * @throws XmlStreamReaderException thrown if the charset encoding could not * be determined according to the specs. */ - public XmlStreamReader(InputStream is, String httpContentType, - boolean lenient) throws IOException { - this(is, httpContentType, lenient, null); + public XmlStreamReader(final InputStream inputStream, final String httpContentType, + final boolean lenient) throws IOException { + this(inputStream, httpContentType, lenient, null); } /** @@ -388,7 +409,7 @@ public class XmlStreamReader extends Reader { * @throws IOException if an I/O error occurs */ @Override - public int read(char[] buf, int offset, int len) throws IOException { + public int read(final char[] buf, final int offset, final int len) throws IOException { return reader.read(buf, offset, len); } @@ -412,19 +433,18 @@ public class XmlStreamReader extends Reader { * @return the encoding to be used * @throws IOException thrown if there is a problem reading the stream. */ - private String doRawStream(BOMInputStream bom, BOMInputStream pis, boolean lenient) + private String doRawStream(final BOMInputStream bom, final BOMInputStream pis, final boolean lenient) throws IOException { - String bomEnc = bom.getBOMCharsetName(); - String xmlGuessEnc = pis.getBOMCharsetName(); - String xmlEnc = getXmlProlog(pis, xmlGuessEnc); + final String bomEnc = bom.getBOMCharsetName(); + final String xmlGuessEnc = pis.getBOMCharsetName(); + final String xmlEnc = getXmlProlog(pis, xmlGuessEnc); try { return calculateRawEncoding(bomEnc, xmlGuessEnc, xmlEnc); - } catch (XmlStreamReaderException ex) { + } catch (final XmlStreamReaderException ex) { if (lenient) { return doLenientDetection(null, ex); - } else { - throw ex; } + throw ex; } } @@ -439,20 +459,18 @@ public class XmlStreamReader extends Reader { * @return the encoding to be used * @throws IOException thrown if there is a problem reading the stream. */ - private String doHttpStream(BOMInputStream bom, BOMInputStream pis, String httpContentType, - boolean lenient) throws IOException { - String bomEnc = bom.getBOMCharsetName(); - String xmlGuessEnc = pis.getBOMCharsetName(); - String xmlEnc = getXmlProlog(pis, xmlGuessEnc); + private String processHttpStream(final BOMInputStream bom, final BOMInputStream pis, final String httpContentType, + final boolean lenient) throws IOException { + final String bomEnc = bom.getBOMCharsetName(); + final String xmlGuessEnc = pis.getBOMCharsetName(); + final String xmlEnc = getXmlProlog(pis, xmlGuessEnc); try { - return calculateHttpEncoding(httpContentType, bomEnc, - xmlGuessEnc, xmlEnc, lenient); - } catch (XmlStreamReaderException ex) { + return calculateHttpEncoding(httpContentType, bomEnc, xmlGuessEnc, xmlEnc, lenient); + } catch (final XmlStreamReaderException ex) { if (lenient) { return doLenientDetection(httpContentType, ex); - } else { - throw ex; } + throw ex; } } @@ -466,14 +484,14 @@ public class XmlStreamReader extends Reader { * @throws IOException thrown if there is a problem reading the stream. */ private String doLenientDetection(String httpContentType, - XmlStreamReaderException ex) throws IOException { + XmlStreamReaderException ex) throws IOException { if (httpContentType != null && httpContentType.startsWith("text/html")) { httpContentType = httpContentType.substring("text/html".length()); httpContentType = "text/xml" + httpContentType; try { return calculateHttpEncoding(httpContentType, ex.getBomEncoding(), ex.getXmlGuessEncoding(), ex.getXmlEncoding(), true); - } catch (XmlStreamReaderException ex2) { + } catch (final XmlStreamReaderException ex2) { ex = ex2; } } @@ -482,7 +500,7 @@ public class XmlStreamReader extends Reader { encoding = ex.getContentTypeEncoding(); } if (encoding == null) { - encoding = (defaultEncoding == null) ? UTF_8 : defaultEncoding; + encoding = defaultEncoding == null ? UTF_8 : defaultEncoding; } return encoding; } @@ -496,16 +514,16 @@ public class XmlStreamReader extends Reader { * @return the raw encoding * @throws IOException thrown if there is a problem reading the stream. */ - String calculateRawEncoding(String bomEnc, String xmlGuessEnc, - String xmlEnc) throws IOException { + String calculateRawEncoding(final String bomEnc, final String xmlGuessEnc, + final String xmlEnc) throws IOException { // BOM is Null if (bomEnc == null) { if (xmlGuessEnc == null || xmlEnc == null) { - return (defaultEncoding == null ? UTF_8 : defaultEncoding); + return defaultEncoding == null ? UTF_8 : defaultEncoding; } if (xmlEnc.equals(UTF_16) && - (xmlGuessEnc.equals(UTF_16BE) || xmlGuessEnc.equals(UTF_16LE))) { + (xmlGuessEnc.equals(UTF_16BE) || xmlGuessEnc.equals(UTF_16LE))) { return xmlGuessEnc; } return xmlEnc; @@ -514,11 +532,11 @@ public class XmlStreamReader extends Reader { // BOM is UTF-8 if (bomEnc.equals(UTF_8)) { if (xmlGuessEnc != null && !xmlGuessEnc.equals(UTF_8)) { - String msg = MessageFormat.format(RAW_EX_1, bomEnc, xmlGuessEnc, xmlEnc); + final String msg = MessageFormat.format(RAW_EX_1, bomEnc, xmlGuessEnc, xmlEnc); throw new XmlStreamReaderException(msg, bomEnc, xmlGuessEnc, xmlEnc); } if (xmlEnc != null && !xmlEnc.equals(UTF_8)) { - String msg = MessageFormat.format(RAW_EX_1, bomEnc, xmlGuessEnc, xmlEnc); + final String msg = MessageFormat.format(RAW_EX_1, bomEnc, xmlGuessEnc, xmlEnc); throw new XmlStreamReaderException(msg, bomEnc, xmlGuessEnc, xmlEnc); } return bomEnc; @@ -527,18 +545,31 @@ public class XmlStreamReader extends Reader { // BOM is UTF-16BE or UTF-16LE if (bomEnc.equals(UTF_16BE) || bomEnc.equals(UTF_16LE)) { if (xmlGuessEnc != null && !xmlGuessEnc.equals(bomEnc)) { - String msg = MessageFormat.format(RAW_EX_1, bomEnc, xmlGuessEnc, xmlEnc); + final String msg = MessageFormat.format(RAW_EX_1, bomEnc, xmlGuessEnc, xmlEnc); throw new XmlStreamReaderException(msg, bomEnc, xmlGuessEnc, xmlEnc); } if (xmlEnc != null && !xmlEnc.equals(UTF_16) && !xmlEnc.equals(bomEnc)) { - String msg = MessageFormat.format(RAW_EX_1, bomEnc, xmlGuessEnc, xmlEnc); + final String msg = MessageFormat.format(RAW_EX_1, bomEnc, xmlGuessEnc, xmlEnc); + throw new XmlStreamReaderException(msg, bomEnc, xmlGuessEnc, xmlEnc); + } + return bomEnc; + } + + // BOM is UTF-32BE or UTF-32LE + if (bomEnc.equals(UTF_32BE) || bomEnc.equals(UTF_32LE)) { + if (xmlGuessEnc != null && !xmlGuessEnc.equals(bomEnc)) { + final String msg = MessageFormat.format(RAW_EX_1, bomEnc, xmlGuessEnc, xmlEnc); + throw new XmlStreamReaderException(msg, bomEnc, xmlGuessEnc, xmlEnc); + } + if (xmlEnc != null && !xmlEnc.equals(UTF_32) && !xmlEnc.equals(bomEnc)) { + final String msg = MessageFormat.format(RAW_EX_1, bomEnc, xmlGuessEnc, xmlEnc); throw new XmlStreamReaderException(msg, bomEnc, xmlGuessEnc, xmlEnc); } return bomEnc; } // BOM is something else - String msg = MessageFormat.format(RAW_EX_2, bomEnc, xmlGuessEnc, xmlEnc); + final String msg = MessageFormat.format(RAW_EX_2, bomEnc, xmlGuessEnc, xmlEnc); throw new XmlStreamReaderException(msg, bomEnc, xmlGuessEnc, xmlEnc); } @@ -555,9 +586,9 @@ public class XmlStreamReader extends Reader { * @return the HTTP encoding * @throws IOException thrown if there is a problem reading the stream. */ - String calculateHttpEncoding(String httpContentType, - String bomEnc, String xmlGuessEnc, String xmlEnc, - boolean lenient) throws IOException { + String calculateHttpEncoding(final String httpContentType, + final String bomEnc, final String xmlGuessEnc, final String xmlEnc, + final boolean lenient) throws IOException { // Lenient and has XML encoding if (lenient && xmlEnc != null) { @@ -565,14 +596,14 @@ public class XmlStreamReader extends Reader { } // Determine mime/encoding content types from HTTP Content Type - String cTMime = getContentTypeMime(httpContentType); - String cTEnc = getContentTypeEncoding(httpContentType); - boolean appXml = isAppXml(cTMime); - boolean textXml = isTextXml(cTMime); + final String cTMime = getContentTypeMime(httpContentType); + final String cTEnc = getContentTypeEncoding(httpContentType); + final boolean appXml = isAppXml(cTMime); + final boolean textXml = isTextXml(cTMime); // Mime type NOT "application/xml" or "text/xml" if (!appXml && !textXml) { - String msg = MessageFormat.format(HTTP_EX_3, cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc); + final String msg = MessageFormat.format(HTTP_EX_3, cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc); throw new XmlStreamReaderException(msg, cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc); } @@ -580,15 +611,14 @@ public class XmlStreamReader extends Reader { if (cTEnc == null) { if (appXml) { return calculateRawEncoding(bomEnc, xmlGuessEnc, xmlEnc); - } else { - return (defaultEncoding == null) ? US_ASCII : defaultEncoding; } + return defaultEncoding == null ? US_ASCII : defaultEncoding; } // UTF-16BE or UTF-16LE content type encoding if (cTEnc.equals(UTF_16BE) || cTEnc.equals(UTF_16LE)) { if (bomEnc != null) { - String msg = MessageFormat.format(HTTP_EX_1, cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc); + final String msg = MessageFormat.format(HTTP_EX_1, cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc); throw new XmlStreamReaderException(msg, cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc); } return cTEnc; @@ -599,7 +629,25 @@ public class XmlStreamReader extends Reader { if (bomEnc != null && bomEnc.startsWith(UTF_16)) { return bomEnc; } - String msg = MessageFormat.format(HTTP_EX_2, cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc); + final String msg = MessageFormat.format(HTTP_EX_2, cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc); + throw new XmlStreamReaderException(msg, cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc); + } + + // UTF-32BE or UTF-132E content type encoding + if (cTEnc.equals(UTF_32BE) || cTEnc.equals(UTF_32LE)) { + if (bomEnc != null) { + final String msg = MessageFormat.format(HTTP_EX_1, cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc); + throw new XmlStreamReaderException(msg, cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc); + } + return cTEnc; + } + + // UTF-32 content type encoding + if (cTEnc.equals(UTF_32)) { + if (bomEnc != null && bomEnc.startsWith(UTF_32)) { + return bomEnc; + } + final String msg = MessageFormat.format(HTTP_EX_2, cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc); throw new XmlStreamReaderException(msg, cTMime, cTEnc, bomEnc, xmlGuessEnc, xmlEnc); } @@ -612,10 +660,10 @@ public class XmlStreamReader extends Reader { * @param httpContentType the HTTP content type * @return The mime content type */ - static String getContentTypeMime(String httpContentType) { + static String getContentTypeMime(final String httpContentType) { String mime = null; if (httpContentType != null) { - int i = httpContentType.indexOf(";"); + final int i = httpContentType.indexOf(";"); if (i >= 0) { mime = httpContentType.substring(0, i); } else { @@ -634,22 +682,25 @@ public class XmlStreamReader extends Reader { * httpContentType is NULL. * * @param httpContentType the HTTP content type - * @return The content type encoding + * @return The content type encoding (upcased) */ - static String getContentTypeEncoding(String httpContentType) { + static String getContentTypeEncoding(final String httpContentType) { String encoding = null; if (httpContentType != null) { - int i = httpContentType.indexOf(";"); + final int i = httpContentType.indexOf(";"); if (i > -1) { - String postMime = httpContentType.substring(i + 1); - Matcher m = CHARSET_PATTERN.matcher(postMime); - encoding = (m.find()) ? m.group(1) : null; - encoding = (encoding != null) ? encoding.toUpperCase() : null; + final String postMime = httpContentType.substring(i + 1); + final Matcher m = CHARSET_PATTERN.matcher(postMime); + encoding = m.find() ? m.group(1) : null; + encoding = encoding != null ? encoding.toUpperCase(Locale.ROOT) : null; } } return encoding; } + /** + * Pattern capturing the encoding of the "xml" processing instruction. + */ public static final Pattern ENCODING_PATTERN = Pattern.compile( "<\\?xml.*encoding[\\s]*=[\\s]*((?:\".[^\"]*\")|(?:'.[^']*'))", Pattern.MULTILINE); @@ -657,52 +708,50 @@ public class XmlStreamReader extends Reader { /** * Returns the encoding declared in the , NULL if none. * - * @param is InputStream to create the reader from. + * @param inputStream InputStream to create the reader from. * @param guessedEnc guessed encoding * @return the encoding declared in the * @throws IOException thrown if there is a problem reading the stream. */ - private static String getXmlProlog(InputStream is, String guessedEnc) + private static String getXmlProlog(final InputStream inputStream, final String guessedEnc) throws IOException { String encoding = null; if (guessedEnc != null) { - byte[] bytes = new byte[BUFFER_SIZE]; - is.mark(BUFFER_SIZE); + final byte[] bytes = new byte[BUFFER_SIZE]; + inputStream.mark(BUFFER_SIZE); int offset = 0; int max = BUFFER_SIZE; - int c = is.read(bytes, offset, max); + int c = inputStream.read(bytes, offset, max); int firstGT = -1; - String xmlProlog = null; + String xmlProlog = ""; // avoid possible NPE warning (cannot happen; this just silences the warning) while (c != -1 && firstGT == -1 && offset < BUFFER_SIZE) { offset += c; max -= c; - c = is.read(bytes, offset, max); + c = inputStream.read(bytes, offset, max); xmlProlog = new String(bytes, 0, offset, guessedEnc); firstGT = xmlProlog.indexOf('>'); } if (firstGT == -1) { if (c == -1) { throw new IOException("Unexpected end of XML stream"); - } else { - throw new IOException( - "XML prolog or ROOT element not found on first " - + offset + " bytes"); } + throw new IOException( + "XML prolog or ROOT element not found on first " + + offset + " bytes"); } - int bytesRead = offset; + final int bytesRead = offset; if (bytesRead > 0) { - is.reset(); - BufferedReader bReader = new BufferedReader(new StringReader( + inputStream.reset(); + final BufferedReader bReader = new BufferedReader(new StringReader( xmlProlog.substring(0, firstGT + 1))); - StringBuilder prolog = new StringBuilder(); - String line = bReader.readLine(); - while (line != null) { + final StringBuffer prolog = new StringBuffer(); + String line; + while ((line = bReader.readLine()) != null) { prolog.append(line); - line = bReader.readLine(); } - Matcher m = ENCODING_PATTERN.matcher(prolog); + final Matcher m = ENCODING_PATTERN.matcher(prolog); if (m.find()) { - encoding = Objects.requireNonNull(m.group(1)).toUpperCase(); + encoding = Objects.requireNonNull(m.group(1)).toUpperCase(Locale.ROOT); encoding = encoding.substring(1, encoding.length() - 1); } } @@ -712,46 +761,46 @@ public class XmlStreamReader extends Reader { /** * Indicates if the MIME type belongs to the APPLICATION XML family. - * + * * @param mime The mime type * @return true if the mime type belongs to the APPLICATION XML family, * otherwise false */ - static boolean isAppXml(String mime) { + static boolean isAppXml(final String mime) { return mime != null && - (mime.equals("application/xml") || - mime.equals("application/xml-dtd") || - mime.equals("application/xml-external-parsed-entity") || - (mime.startsWith("application/") && mime.endsWith("+xml"))); + (mime.equals("application/xml") || + mime.equals("application/xml-dtd") || + mime.equals("application/xml-external-parsed-entity") || + mime.startsWith("application/") && mime.endsWith("+xml")); } /** * Indicates if the MIME type belongs to the TEXT XML family. - * + * * @param mime The mime type * @return true if the mime type belongs to the TEXT XML family, * otherwise false */ - static boolean isTextXml(String mime) { + static boolean isTextXml(final String mime) { return mime != null && - (mime.equals("text/xml") || - mime.equals("text/xml-external-parsed-entity") || - (mime.startsWith("text/") && mime.endsWith("+xml"))); + (mime.equals("text/xml") || + mime.equals("text/xml-external-parsed-entity") || + mime.startsWith("text/") && mime.endsWith("+xml")); } private static final String RAW_EX_1 = - "Invalid encoding, BOM [{0}] XML guess [{1}] XML prolog [{2}] encoding mismatch"; + "Invalid encoding, BOM [{0}] XML guess [{1}] XML prolog [{2}] encoding mismatch"; private static final String RAW_EX_2 = - "Invalid encoding, BOM [{0}] XML guess [{1}] XML prolog [{2}] unknown BOM"; + "Invalid encoding, BOM [{0}] XML guess [{1}] XML prolog [{2}] unknown BOM"; private static final String HTTP_EX_1 = - "Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], BOM must be NULL"; + "Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], BOM must be NULL"; private static final String HTTP_EX_2 = - "Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], encoding mismatch"; + "Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], encoding mismatch"; private static final String HTTP_EX_3 = - "Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], Invalid MIME"; + "Invalid encoding, CT-MIME [{0}] CT-Enc [{1}] BOM [{2}] XML guess [{3}] XML prolog [{4}], Invalid MIME"; -} +} \ No newline at end of file diff --git a/epublib/src/main/java/me/ag2s/epublib/util/commons/io/XmlStreamReaderException.java b/epublib/src/main/java/me/ag2s/epublib/util/commons/io/XmlStreamReaderException.java index 0f97df60c..a903279d4 100644 --- a/epublib/src/main/java/me/ag2s/epublib/util/commons/io/XmlStreamReaderException.java +++ b/epublib/src/main/java/me/ag2s/epublib/util/commons/io/XmlStreamReaderException.java @@ -28,10 +28,9 @@ import java.io.IOException; * do an alternate processing with the stream. Note that the original * InputStream given to the XmlStreamReader cannot be used as that one has been * already read. + *
* - * @author Alejandro Abdelnur - * @version $Id: XmlStreamReaderException.java 1004112 2010-10-04 04:48:25Z niallp $ - * @since Commons IO 2.0 + * @since 2.0 */ public class XmlStreamReaderException extends IOException { @@ -52,14 +51,15 @@ public class XmlStreamReaderException extends IOException { * determined. ** Instances of this exception are thrown by the XmlStreamReader. + *
* * @param msg message describing the reason for the exception. * @param bomEnc BOM encoding. * @param xmlGuessEnc XML guess encoding. * @param xmlEnc XML prolog encoding. */ - public XmlStreamReaderException(String msg, String bomEnc, - String xmlGuessEnc, String xmlEnc) { + public XmlStreamReaderException(final String msg, final String bomEnc, + final String xmlGuessEnc, final String xmlEnc) { this(msg, null, null, bomEnc, xmlGuessEnc, xmlEnc); } @@ -68,6 +68,7 @@ public class XmlStreamReaderException extends IOException { * determined. ** Instances of this exception are thrown by the XmlStreamReader. + *
* * @param msg message describing the reason for the exception. * @param ctMime MIME type in the content-type. @@ -76,8 +77,8 @@ public class XmlStreamReaderException extends IOException { * @param xmlGuessEnc XML guess encoding. * @param xmlEnc XML prolog encoding. */ - public XmlStreamReaderException(String msg, String ctMime, String ctEnc, - String bomEnc, String xmlGuessEnc, String xmlEnc) { + public XmlStreamReaderException(final String msg, final String ctMime, final String ctEnc, + final String bomEnc, final String xmlGuessEnc, final String xmlEnc) { super(msg); contentTypeMime = ctMime; contentTypeEncoding = ctEnc; @@ -120,7 +121,6 @@ public class XmlStreamReaderException extends IOException { * @return the MIME type in the content-type, null if there was not * content-type or the encoding detection did not involve HTTP. */ - @SuppressWarnings("unused") public String getContentTypeMime() { return contentTypeMime; }