Java DataInput Read Long readLongUTF8(DataInput in)

Here you can find the source of readLongUTF8(DataInput in)

Description

Read a UTF8 String previously written with writeLongUTF8.

License

Open Source License

Declaration

public static String readLongUTF8(DataInput in) throws IOException 

Method Source Code


//package com.java2s;
/*/*  w w  w.  j a v a 2 s. c o m*/
 * Copyright (c) 1998 - 2005 Versant Corporation
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Versant Corporation - initial API and implementation
 */

import java.io.*;

public class Main {
    /**
     * Read a UTF8 String previously written with writeLongUTF8.  This code
     * was cut and pasted from ObjectInputStream. This method will be slow
     * if in is not buffered.
     *
     * @see #writeLongUTF8(String, DataOutput)
     */
    public static String readLongUTF8(DataInput in) throws IOException {
        int len = in.readInt();
        if (len == 0)
            return "";
        char[] cbuf = new char[len];
        for (int i = 0; i < len; i++) {
            int b1, b2, b3;
            b1 = in.readUnsignedByte();
            switch (b1 >> 4) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7: // 1 byte format: 0xxxxxxx
                cbuf[i] = (char) b1;
                break;

            case 12:
            case 13: // 2 byte format: 110xxxxx 10xxxxxx
                b2 = in.readUnsignedByte();
                if ((b2 & 0xC0) != 0x80) {
                    throw new UTFDataFormatException();
                }
                cbuf[i] = (char) (((b1 & 0x1F) << 6) | ((b2 & 0x3F) << 0));
                break;

            case 14: // 3 byte format: 1110xxxx 10xxxxxx 10xxxxxx
                b2 = in.readUnsignedByte();
                b3 = in.readUnsignedByte();
                if ((b2 & 0xC0) != 0x80 || (b3 & 0xC0) != 0x80) {
                    throw new UTFDataFormatException();
                }
                cbuf[i] = (char) (((b1 & 0x0F) << 12) | ((b2 & 0x3F) << 6) | ((b3 & 0x3F) << 0));
                break;

            default: // 10xx xxxx, 1111 xxxx
                throw new UTFDataFormatException();
            }
        }
        return new String(cbuf);
    }
}

Related

  1. readLong(DataInput dataInput)
  2. readLong(DataInput in)
  3. readLong(DataInput in)
  4. readLongSequence(DataInput in)