ops) throws IOException;
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/badiff/src/main/java/org/badiff/alg/Graph.java:
--------------------------------------------------------------------------------
1 | /**
2 | * badiff - byte array diff - fast pure-java byte-level diffing
3 | *
4 | * Copyright (c) 2013, Robin Kirkman All rights reserved.
5 | *
6 | * Redistribution and use in source and binary forms, with or without modification,
7 | * are permitted provided that the following conditions are met:
8 | *
9 | * 1) Redistributions of source code must retain the above copyright notice,
10 | * this list of conditions and the following disclaimer.
11 | * 2) Redistributions in binary form must reproduce the above copyright notice,
12 | * this list of conditions and the following disclaimer in the documentation
13 | * and/or other materials provided with the distribution.
14 | * 3) Neither the name of the badiff nor the names of its contributors may be
15 | * used to endorse or promote products derived from this software without
16 | * specific prior written permission.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 | * THE POSSIBILITY OF SUCH DAMAGE.
29 | */
30 | package org.badiff.alg;
31 |
32 | import org.badiff.q.OpQueue;
33 |
34 | /**
35 | * An object which computes an {@link OpQueue} to convert an input byte array to an
36 | * output byte array. Usually this will use a mathematical graph construct.
37 | * @author robin
38 | *
39 | */
40 | public interface Graph {
41 | /**
42 | * Reset this graph and then recompute it for the new original and target arrays
43 | * @param orig
44 | * @param target
45 | */
46 | public void compute(byte[] orig, byte[] target);
47 | /**
48 | * Return the {@link OpQueue} of operations computed by this object
49 | * @return
50 | */
51 | public OpQueue queue();
52 | }
53 |
--------------------------------------------------------------------------------
/badiff/src/main/java/org/badiff/alg/GraphFactory.java:
--------------------------------------------------------------------------------
1 | package org.badiff.alg;
2 |
3 |
4 | public interface GraphFactory {
5 | public static final GraphFactory EDIT_GRAPH = new GraphFactory() {
6 | @Override
7 | public Graph newGraph(int capacity) {
8 | return new EditGraph(capacity);
9 | }
10 | };
11 | public static final GraphFactory INERTIAL_GRAPH = new GraphFactory() {
12 | @Override
13 | public Graph newGraph(int capacity) {
14 | return new InertialGraph(capacity);
15 | }
16 | };
17 | public static final GraphFactory ADJUSTABLE_GRAPH = new GraphFactory() {
18 | @Override
19 | public Graph newGraph(int capacity) {
20 | return new AdjustableInertialGraph(capacity);
21 | }
22 | };
23 |
24 | public Graph newGraph(int capacity);
25 | }
--------------------------------------------------------------------------------
/badiff/src/main/java/org/badiff/alg/MinNextEditGraph.java:
--------------------------------------------------------------------------------
1 | package org.badiff.alg;
2 |
3 | import java.util.Arrays;
4 |
5 | import org.badiff.Op;
6 |
7 | public class MinNextEditGraph extends EditGraph {
8 |
9 | protected int minNext = 1;
10 |
11 | public MinNextEditGraph(int size) {
12 | this(size, 1);
13 | }
14 |
15 | public MinNextEditGraph(int size, int minNext) {
16 | super(size);
17 | this.minNext = minNext;
18 | }
19 |
20 | @Override
21 | public void compute(byte[] orig, byte[] target) {
22 | xval = new byte[orig.length + 1];
23 | yval = new byte[target.length + 1];
24 |
25 | System.arraycopy(orig, 0, xval, 1, orig.length);
26 | System.arraycopy(target, 0, yval, 1, target.length);
27 |
28 | // mark short next runs as unusable
29 | Arrays.fill(flags, (byte) -1);
30 | for(int y = 0; y < yval.length; y++) {
31 | for(int x = 0; x < xval.length; x++) {
32 | if(x == 0 || y == 0 || xval[x] != yval[y])
33 | continue;
34 | int pos = x + y * xval.length;
35 |
36 | if(flags[pos] == Op.NEXT || flags[pos] == Op.STOP)
37 | continue;
38 |
39 | int extent = 0;
40 | int ix = x;
41 | int iy = y;
42 | do {
43 | extent++;
44 | ix++;
45 | iy++;
46 | if(ix == xval.length || iy == yval.length)
47 | break;
48 | } while(xval[ix] == yval[iy]);
49 |
50 | while(--ix >= x && --iy >= y) {
51 | flags[ix + iy * xval.length] = (extent >= minNext) ? Op.NEXT : Op.STOP;
52 | }
53 | }
54 | }
55 |
56 | // "normal" compute
57 | for(int y = 0; y < yval.length; y++) {
58 | for(int x = 0; x < xval.length; x++) {
59 | if(x == 0 && y == 0)
60 | continue;
61 | int pos = x + y * xval.length;
62 |
63 | // check for equality, but ensure that if equal not marked as too short
64 | if(x > 0 && y > 0 && flags[pos] == Op.NEXT) {
65 | lengths[pos] = (short) (1 + lengths[pos - xval.length - 1]);
66 | continue;
67 | }
68 | short dlen = x > 0 ? (short)(1 + lengths[pos-1]) : Short.MAX_VALUE;
69 | short ilen = y > 0 ? (short)(1 + lengths[pos - xval.length]) : Short.MAX_VALUE;
70 | if(dlen <= ilen) {
71 | flags[pos] = Op.DELETE;
72 | lengths[pos] = dlen;
73 | } else {
74 | flags[pos] = Op.INSERT;
75 | lengths[pos] = ilen;
76 | }
77 |
78 | }
79 | }
80 | }
81 |
82 | public int getMinNext() {
83 | return minNext;
84 | }
85 |
86 | public void setMinNext(int minNext) {
87 | if(minNext < 1)
88 | throw new IllegalArgumentException();
89 | this.minNext = minNext;
90 | }
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/badiff/src/main/java/org/badiff/alg/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Edit-graphing algorithm implementations.
3 | *
4 | * Contains various implementations of the basic edit graph concept, such
5 | * as weighted edit graphs.
6 | */
7 | package org.badiff.alg;
--------------------------------------------------------------------------------
/badiff/src/main/java/org/badiff/fmt/InputFormat.java:
--------------------------------------------------------------------------------
1 | /**
2 | * badiff - byte array diff - fast pure-java byte-level diffing
3 | *
4 | * Copyright (c) 2013, Robin Kirkman All rights reserved.
5 | *
6 | * Redistribution and use in source and binary forms, with or without modification,
7 | * are permitted provided that the following conditions are met:
8 | *
9 | * 1) Redistributions of source code must retain the above copyright notice,
10 | * this list of conditions and the following disclaimer.
11 | * 2) Redistributions in binary form must reproduce the above copyright notice,
12 | * this list of conditions and the following disclaimer in the documentation
13 | * and/or other materials provided with the distribution.
14 | * 3) Neither the name of the badiff nor the names of its contributors may be
15 | * used to endorse or promote products derived from this software without
16 | * specific prior written permission.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 | * THE POSSIBILITY OF SUCH DAMAGE.
29 | */
30 | package org.badiff.fmt;
31 |
32 | import java.io.IOException;
33 | import org.badiff.io.RandomInput;
34 | import org.badiff.q.OpQueue;
35 |
36 | /**
37 | * An object which can import a diff from an external format
38 | * @author robin
39 | *
40 | */
41 | public interface InputFormat {
42 | /**
43 | * Import the externally formatted diff
44 | * @param orig The original file
45 | * @param extDiff The diff to import
46 | * @return A badiff diff
47 | */
48 | public OpQueue importDiff(RandomInput orig, RandomInput ext) throws IOException;
49 | }
50 |
--------------------------------------------------------------------------------
/badiff/src/main/java/org/badiff/fmt/OutputFormat.java:
--------------------------------------------------------------------------------
1 | /**
2 | * badiff - byte array diff - fast pure-java byte-level diffing
3 | *
4 | * Copyright (c) 2013, Robin Kirkman All rights reserved.
5 | *
6 | * Redistribution and use in source and binary forms, with or without modification,
7 | * are permitted provided that the following conditions are met:
8 | *
9 | * 1) Redistributions of source code must retain the above copyright notice,
10 | * this list of conditions and the following disclaimer.
11 | * 2) Redistributions in binary form must reproduce the above copyright notice,
12 | * this list of conditions and the following disclaimer in the documentation
13 | * and/or other materials provided with the distribution.
14 | * 3) Neither the name of the badiff nor the names of its contributors may be
15 | * used to endorse or promote products derived from this software without
16 | * specific prior written permission.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 | * THE POSSIBILITY OF SUCH DAMAGE.
29 | */
30 | package org.badiff.fmt;
31 |
32 | import java.io.DataOutput;
33 | import java.io.IOException;
34 | import org.badiff.Diff;
35 | import org.badiff.io.RandomInput;
36 |
37 | /**
38 | * An object which can convert a {@link Diff} to an external format for long-term storage
39 | * @author robin
40 | *
41 | */
42 | public interface OutputFormat {
43 | /**
44 | * Export the diff to the external format.
45 | * @param diff The diff to export
46 | * @param orig The original file to reference while exporting
47 | * @param out The output to which to write the exported diff
48 | * @throws IOException
49 | */
50 | public void exportDiff(Diff diff, RandomInput orig, DataOutput out) throws IOException;
51 | }
52 |
--------------------------------------------------------------------------------
/badiff/src/main/java/org/badiff/fmt/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Classes for reading and writing diffs in various formats.
3 | */
4 | package org.badiff.fmt;
--------------------------------------------------------------------------------
/badiff/src/main/java/org/badiff/imp/StreamQueueable.java:
--------------------------------------------------------------------------------
1 | package org.badiff.imp;
2 |
3 | import java.io.IOException;
4 | import java.io.InputStream;
5 |
6 | import org.badiff.Op;
7 | import org.badiff.Queueable;
8 | import org.badiff.io.DefaultSerialization;
9 | import org.badiff.io.RuntimeIOException;
10 | import org.badiff.io.Serialization;
11 | import org.badiff.q.OpQueue;
12 |
13 | /**
14 | * {@link Queueable} that reads its elements from an {@link InputStream}.
15 | * The {@link #queue()} method can be called only once; calling it again
16 | * will return the next sequence of {@link Op}s in the input stream.
17 | * @author robin
18 | *
19 | */
20 | public class StreamQueueable implements Queueable {
21 | /**
22 | * Serialization used for this stream
23 | */
24 | protected Serialization serial;
25 | /**
26 | * The input stream
27 | */
28 | protected InputStream in;
29 |
30 | /**
31 | * Create a {@link StreamQueueable} with the {@link DefaultSerialization}
32 | * @param in
33 | */
34 | public StreamQueueable(InputStream in) {
35 | this(in, DefaultSerialization.newInstance());
36 | }
37 |
38 | /**
39 | * Create a {@link StreamQueueable}
40 | * @param in
41 | * @param serial
42 | */
43 | public StreamQueueable(InputStream in, Serialization serial) {
44 | this.in = in;
45 | this.serial = serial;
46 | }
47 |
48 | @Override
49 | public OpQueue queue() throws IOException {
50 | return new StreamOpQueue();
51 | }
52 |
53 | /**
54 | * {@link OpQueue} that reads from a stream
55 | * @author robin
56 | *
57 | */
58 | private class StreamOpQueue extends OpQueue {
59 | private boolean closed;
60 |
61 | @Override
62 | public boolean offer(Op e) {
63 | throw new UnsupportedOperationException();
64 | }
65 |
66 | @Override
67 | protected boolean pull() {
68 | if(closed)
69 | return false;
70 |
71 | try {
72 | Op e = serial.readObject(in, Op.class);
73 | if(e.getOp() == Op.STOP) {
74 | closed = true;
75 | return false;
76 | } else {
77 | prepare(e);
78 | return true;
79 | }
80 | } catch(IOException ioe) {
81 | throw new RuntimeIOException(ioe);
82 | }
83 | }
84 | }
85 |
86 | }
87 |
--------------------------------------------------------------------------------
/badiff/src/main/java/org/badiff/imp/StreamStoreable.java:
--------------------------------------------------------------------------------
1 | package org.badiff.imp;
2 |
3 | import java.io.IOException;
4 | import java.io.OutputStream;
5 | import java.util.Iterator;
6 |
7 | import org.badiff.Op;
8 | import org.badiff.Storeable;
9 | import org.badiff.io.DefaultSerialization;
10 | import org.badiff.io.Serialization;
11 |
12 | public class StreamStoreable implements Storeable {
13 |
14 | protected OutputStream out;
15 | protected Serialization serial;
16 |
17 | public StreamStoreable(OutputStream out) {
18 | this(out, DefaultSerialization.newInstance());
19 | }
20 |
21 | public StreamStoreable(OutputStream out, Serialization serial) {
22 | this.out = out;
23 | this.serial = serial;
24 | }
25 |
26 | @Override
27 | public void store(Iterator ops) throws IOException {
28 | while(ops.hasNext()) {
29 | Op e = ops.next();
30 | e.serialize(serial, out);
31 | }
32 | new Op(Op.STOP, 1, null).serialize(serial, out);
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/badiff/src/main/java/org/badiff/imp/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Implementations of {@link org.badiff.Diff}
3 | */
4 | package org.badiff.imp;
--------------------------------------------------------------------------------
/badiff/src/main/java/org/badiff/io/DataInputInputStream.java:
--------------------------------------------------------------------------------
1 | /**
2 | * badiff - byte array diff - fast pure-java byte-level diffing
3 | *
4 | * Copyright (c) 2013, Robin Kirkman All rights reserved.
5 | *
6 | * Redistribution and use in source and binary forms, with or without modification,
7 | * are permitted provided that the following conditions are met:
8 | *
9 | * 1) Redistributions of source code must retain the above copyright notice,
10 | * this list of conditions and the following disclaimer.
11 | * 2) Redistributions in binary form must reproduce the above copyright notice,
12 | * this list of conditions and the following disclaimer in the documentation
13 | * and/or other materials provided with the distribution.
14 | * 3) Neither the name of the badiff nor the names of its contributors may be
15 | * used to endorse or promote products derived from this software without
16 | * specific prior written permission.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 | * THE POSSIBILITY OF SUCH DAMAGE.
29 | */
30 | package org.badiff.io;
31 |
32 | import java.io.DataInput;
33 | import java.io.EOFException;
34 | import java.io.IOException;
35 | import java.io.InputStream;
36 | import java.io.ObjectInput;
37 |
38 | /**
39 | * {@link InputStream} that reads from an {@link ObjectInput}
40 | * @author robin
41 | *
42 | */
43 | public class DataInputInputStream extends InputStream {
44 |
45 | protected DataInput in;
46 |
47 | public DataInputInputStream(DataInput in) {
48 | this.in = in;
49 | }
50 |
51 | @Override
52 | public int read() throws IOException {
53 | try {
54 | return 0xff & in.readByte();
55 | } catch(EOFException eof) {
56 | return -1;
57 | }
58 | }
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/badiff/src/main/java/org/badiff/io/DataOutputOutputStream.java:
--------------------------------------------------------------------------------
1 | /**
2 | * badiff - byte array diff - fast pure-java byte-level diffing
3 | *
4 | * Copyright (c) 2013, Robin Kirkman All rights reserved.
5 | *
6 | * Redistribution and use in source and binary forms, with or without modification,
7 | * are permitted provided that the following conditions are met:
8 | *
9 | * 1) Redistributions of source code must retain the above copyright notice,
10 | * this list of conditions and the following disclaimer.
11 | * 2) Redistributions in binary form must reproduce the above copyright notice,
12 | * this list of conditions and the following disclaimer in the documentation
13 | * and/or other materials provided with the distribution.
14 | * 3) Neither the name of the badiff nor the names of its contributors may be
15 | * used to endorse or promote products derived from this software without
16 | * specific prior written permission.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 | * THE POSSIBILITY OF SUCH DAMAGE.
29 | */
30 | package org.badiff.io;
31 |
32 | import java.io.DataOutput;
33 | import java.io.IOException;
34 | import java.io.ObjectOutput;
35 | import java.io.OutputStream;
36 |
37 | /**
38 | * {@link OutputStream} that reads from an {@link ObjectOutput}
39 | * @author robin
40 | *
41 | */
42 | public class DataOutputOutputStream extends OutputStream {
43 |
44 | protected DataOutput out;
45 |
46 | public DataOutputOutputStream(DataOutput out) {
47 | this.out = out;
48 | }
49 |
50 | @Override
51 | public void write(int b) throws IOException {
52 | out.write(b);
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/badiff/src/main/java/org/badiff/io/EmptyInputStream.java:
--------------------------------------------------------------------------------
1 | /**
2 | * badiff - byte array diff - fast pure-java byte-level diffing
3 | *
4 | * Copyright (c) 2013, Robin Kirkman All rights reserved.
5 | *
6 | * Redistribution and use in source and binary forms, with or without modification,
7 | * are permitted provided that the following conditions are met:
8 | *
9 | * 1) Redistributions of source code must retain the above copyright notice,
10 | * this list of conditions and the following disclaimer.
11 | * 2) Redistributions in binary form must reproduce the above copyright notice,
12 | * this list of conditions and the following disclaimer in the documentation
13 | * and/or other materials provided with the distribution.
14 | * 3) Neither the name of the badiff nor the names of its contributors may be
15 | * used to endorse or promote products derived from this software without
16 | * specific prior written permission.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 | * THE POSSIBILITY OF SUCH DAMAGE.
29 | */
30 | package org.badiff.io;
31 |
32 | import java.io.IOException;
33 | import java.io.InputStream;
34 |
35 | /**
36 | * {@link InputStream} that has no content
37 | * @author robin
38 | *
39 | */
40 | public class EmptyInputStream extends InputStream {
41 |
42 | @Override
43 | public int read() throws IOException {
44 | return -1;
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/badiff/src/main/java/org/badiff/io/FileRandomInput.java:
--------------------------------------------------------------------------------
1 | package org.badiff.io;
2 |
3 | import java.io.DataInputStream;
4 | import java.io.File;
5 | import java.io.FileInputStream;
6 | import java.io.FilterInputStream;
7 | import java.io.IOException;
8 |
9 | public class FileRandomInput extends FilterInputStream implements RandomInput {
10 | protected File file;
11 | protected long pos;
12 | protected DataInputStream data;
13 |
14 | public FileRandomInput(File file) throws IOException {
15 | super(new FileInputStream(file));
16 | this.file = file;
17 | data = new DataInputStream(this);
18 | }
19 |
20 | @Override
21 | public long first() {
22 | return 0;
23 | }
24 |
25 | @Override
26 | public long last() {
27 | return file.length();
28 | }
29 |
30 | @Override
31 | public long position() {
32 | return pos;
33 | }
34 |
35 | @Override
36 | public void seek(long pos) throws IOException {
37 | skip(pos - this.pos);
38 | }
39 |
40 | @Override
41 | public int read() throws IOException {
42 | int b = super.read();
43 | if(b >= 0)
44 | pos++;
45 | return b;
46 | }
47 |
48 | @Override
49 | public int read(byte[] b) throws IOException {
50 | return super.read(b);
51 | }
52 |
53 | @Override
54 | public int read(byte[] b, int off, int len) throws IOException {
55 | int r = super.read(b, off, len);
56 | if(r > 0)
57 | pos += r;
58 | return r;
59 | }
60 |
61 | @Override
62 | public long skip(long n) throws IOException {
63 | if(n > 0) {
64 | long s = in.skip(n);
65 | pos += s;
66 | return s;
67 | } else if(n < 0) {
68 | in.close();
69 | in = new FileInputStream(file);
70 | in.skip(pos + n);
71 | pos = pos + n;
72 | return n;
73 | } else
74 | return 0;
75 | }
76 |
77 | public final void readFully(byte[] b) throws IOException {
78 | data.readFully(b);
79 | }
80 |
81 | public final void readFully(byte[] b, int off, int len) throws IOException {
82 | data.readFully(b, off, len);
83 | }
84 |
85 | public final boolean readBoolean() throws IOException {
86 | return data.readBoolean();
87 | }
88 |
89 | public final byte readByte() throws IOException {
90 | return data.readByte();
91 | }
92 |
93 | public final int readUnsignedByte() throws IOException {
94 | return data.readUnsignedByte();
95 | }
96 |
97 | public final short readShort() throws IOException {
98 | return data.readShort();
99 | }
100 |
101 | public final int readUnsignedShort() throws IOException {
102 | return data.readUnsignedShort();
103 | }
104 |
105 | public final char readChar() throws IOException {
106 | return data.readChar();
107 | }
108 |
109 | public final int readInt() throws IOException {
110 | return data.readInt();
111 | }
112 |
113 | public final long readLong() throws IOException {
114 | return data.readLong();
115 | }
116 |
117 | public final float readFloat() throws IOException {
118 | return data.readFloat();
119 | }
120 |
121 | public final double readDouble() throws IOException {
122 | return data.readDouble();
123 | }
124 |
125 | @Deprecated
126 | public final String readLine() throws IOException {
127 | return data.readLine();
128 | }
129 |
130 | public final String readUTF() throws IOException {
131 | return data.readUTF();
132 | }
133 |
134 | @Override
135 | public int skipBytes(int n) throws IOException {
136 | return data.skipBytes(n);
137 | }
138 |
139 | }
140 |
--------------------------------------------------------------------------------
/badiff/src/main/java/org/badiff/io/GraphContext.java:
--------------------------------------------------------------------------------
1 | /**
2 | * badiff - byte array diff - fast pure-java byte-level diffing
3 | *
4 | * Copyright (c) 2013, Robin Kirkman All rights reserved.
5 | *
6 | * Redistribution and use in source and binary forms, with or without modification,
7 | * are permitted provided that the following conditions are met:
8 | *
9 | * 1) Redistributions of source code must retain the above copyright notice,
10 | * this list of conditions and the following disclaimer.
11 | * 2) Redistributions in binary form must reproduce the above copyright notice,
12 | * this list of conditions and the following disclaimer in the documentation
13 | * and/or other materials provided with the distribution.
14 | * 3) Neither the name of the badiff nor the names of its contributors may be
15 | * used to endorse or promote products derived from this software without
16 | * specific prior written permission.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 | * THE POSSIBILITY OF SUCH DAMAGE.
29 | */
30 | package org.badiff.io;
31 |
32 | import java.util.HashMap;
33 | import java.util.Map;
34 |
35 | public class GraphContext extends HashMap