All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nothome.delta.GDiffWriter Maven / Gradle / Ivy

There is a newer version: 1.5
Show newest version
/* 
 *
 * Copyright (c) 2001 Torgeir Veimo
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 */
package com.nothome.delta;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Outputs a diff following the GDIFF file specification available at
 * http://www.w3.org/TR/NOTE-gdiff-19970901.html.
 */
public class GDiffWriter implements DiffWriter, AutoCloseable {
  /**
   * Max length of a chunk.
   */
  public static final int CHUNK_SIZE = Short.MAX_VALUE;
  /** The Constant EOF. */
  public static final byte EOF = 0;
  /**
   * Max length for single length data encode.
   */
  public static final int DATA_MAX = 246;
  /** The Constant DATA_USHORT. */
  public static final int DATA_USHORT = 247;
  /** The Constant DATA_INT. */
  public static final int DATA_INT = 248;
  /** The Constant COPY_USHORT_UBYTE. */
  public static final int COPY_USHORT_UBYTE = 249;
  /** The Constant COPY_USHORT_USHORT. */
  public static final int COPY_USHORT_USHORT = 250;
  /** The Constant COPY_USHORT_INT. */
  public static final int COPY_USHORT_INT = 251;
  /** The Constant COPY_INT_UBYTE. */
  public static final int COPY_INT_UBYTE = 252;
  /** The Constant COPY_INT_USHORT. */
  public static final int COPY_INT_USHORT = 253;
  /** The Constant COPY_INT_INT. */
  public static final int COPY_INT_INT = 254;
  /** The Constant COPY_LONG_INT. */
  public static final int COPY_LONG_INT = 255;
  /** The buf. */
  private ByteArrayOutputStream buf = new ByteArrayOutputStream();
  /** The debug. */
  private boolean debug = false;
  /** The output. */
  private DataOutputStream output = null;

  /**
   * Constructs a new GDiffWriter.
   *
   * @param os the os
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public GDiffWriter(DataOutputStream os) throws IOException {
    this.output = os;
    // write magic string "d1 ff d1 ff 04"
    output.writeByte(0xd1);
    output.writeByte(0xff);
    output.writeByte(0xd1);
    output.writeByte(0xff);
    output.writeByte(0x04);
  }

  /**
   * Constructs a new GDiffWriter.
   *
   * @param output the output
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public GDiffWriter(OutputStream output) throws IOException {
    this(new DataOutputStream(output));
  }

  /* (non-Javadoc)
   * @see com.nothome.delta.DiffWriter#addCopy(long, int)
   */
  @Override
  public void addCopy(long offset, int length) throws IOException {
    writeBuf();
    //output debug data        
    if (debug)
      System.err.println("COPY off: " + offset + ", len: " + length);
    // output real data
    if (offset > Integer.MAX_VALUE) {
      // Actually, we don't support longer files than int.MAX_VALUE at the moment..
      output.writeByte(COPY_LONG_INT);
      output.writeLong(offset);
      output.writeInt(length);
    } else if (offset < 65536) {
      if (length < 256) {
        output.writeByte(COPY_USHORT_UBYTE);
        output.writeShort((int) offset);
        output.writeByte(length);
      } else if (length > 65535) {
        output.writeByte(COPY_USHORT_INT);
        output.writeShort((int) offset);
        output.writeInt(length);
      } else {
        output.writeByte(COPY_USHORT_USHORT);
        output.writeShort((int) offset);
        output.writeShort(length);
      }
    } else {
      if (length < 256) {
        output.writeByte(COPY_INT_UBYTE);
        output.writeInt((int) offset);
        output.writeByte(length);
      } else if (length > 65535) {
        output.writeByte(COPY_INT_INT);
        output.writeInt((int) offset);
        output.writeInt(length);
      } else {
        output.writeByte(COPY_INT_USHORT);
        output.writeInt((int) offset);
        output.writeShort(length);
      }
    }
  }

  /**
   * Adds a data byte.
   *
   * @param b the b
   * @throws IOException Signals that an I/O exception has occurred.
   */
  @Override
  public void addData(byte b) throws IOException {
    buf.write(b);
    if (buf.size() >= CHUNK_SIZE)
      writeBuf();
  }

  /**
   * Write buf.
   *
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private void writeBuf() throws IOException {
    if (buf.size() > 0) {
      if (buf.size() <= DATA_MAX) {
        output.writeByte(buf.size());
      } else if (buf.size() <= 65535) {
        output.writeByte(DATA_USHORT);
        output.writeShort(buf.size());
      } else {
        output.writeByte(DATA_INT);
        output.writeInt(buf.size());
      }
      buf.writeTo(output);
      buf.reset();
    }
  }

  /**
   * Flushes accumulated data bytes, if any.
   *
   * @throws IOException Signals that an I/O exception has occurred.
   */
  @Override
  public void flush() throws IOException {
    writeBuf();
    output.flush();
  }

  /**
   * Writes the final EOF byte, closes the underlying stream.
   *
   * @throws IOException Signals that an I/O exception has occurred.
   */
  @Override
  public void close() throws IOException {
    try (OutputStream os = output) {
      this.flush();
      output.write(EOF);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy