Giter VIP home page Giter VIP logo

bsdiff-java's Introduction

bsdiff-java

bsdiff v4.3 Java implementation ( the core diff and patch code is hard-code translated )

Hightlights

  1. support low memory useage (NIO file mapping)
  2. support memory diff & path and write to memory

Disclaimer

Although I did some tests like diff in Java and then patch using bsdpatch command on the terminal or vice versa, but this does not guarantee that the code is fully compatible with the C version.

The code is not guaranteed to be usable in all situations and has not been extensively tested.

Examples

public void fileDiff() throws IOException {
    final String oldFilePath = "/path/to/file.old";
    final String newFilePath = "/path/to/file.new";
    final String patchFilePath = "/path/to/file.old.new.path";

    try (FileInputStream ofis = new FileInputStream(oldFilePath)) {
        final ByteBuffer old =
                ofis.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, ofis.available());
        try (FileInputStream nfis = new FileInputStream(newFilePath)) {
            final ByteBuffer new0 =
                    nfis.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, nfis.available());
            try (FileOutputStream pfos = new FileOutputStream(patchFilePath)) {
                BufferAllocator bufferAllocator = ByteBuffer::allocate;
                // or
                // BufferAllocator bufferAllocator = new FileBufferAllocator();
                BSDiff.diff(old, new0, new SeekableFileOutputStream(pfos), bufferAllocator);
            }
        }
    }
}

public void filePatch() throws IOException {
    final String oldFilePath = "/path/to/file.old";
    final String newFilePath = "/path/to/file.new";
    final String patchFilePath = "/path/to/file.old.new.path";

    try (FileInputStream pfis = new FileInputStream(patchFilePath)) {
        final ByteBuffer pbuf = pfis.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, pfis.available());
        int newFileSize = BSPatch.parseNewSize(pbuf);
        try (RandomAccessFile newFile = new RandomAccessFile(newFilePath, "rw")) {
            newFile.getChannel().truncate(newFileSize);
            // Note: FileOutputStream channel does not support mapping immediately after truncation
            final ByteBuffer new0 = newFile.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, newFileSize);
            try (FileInputStream ofis = new FileInputStream(oldFilePath)) {
                final ByteBuffer old =
                        ofis.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, ofis.available());
                BSPatch.patch(old, new0, pbuf);
            }
        }
    }
}

public void memoryDiff() throws IOException {
    byte[] oldMem = null;
    byte[] newMem = null;

    ByteBuffer old = ByteBuffer.wrap(oldMem);
    ByteBuffer new0 = ByteBuffer.wrap(newMem);

    SeekableByteArrayOutputStream sbos = new SeekableByteArrayOutputStream();
    BSDiff.diff(old, new0, sbos, ByteBuffer::allocate);
}

public void memoryPatch() throws Exception {
    byte[] oldMem = null;
    byte[] patchMem = null;
    byte[] newMem;

    ByteBuffer old = ByteBuffer.wrap(oldMem);
    ByteBuffer p = ByteBuffer.wrap(patchMem);
    int newSize = BSPatch.parseNewSize(p);
    newMem = new byte[newSize];
    ByteBuffer new0 = ByteBuffer.wrap(newMem);
    BSPatch.patch(old, new0, p);
}

bsdiff-java's People

Contributors

joelcho avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.