FileDocCategorySizeDatePackage
DataInOutUtils.javaAPI DocExample12860Tue May 29 16:57:08 BST 2007com.sun.xml.ws.transport.tcp.io

DataInOutUtils

public final class DataInOutUtils extends Object
author
Alexey Stashok

Fields Summary
Constructors Summary
Methods Summary
public static voidreadFully(java.io.InputStream inputStream, byte[] buffer)

        readFully(inputStream, buffer, 0, buffer.length);
    
public static voidreadFully(java.io.InputStream inputStream, byte[] buffer, int offset, int length)

        int bytesRead = 0;
        while(bytesRead < length) {
            final int count = inputStream.read(buffer, offset + bytesRead, length - bytesRead);
            if (count < 0) {
                throw new EOFException();
            }
            bytesRead += count;
        }
    
public static intreadInt4(java.io.InputStream is)

        int value = 0;
        
        for(int shVal = 0, neeble = 8; (neeble & 8) != 0; shVal += 6) {
            final int octet = is.read();
            if (octet == -1) {
                throw new EOFException();
            }
            neeble = octet >> 4;
            
            value |= ((neeble & 7) << shVal);
            if ((neeble & 8) != 0) {
                neeble = octet & 0xF;
                value |= ((neeble & 7) << (shVal + 3));
            }
        }
        
        return value;
    
public static intreadInt4(java.nio.ByteBuffer buffer)

        int value = 0;
        
        for(int shVal = 0, neeble = 8; (neeble & 8) != 0; shVal += 6) {
            if (!buffer.hasRemaining()) {
                throw new EOFException();
            }
            
            final int octet = buffer.get();
            neeble = octet >> 4;
            
            value |= ((neeble & 7) << shVal);
            if ((neeble & 8) != 0) {
                neeble = octet & 0xF;
                value |= ((neeble & 7) << (shVal + 3));
            }
        }
        
        return value;
    
public static intreadInt8(java.nio.ByteBuffer buffer)

        int value = 0;
        for(int shVal = 0, octet = 0x80; (octet & 0x80) != 0; shVal += 7) {
            if (!buffer.hasRemaining()) {
                throw new EOFException();
            }
            
            octet = buffer.get();
            
            value |= ((octet & 0x7F) << shVal);
        }
        
        return value;
    
public static intreadInt8(java.io.InputStream is)

        int value = 0;
        for(int shVal = 0, octet = 0x80; (octet & 0x80) != 0; shVal += 7) {
            octet = is.read();
            if (octet == -1) {
                throw new EOFException();
            }
            
            value |= ((octet & 0x7F) << shVal);
        }
        
        return value;
    
public static voidreadInts4(java.io.InputStream is, int[] array, int count)

        readInts4(is, array, count, 0);
    
public static intreadInts4(java.io.InputStream is, int[] array, int count, int lowValue)

        int value = 0;
        int octet = 0;
        int readInts = 0;
        int shVal = 0;
        int neeble = 0;
        int neebleNum = 0;
        
        if (lowValue > 0) {
            octet = lowValue & 0xF;
            neebleNum = 1;
        }
        
        for(; readInts < count; neebleNum++) {
            if (neebleNum % 2 == 0) {
                octet = is.read();
                if (octet == -1) {
                    throw new EOFException();
                }
                neeble = octet >> 4;
            } else {
                neeble = octet & 0xF;
            }
            
            value |= ((neeble & 7) << shVal);
            if ((neeble & 8) == 0) {
                array[readInts++] = value;
                shVal = 0;
                value = 0;
            } else {
                shVal += 3;
            }
        }
        
        if (neebleNum % 2 != 0) {
            return 0x80 | (octet & 0xF);
        }

        return 0;
    
public static voidreadInts4(java.nio.ByteBuffer buffer, int[] array, int count)

        readInts4(buffer, array, count, 0);
    
public static intreadInts4(java.nio.ByteBuffer buffer, int[] array, int count, int lowValue)

        int value = 0;
        int octet = 0;
        int readInts = 0;
        int shVal = 0;
        int neeble = 0;
        int neebleNum = 0;
        
        if (lowValue > 0) {
            octet = lowValue & 0xF;
            neebleNum = 1;
        }
        
        for(; readInts < count; neebleNum++) {
            if (neebleNum % 2 == 0) {
                if (!buffer.hasRemaining()) {
                    throw new EOFException();
                }
                octet = buffer.get();
                
                neeble = octet >> 4;
            } else {
                neeble = octet & 0xF;
            }
            
            value |= ((neeble & 7) << shVal);
            if ((neeble & 8) == 0) {
                array[readInts++] = value;
                shVal = 0;
                value = 0;
            } else {
                shVal += 3;
            }
        }
        
        if (neebleNum % 2 != 0) {
            return 0x80 | (octet & 0xF);
        }

        return 0;
    
public static intwriteInt4(java.io.OutputStream out, int value, int highValue, boolean flush)

        int nibbleL;
        int nibbleH;
        
        if (highValue > 0) {
            highValue &= 0x70; // clear highest bit
            nibbleL = value & 7;
            value >>>= 3;
            if (value != 0) {
                nibbleL |= 8;
            }
            
            out.write(highValue | nibbleL);
            
            if (value == 0) {
                return 0;
            }
        }
        
        do {
            // shift nibbleH to high byte's bits
            nibbleH = (value & 7) << 4;
            value >>>= 3;
            
            if (value != 0) {
                nibbleH |= 0x80;
                nibbleL = value & 7;
                value >>>= 3;
                if (value != 0) {
                    nibbleL |= 8;
                }
            } else {
                if (!flush) {
                    return nibbleH | 0x80;
                }
                
                nibbleL = 0;
            }
            
            out.write(nibbleH | nibbleL);
        } while(value != 0);
        
        return 0;
    
public static intwriteInt4(java.nio.ByteBuffer bb, int value, int highValue, boolean flush)

        int nibbleL;
        int nibbleH;
        
        if (highValue > 0) {
            highValue &= 0x70; // clear highest bit
            nibbleL = value & 7;
            value >>>= 3;
            if (value != 0) {
                nibbleL |= 8;
            }
            
            bb.put((byte) (highValue | nibbleL));
            
            if (value == 0) {
                return 0;
            }
        }
        
        do {
            // shift nibbleH to high byte's bits
            nibbleH = (value & 7) << 4;
            value >>>= 3;
            
            if (value != 0) {
                nibbleH |= 0x80;
                nibbleL = value & 7;
                value >>>= 3;
                if (value != 0) {
                    nibbleL |= 8;
                }
            } else {
                if (!flush) {
                    return nibbleH | 0x80;
                }
                
                nibbleL = 0;
            }
            
            bb.put((byte) (nibbleH | nibbleL));
        } while(value != 0);
        
        return 0;
    
public static voidwriteInt4(java.io.OutputStream os, int value)

        int nibbleL;
        int nibbleH;
        do {
            nibbleH = value & 7;
            value >>>= 3;
            
            if (value != 0) {
                nibbleH |= 8;
                nibbleL = value & 7;
                value >>>= 3;
                if (value != 0) {
                    nibbleL |= 8;
                }
            } else {
                nibbleL = 0;
            }
            
            os.write(nibbleL | (nibbleH << 4));
        } while(value != 0);
    
public static voidwriteInt8(java.io.OutputStream os, int value)

        int octet;
        do {
            octet = value & 0x7F;
            value >>>= 7;
            
            if (value != 0) {
                octet |= 0x80;
            }
            
            os.write(octet);
        } while(value != 0);
    
public static voidwriteInt8(java.nio.ByteBuffer bb, int value)

        int octet;
        do {
            octet = value & 0x7F;
            value >>>= 7;
            
            if (value != 0) {
                octet |= 0x80;
            }
            
            bb.put((byte) octet);
        } while(value != 0);
    
public static voidwriteInts4(java.nio.ByteBuffer bb, int values)

        writeInts4(bb, values, 0, values.length);
    
public static voidwriteInts4(java.nio.ByteBuffer bb, int[] array, int offset, int count)

        int shiftValue = 0;
        for(int i=0; i<count - 1; i++) {
            final int value = array[offset + i];
            shiftValue = writeInt4(bb, value, shiftValue, false);
        }
        
        if (count > 0) {
            writeInt4(bb, array[offset + count - 1], shiftValue, true);
        }
    
public static voidwriteInts4(java.io.OutputStream out, int values)

        writeInts4(out, values, 0, values.length);
    
public static voidwriteInts4(java.io.OutputStream out, int[] array, int offset, int count)

        int shiftValue = 0;
        for(int i=0; i<count - 1; i++) {
            final int value = array[offset + i];
            shiftValue = writeInt4(out, value, shiftValue, false);
        }
        
        if (count > 0) {
            writeInt4(out, array[offset + count - 1], shiftValue, true);
        }