Write and read compressed forms of numbers to DataOutput and DataInput interfaces. : Byte Read Write « File Input Output « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » File Input Output » Byte Read WriteScreenshots 
Write and read compressed forms of numbers to DataOutput and DataInput interfaces.
  
 
/*

 Derby - Class org.apache.derby.iapi.services.io.CompressedNumber

 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to you under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 */

import java.io.DataInput;
import java.io.DataOutput;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Static methods to write and read compressed forms of numbers to DataOut and
 * DataIn interfaces. Format written is platform independent like the Data*
 * interfaces and must remain fixed once a product is shipped. If a different
 * format is required then write a new set of methods, e.g. writeInt2. The
 * formats defined by stored format identifiers are implicitly dependent on
 * these formats not changing.
 */

public abstract class CompressedNumber {

  // the maximum number of bytes written out for an int
  public static final int MAX_INT_STORED_SIZE = 4;

  // the maximum number of bytes written out for a long
  public static final int MAX_LONG_STORED_SIZE = 8;

  // largest int stored compressed in 1 byte
  public static final int MAX_COMPRESSED_INT_ONE_BYTE = 0x3f;

  // largest int stored compressed in 2 bytes
  public static final int MAX_COMPRESSED_INT_TWO_BYTES = 0x3fff;

  /**
   * Write a compressed integer only supporting signed values. Formats are (with
   * x representing value bits):
   
   * <PRE>
   
   * 1 Byte - 00xxxxxx Represents the value <= 63 (0x3f) 2 Byte - 01xxxxxx
   * xxxxxxxx Represents the value > 63 && <= 16383 (0x3fff) 4 byte - 1xxxxxxx
   * xxxxxxxx xxxxxxxx xxxxxxxx Represents the value > 16383 && <= MAX_INT
   
   * </PRE>
   
   
   @exception IOException
   *              value is negative or an exception was thrown by a method on
   *              out.
   */
  public static final int writeInt(DataOutput out, int valuethrows IOException {

    if (value < 0)
      throw new IOException();

    if (value <= 0x3f) {

      out.writeByte(value);
      return 1;
    }

    if (value <= 0x3fff) {

      out.writeByte(0x40 (value >>> 8));
      out.writeByte(value & 0xff);
      return 2;
    }

    out.writeByte(((value >>> 240x800xff);
    out.writeByte((value >>> 160xff);
    out.writeByte((value >>> 80xff);
    out.writeByte((value0xff);
    return 4;
  }

  /**
   * Write a compressed integer directly to an OutputStream.
   
   @exception IOException
   *              an exception was thrown by a method on in.
   */
  public static final int writeInt(OutputStream out, int valuethrows IOException {

    if (value < 0)
      throw new IOException();

    if (value <= 0x3f) {

      out.write(value);
      return 1;
    }

    if (value <= 0x3fff) {

      out.write(0x40 (value >>> 8));
      out.write(value & 0xff);
      return 2;
    }

    out.write(((value >>> 240x800xff);
    out.write((value >>> 160xff);
    out.write((value >>> 80xff);
    out.write((value0xff);
    return 4;
  }

  /**
   * Read an integer previously written by writeInt().
   
   @exception IOException
   *              an exception was thrown by a method on in.
   */
  public static final int readInt(DataInput inthrows IOException {

    int value = in.readUnsignedByte();

    if ((value & ~0x3f== 0) {
      // length is stored in this byte, we also know that the 0x80 bit
      // was not set, so no need to mask off the sign extension from
      // the byte to int conversion.

      // account for 1 byte stored length of field + 1 for all returns
      return (value);
    else if ((value & 0x80== 0) {
      // length is stored in 2 bytes. only use low 6 bits from 1st byte.

      // top 8 bits of 2 byte length is stored in this byte, we also
      // know that the 0x80 bit was not set, so no need to mask off the
      // sign extension from the 1st byte to int conversion. Need to
      // mask the byte in data[offset + 1] to account for possible sign
      // extension.

      return (((value & 0x3f<< 8| in.readUnsignedByte());
    else {

      // top 8 bits of 4 byte length is stored in this byte, we also
      // know that the 0x80 bit was set, so need to mask off the
      // sign extension from the 1st byte to int conversion. Need to
      // mask the bytes from the next 3 bytes data[offset + 1,2,3] to
      // account for possible sign extension.
      //
      return (((value & 0x7f<< 24(in.readUnsignedByte() << 16(in.readUnsignedByte() << 8(in
          .readUnsignedByte()));
    }
  }

  /**
   * Read an integer previously written by writeInt().
   
   @exception IOException
   *              an exception was thrown by a method on in.
   */
  public static final int readInt(InputStream inthrows IOException {

    int value = InputStreamUtil.readUnsignedByte(in);

    if ((value & ~0x3f== 0) {
      return (value);
    else if ((value & 0x80== 0) {
      return (((value & 0x3f<< 8| InputStreamUtil.readUnsignedByte(in));
    else {
      return (((value & 0x7f<< 24(InputStreamUtil.readUnsignedByte(in<< 16)
          (InputStreamUtil.readUnsignedByte(in<< 8(InputStreamUtil.readUnsignedByte(in)));
    }
  }

  public static final int readInt(byte[] data, int offset) {
    int value = data[offset++];

    if ((value & ~0x3f== 0) {
      // length is stored in this byte, we also know that the 0x80 bit
      // was not set, so no need to mask off the sign extension from
      // the byte to int conversion.

      return (value);
    else if ((value & 0x80== 0) {
      // length is stored in 2 bytes. only use low 6 bits from 1st byte.

      // top 8 bits of 2 byte length is stored in this byte, we also
      // know that the 0x80 bit was not set, so no need to mask off the
      // sign extension from the 1st byte to int conversion. Need to
      // mask the byte in data[offset + 1] to account for possible sign
      // extension.

      return (((value & 0x3f<< 8(data[offset0xff));
    else {

      // top 8 bits of 4 byte length is stored in this byte, we also
      // know that the 0x80 bit was set, so need to mask off the
      // sign extension from the 1st byte to int conversion. Need to
      // mask the bytes from the next 3 bytes data[offset + 1,2,3] to
      // account for possible sign extension.
      //
      return (((value & 0x7f<< 24((data[offset++0xff<< 16)
          ((data[offset++0xff<< 8((data[offset0xff)));
    }
  }

  /**
   * Skip an integer previously written by writeInt().
   
   @exception IOException
   *              an exception was thrown by a method on in.
   */

  /**
   * Return the number of bytes that would be written by a writeInt call
   */
  public static final int sizeInt(int value) {
    if (value <= 0x3f) {
      return 1;
    }
    if (value <= 0x3fff) {
      return 2;
    }
    return 4;
  }

  /**
   * Write a compressed long only supporting signed values.
   
   * Formats are (with x representing value bits):
   
   * <PRE>
   
   * 2 byte - 00xxxxxx xxxxxxxx Represents the value <= 16383 (0x3fff) 4 byte -
   * 01xxxxxx xxxxxxxx xxxxxxxx xxxxxxxx Represents the value > 16383 && <=
   * 0x3fffffff 8 byte - 1xxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
   * xxxxxxxx xxxxxxxx Represents the value > 0x3fffffff && <= MAX_LONG
   
   * </PRE>
   
   
   @exception IOException
   *              value is negative or an exception was thrown by a method on
   *              out.
   */
  public static final int writeLong(DataOutput out, long valuethrows IOException {

    if (value < 0)
      throw new IOException();

    if (value <= 0x3fff) {

      out.writeByte((int) ((value >>> 80xff));
      out.writeByte((int) ((value0xff));
      return 2;
    }

    if (value <= 0x3fffffff) {

      out.writeByte((int) (((value >>> 240x400xff));
      out.writeByte((int) ((value >>> 160xff));
      out.writeByte((int) ((value >>> 80xff));
      out.writeByte((int) ((value0xff));
      return 4;
    }

    out.writeByte((int) (((value >>> 560x800xff));
    out.writeByte((int) ((value >>> 480xff));
    out.writeByte((int) ((value >>> 400xff));
    out.writeByte((int) ((value >>> 320xff));
    out.writeByte((int) ((value >>> 240xff));
    out.writeByte((int) ((value >>> 160xff));
    out.writeByte((int) ((value >>> 80xff));
    out.writeByte((int) ((value0xff));
    return 8;
  }

  /**
   * Write a compressed integer only supporting signed values.
   
   @exception IOException
   *              value is negative or an exception was thrown by a method on
   *              out.
   */
  public static final int writeLong(OutputStream out, long valuethrows IOException {

    if (value < 0)
      throw new IOException();

    if (value <= 0x3fff) {

      out.write((int) ((value >>> 80xff));
      out.write((int) ((value0xff));
      return 2;
    }

    if (value <= 0x3fffffff) {

      out.write((int) (((value >>> 240x400xff));
      out.write((int) ((value >>> 160xff));
      out.write((int) ((value >>> 80xff));
      out.write((int) ((value0xff));
      return 4;
    }

    out.write((int) (((value >>> 560x800xff));
    out.write((int) ((value >>> 480xff));
    out.write((int) ((value >>> 400xff));
    out.write((int) ((value >>> 320xff));
    out.write((int) ((value >>> 240xff));
    out.write((int) ((value >>> 160xff));
    out.write((int) ((value >>> 80xff));
    out.write((int) ((value0xff));
    return 8;
  }

  /**
   * Read a long previously written by writeLong().
   
   @exception IOException
   *              an exception was thrown by a method on in.
   */
  public static final long readLong(DataInput inthrows IOException {

    int int_value = in.readUnsignedByte();

    if ((int_value & ~0x3f== 0) {
      // test for small case first - assuming this is usual case.
      // this is stored in 2 bytes.

      return ((int_value << 8| in.readUnsignedByte());
    else if ((int_value & 0x80== 0) {
      // value is stored in 4 bytes. only use low 6 bits from 1st byte.

      return (((int_value & 0x3f<< 24(in.readUnsignedByte() << 16)
          (in.readUnsignedByte() << 8(in.readUnsignedByte()));
    else {
      // value is stored in 8 bytes. only use low 7 bits from 1st byte.
      return ((((long) (int_value & 0x7f)) << 56(((longin.readUnsignedByte()) << 48)
          (((longin.readUnsignedByte()) << 40(((longin.readUnsignedByte()) << 32)
          (((longin.readUnsignedByte()) << 24(((longin.readUnsignedByte()) << 16)
          (((longin.readUnsignedByte()) << 8(((longin.readUnsignedByte())));
    }
  }

  /**
   * Read a long previously written by writeLong().
   
   @exception IOException
   *              an exception was thrown by a method on in.
   */
  public static final long readLong(InputStream inthrows IOException {

    int int_value = InputStreamUtil.readUnsignedByte(in);

    if ((int_value & ~0x3f== 0) {
      // test for small case first - assuming this is usual case.
      // this is stored in 2 bytes.

      return ((int_value << 8| InputStreamUtil.readUnsignedByte(in));
    else if ((int_value & 0x80== 0) {
      // value is stored in 4 bytes. only use low 6 bits from 1st byte.

      return (((int_value & 0x3f<< 24(InputStreamUtil.readUnsignedByte(in<< 16)
          (InputStreamUtil.readUnsignedByte(in<< 8(InputStreamUtil.readUnsignedByte(in)));

    else {
      // value is stored in 8 bytes. only use low 7 bits from 1st byte.
      long value = int_value;

      return ((((long) (value & 0x7f)) << 56)
          (((longInputStreamUtil.readUnsignedByte(in)) << 48)
          (((longInputStreamUtil.readUnsignedByte(in)) << 40)
          (((longInputStreamUtil.readUnsignedByte(in)) << 32)
          (((longInputStreamUtil.readUnsignedByte(in)) << 24)
          (((longInputStreamUtil.readUnsignedByte(in)) << 16)
          (((longInputStreamUtil.readUnsignedByte(in)) << 8(((longInputStreamUtil
          .readUnsignedByte(in))));
    }
  }

  public static final long readLong(byte[] data, int offset) {
    int int_value = data[offset++];

    if ((int_value & ~0x3f== 0) {
      // test for small case first - assuming this is usual case.
      // this is stored in 2 bytes.

      return ((int_value << 8(data[offset0xff));
    else if ((int_value & 0x80== 0) {
      // value is stored in 4 bytes. only use low 6 bits from 1st byte.

      return (((int_value & 0x3f<< 24((data[offset++0xff<< 16)
          ((data[offset++0xff<< 8((data[offset0xff)));

    else {
      // value is stored in 8 bytes. only use low 6 bits from 1st byte.
      return ((((long) (int_value & 0x7f)) << 56(((long) (data[offset++0xff)) << 48)
          (((long) (data[offset++0xff)) << 40(((long) (data[offset++0xff)) << 32)
          (((long) (data[offset++0xff)) << 24(((long) (data[offset++0xff)) << 16)
          (((long) (data[offset++0xff)) << 8(((long) (data[offset0xff))));

    }
  }

  public static final int sizeLong(long value) {

    if (value <= 0x3fff) {

      return 2;
    }

    if (value <= 0x3fffffff) {
      return 4;
    }

    return 8;
  }
}

/*
 
 * Derby - Class org.apache.derby.iapi.services.io.InputStreamUtil
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with this
 * work for additional information regarding copyright ownership. The ASF
 * licenses this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 
 * http://www.apache.org/licenses/LICENSE-2.0
 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 
 */

/**
 * Utility methods for InputStream that are stand-ins for a small subset of
 * DataInput methods. This avoids pushing a DataInputStream just to get this
 * functionality.
 */
final class InputStreamUtil {
  private static final int SKIP_FRAGMENT_SIZE = Integer.MAX_VALUE;

  /**
   * Read an unsigned byte from an InputStream, throwing an EOFException if the
   * end of the input is reached.
   
   @exception IOException
   *              if an I/O error occurs.
   @exception EOFException
   *              if the end of the stream is reached
   
   @see DataInput#readUnsignedByte
   
   */
  public static int readUnsignedByte(InputStream inthrows IOException {
    int b = in.read();
    if (b < 0)
      throw new EOFException();

    return b;
  }

  /**
   * Read a number of bytes into an array.
   
   @exception IOException
   *              if an I/O error occurs.
   @exception EOFException
   *              if the end of the stream is reached
   
   @see DataInput#readFully
   
   */
  public static void readFully(InputStream in, byte b[]int offset, int lenthrows IOException {
    do {
      int bytesRead = in.read(b, offset, len);
      if (bytesRead < 0)
        throw new EOFException();
      len -= bytesRead;
      offset += bytesRead;
    while (len != 0);
  }

  /**
   * Read a number of bytes into an array. Keep reading in a loop until len
   * bytes are read or EOF is reached or an exception is thrown. Return the
   * number of bytes read. (InputStream.read(byte[],int,int) does not guarantee
   * to read len bytes even if it can do so without reaching EOF or raising an
   * exception.)
   
   @exception IOException
   *              if an I/O error occurs.
   */
  public static int readLoop(InputStream in, byte b[]int offset, int lenthrows IOException {
    int firstOffset = offset;
    do {
      int bytesRead = in.read(b, offset, len);
      if (bytesRead <= 0)
        break;
      len -= bytesRead;
      offset += bytesRead;
    while (len != 0);
    return offset - firstOffset;
  }

  /**
   * Skips until EOF, returns number of bytes skipped.
   
   @param is
   *          InputStream to be skipped.
   @return number of bytes skipped in fact.
   @throws IOException
   *           if IOException occurs. It doesn't contain EOFException.
   @throws NullPointerException
   *           if the param 'is' equals null.
   */
  public static long skipUntilEOF(InputStream isthrows IOException {
    if (is == null)
      throw new NullPointerException();

    long bytes = 0;
    while (true) {
      long r = skipPersistent(is, SKIP_FRAGMENT_SIZE);
      bytes += r;
      if (r < SKIP_FRAGMENT_SIZE)
        return bytes;
    }
  }

  /**
   * Skips requested number of bytes, throws EOFException if there is too few
   * bytes in the stream.
   
   @param is
   *          InputStream to be skipped.
   @param skippedBytes
   *          number of bytes to skip. if skippedBytes <= zero, do nothing.
   @throws EOFException
   *           if EOF meets before requested number of bytes are skipped.
   @throws IOException
   *           if IOException occurs. It doesn't contain EOFException.
   @throws NullPointerException
   *           if the param 'is' equals null.
   */
  public static void skipFully(InputStream is, long skippedBytesthrows IOException {
    if (is == null)
      throw new NullPointerException();

    if (skippedBytes <= 0)
      return;

    long bytes = skipPersistent(is, skippedBytes);

    if (bytes < skippedBytes)
      throw new EOFException();
  }

  /**
   * Tries harder to skip the requested number of bytes.
   * <p>
   * Note that even if the method fails to skip the requested number of bytes,
   * it will not throw an exception. If this happens, the caller can be sure
   * that end-of-stream has been reached.
   
   @param in
   *          byte stream
   @param bytesToSkip
   *          the number of bytes to skip
   @return The number of bytes skipped.
   @throws IOException
   *           if reading from the stream fails
   */
  public static final long skipPersistent(InputStream in, long bytesToSkipthrows IOException {
    long skipped = 0;
    while (skipped < bytesToSkip) {
      long skippedNow = in.skip(bytesToSkip - skipped);
      if (skippedNow == 0) {
        if (in.read() == -1) {
          // EOF, return what we have and leave it up to caller to
          // decide what to do about it.
          break;
        else {
          skippedNow = 1// Added to count below.
        }
      }
      skipped += skippedNow;
    }
    return skipped;
  }
}

   
    
  
Related examples in the same category
1. Byte Reader with FileInputStream
2. Byte Writer with FileOutputStream
3. Binary Dump OutputStream
4. Compare binary files
5. Reads bytes available from one InputStream and returns these bytes in a byte array.
6. Buffered copying between source(InputStream, Reader, String and byte[]) and destinations (OutputStream, Writer, String and byte[]).
7. Count the number of bytes written to the output stream.
8. Read and return the entire contents of the supplied file.
9. Convert 4 hex digits to an int, and return the number of converted bytes.
10. Get bytes from InputStream
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.