diff options
Diffstat (limited to 'org.jacoco.core/src/org/jacoco/core/instr/Instrumenter.java')
-rw-r--r-- | org.jacoco.core/src/org/jacoco/core/instr/Instrumenter.java | 70 |
1 files changed, 51 insertions, 19 deletions
diff --git a/org.jacoco.core/src/org/jacoco/core/instr/Instrumenter.java b/org.jacoco.core/src/org/jacoco/core/instr/Instrumenter.java index aa260168..eb91f561 100644 --- a/org.jacoco.core/src/org/jacoco/core/instr/Instrumenter.java +++ b/org.jacoco.core/src/org/jacoco/core/instr/Instrumenter.java @@ -132,11 +132,13 @@ public class Instrumenter { */ public byte[] instrument(final InputStream input, final String name) throws IOException { + final byte[] bytes; try { - return instrument(Java9Support.readFully(input), name); - } catch (final RuntimeException e) { + bytes = Java9Support.readFully(input); + } catch (final IOException e) { throw instrumentError(name, e); } + return instrument(bytes, name); } /** @@ -154,17 +156,13 @@ public class Instrumenter { */ public void instrument(final InputStream input, final OutputStream output, final String name) throws IOException { - try { - output.write(instrument(Java9Support.readFully(input), name)); - } catch (final RuntimeException e) { - throw instrumentError(name, e); - } + output.write(instrument(input, name)); } private IOException instrumentError(final String name, - final RuntimeException cause) { - final IOException ex = new IOException(String.format( - "Error while instrumenting class %s.", name)); + final Exception cause) { + final IOException ex = new IOException( + String.format("Error while instrumenting %s.", name)); ex.initCause(cause); return ex; } @@ -187,7 +185,12 @@ public class Instrumenter { */ public int instrumentAll(final InputStream input, final OutputStream output, final String name) throws IOException { - final ContentTypeDetector detector = new ContentTypeDetector(input); + final ContentTypeDetector detector; + try { + detector = new ContentTypeDetector(input); + } catch (IOException e) { + throw instrumentError(name, e); + } switch (detector.getType()) { case ContentTypeDetector.CLASSFILE: instrument(detector.getInputStream(), output, name); @@ -199,7 +202,7 @@ public class Instrumenter { case ContentTypeDetector.PACK200FILE: return instrumentPack200(detector.getInputStream(), output, name); default: - copy(detector.getInputStream(), output); + copy(detector.getInputStream(), output, name); return 0; } } @@ -210,7 +213,7 @@ public class Instrumenter { final ZipOutputStream zipout = new ZipOutputStream(output); ZipEntry entry; int count = 0; - while ((entry = zipin.getNextEntry()) != null) { + while ((entry = nextEntry(zipin, name)) != null) { final String entryName = entry.getName(); if (signatureRemover.removeEntry(entryName)) { continue; @@ -226,30 +229,59 @@ public class Instrumenter { return count; } + private ZipEntry nextEntry(ZipInputStream input, String location) + throws IOException { + try { + return input.getNextEntry(); + } catch (IOException e) { + throw instrumentError(location, e); + } + } + private int instrumentGzip(final InputStream input, final OutputStream output, final String name) throws IOException { + final GZIPInputStream gzipInputStream; + try { + gzipInputStream = new GZIPInputStream(input); + } catch (IOException e) { + throw instrumentError(name, e); + } final GZIPOutputStream gzout = new GZIPOutputStream(output); - final int count = instrumentAll(new GZIPInputStream(input), gzout, name); + final int count = instrumentAll(gzipInputStream, gzout, name); gzout.finish(); return count; } private int instrumentPack200(final InputStream input, final OutputStream output, final String name) throws IOException { + final InputStream unpackedInput; + try { + unpackedInput = Pack200Streams.unpack(input); + } catch (IOException e) { + throw instrumentError(name, e); + } final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - final int count = instrumentAll(Pack200Streams.unpack(input), buffer, - name); + final int count = instrumentAll(unpackedInput, buffer, name); Pack200Streams.pack(buffer.toByteArray(), output); return count; } - private void copy(final InputStream input, final OutputStream output) - throws IOException { + private void copy(final InputStream input, final OutputStream output, + final String name) throws IOException { final byte[] buffer = new byte[1024]; int len; - while ((len = input.read(buffer)) != -1) { + while ((len = read(input, buffer, name)) != -1) { output.write(buffer, 0, len); } } + private int read(final InputStream input, final byte[] buffer, + final String name) throws IOException { + try { + return input.read(buffer); + } catch (IOException e) { + throw instrumentError(name, e); + } + } + } |