2020import java .io .Writer ;
2121import java .util .ArrayList ;
2222import java .util .List ;
23+ import java .util .Objects ;
2324import java .util .function .Consumer ;
2425import java .util .function .Function ;
2526import java .util .logging .Level ;
@@ -34,6 +35,7 @@ public class UnifiedDiffWriter {
3435 private static final Logger LOG = Logger .getLogger (UnifiedDiffWriter .class .getName ());
3536
3637 public static void write (UnifiedDiff diff , Function <String , List <String >> originalLinesProvider , Writer writer , int contextSize ) throws IOException {
38+ Objects .requireNonNull (originalLinesProvider , "original lines provider needs to be specified" );
3739 write (diff , originalLinesProvider , line -> {
3840 try {
3941 writer .append (line ).append ("\n " );
@@ -138,7 +140,8 @@ private static void processDeltas(Consumer<String> writer,
138140 }
139141
140142 // output the context before the first Delta
141- for (line = contextStart ; line < curDelta .getSource ().getPosition (); line ++) { //
143+ for (line = contextStart ; line < curDelta .getSource ().getPosition ()
144+ && line < origLines .size (); line ++) { //
142145 buffer .add (" " + origLines .get (line ));
143146 origTotal ++;
144147 revTotal ++;
@@ -153,8 +156,8 @@ private static void processDeltas(Consumer<String> writer,
153156 AbstractDelta <String > nextDelta = deltas .get (deltaIndex );
154157 int intermediateStart = curDelta .getSource ().getPosition ()
155158 + curDelta .getSource ().getLines ().size ();
156- for (line = intermediateStart ; line < nextDelta .getSource ()
157- . getPosition (); line ++) {
159+ for (line = intermediateStart ; line < nextDelta .getSource (). getPosition ()
160+ && line < origLines . size (); line ++) {
158161 // output the code between the last Delta and this one
159162 buffer .add (" " + origLines .get (line ));
160163 origTotal ++;
0 commit comments