pull/952/head
ag2s20150909 4 years ago
parent e4513af88e
commit 937d4c61ef
  1. 3
      epublib/src/main/java/me/ag2s/epublib/epub/EpubWriter.java
  2. 16
      epublib/src/main/java/me/ag2s/epublib/util/IOUtil.java

@ -102,7 +102,8 @@ public class EpubWriter {
try { try {
resultStream.putNextEntry(new ZipEntry("OEBPS/" + resource.getHref())); resultStream.putNextEntry(new ZipEntry("OEBPS/" + resource.getHref()));
InputStream inputStream = resource.getInputStream(); InputStream inputStream = resource.getInputStream();
IOUtil.copy(inputStream, resultStream,IOUtil.DEFAULT_BUFFER_SIZE);
IOUtil.copy(inputStream, resultStream);
inputStream.close(); inputStream.close();
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG,e.getMessage(), e); Log.e(TAG,e.getMessage(), e);

@ -31,6 +31,7 @@ public class IOUtil {
/** /**
* Represents the end-of-file (or stream). * Represents the end-of-file (or stream).
*
* @since 2.5 (made public) * @since 2.5 (made public)
*/ */
public static final int EOF = -1; public static final int EOF = -1;
@ -77,11 +78,12 @@ public class IOUtil {
*/ */
public static byte[] toByteArray(InputStream in) throws IOException { public static byte[] toByteArray(InputStream in) throws IOException {
ByteArrayOutputStream result = new ByteArrayOutputStream(); ByteArrayOutputStream result = new ByteArrayOutputStream();
copy(in, result,DEFAULT_BUFFER_SIZE); copy(in, result);
result.flush(); result.flush();
return result.toByteArray(); return result.toByteArray();
} }
/** /**
* Reads data from the InputStream, using the specified buffer size. * Reads data from the InputStream, using the specified buffer size.
* <p> * <p>
@ -105,7 +107,7 @@ public class IOUtil {
result = new ByteArrayOutputStream(); result = new ByteArrayOutputStream();
} }
copy(in, result,DEFAULT_BUFFER_SIZE); copy(in, result);
result.flush(); result.flush();
return result.toByteArray(); return result.toByteArray();
} catch (OutOfMemoryError error) { } catch (OutOfMemoryError error) {
@ -139,6 +141,14 @@ public class IOUtil {
} }
// //
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 <code>InputStream</code> to an <code>OutputStream</code> using an internal buffer of the * Copies bytes from an <code>InputStream</code> to an <code>OutputStream</code> using an internal buffer of the
* given size. * given size.
@ -828,6 +838,7 @@ public class IOUtil {
throw new EOFException("Chars to skip: " + toSkip + " actual: " + skipped); throw new EOFException("Chars to skip: " + toSkip + " actual: " + skipped);
} }
} }
/** /**
* Returns the length of the given array in a null-safe manner. * Returns the length of the given array in a null-safe manner.
* *
@ -871,6 +882,7 @@ public class IOUtil {
public static int length(final Object[] array) { public static int length(final Object[] array) {
return array == null ? 0 : array.length; return array == null ? 0 : array.length;
} }
/** /**
* Closes the given {@link Closeable} as a null-safe operation. * Closes the given {@link Closeable} as a null-safe operation.
* *

Loading…
Cancel
Save