datamarshalinputstreamimpl.java

来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 361 行

JAVA
361
字号
/* $Id: DataMarshalInputStreamImpl.java,v 1.3 2003/09/27 03:01:11 giuli Exp $*/
/**************************************************************************
 * Copyright 2002, 2003 SRI International. All rights reserved.
 *
 * The material contained in this file is confidential and proprietary to SRI
 * International and may not be reproduced, published, or disclosed to others
 * without authorization from SRI International.
 *
 * DISCLAIMER OF WARRANTIES
 *
 * SRI International MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
 * LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SRI International SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THIS SOFTWARE
 **************************************************************************/
package com.sri.sedc.javanetbridge.io;

import java.io.IOException;
import java.io.InputStream;
import java.io.EOFException;
import java.io.DataInputStream;
import java.io.BufferedInputStream;

/**
 * Default implementation of DataMarshalInputStream.
 */
class DataMarshalInputStreamImpl implements DataMarshalInputStream {
    final static int BYTE_RANGE = Byte.MAX_VALUE - Byte.MIN_VALUE + 1;

    private DataInputStream in;

    final int makeByteAsInt(byte b) {
        if (b >= 0) {
            return b;
        } else {
            return b + BYTE_RANGE;
        }
    }

    DataMarshalInputStreamImpl(InputStream in) {
        this.in = new DataInputStream(new BufferedInputStream(in, 10240));
    }

    public byte readByte() throws IOException {
        return in.readByte();
    }

    public short readShort() throws IOException {
        return in.readShort();
    }

    public int readInt() throws IOException {
        return in.readInt();
    }

    public float readFloat() throws IOException {
        return (float)in.readDouble();
    }

    public long readLong() throws IOException {
        return in.readLong();
    }

    public double readDouble() throws IOException {
        return in.readDouble();
    }

    public char readChar() throws IOException {
        return in.readChar();
    }

    public boolean readBoolean() throws IOException {
        return in.readBoolean();
    }

    public String readString() throws IOException {
        return new String(readChar1D());
    }

    public byte[] readByte1D() throws IOException {
        int len = readInt();
        if (len < 0) {
            throw new IOException("Received negative length for array: " + len);
        }
        byte[] ret = new byte[len];
        readFully(in, ret, 0, ret.length);
        return ret;
    }

    public void readFully(InputStream in, byte[] b, int off, int len) throws IOException {
        // Copied from the DataInputStream class
        if (len < 0)
            throw new IndexOutOfBoundsException();
        int n = 0;
        while (n < len) {
            int count = in.read(b, off + n, len - n);
            if (count < 0)
            throw new EOFException();
            n += count;
        }
    }

    public short[] readShort1D() throws IOException {
        int len = readInt();
        if (len < 0) {
            throw new IOException("Received negative length for array: " + len);
        }
        short[] ret = new short[len];
        for (int i = 0; i < ret.length; i++) {
            ret[i] = readShort();
        }
        return ret;
    }

    public int[] readInt1D() throws IOException {
        int len = readInt();
        if (len < 0) {
            throw new IOException("Received negative length for array: " + len);
        }
        int[] ret = new int[len];
        for (int i = 0; i < ret.length; i++) {
            ret[i] = readInt();
        }
        return ret;
    }

    public float[] readFloat1D() throws IOException {
        int len = readInt();
        if (len < 0) {
            throw new IOException("Received negative length for array: " + len);
        }
        float[] ret = new float[len];
        for (int i = 0; i < ret.length; i++) {
            ret[i] = readFloat();
        }
        return ret;
    }

    public long[] readLong1D() throws IOException {
        int len = readInt();
        if (len < 0) {
            throw new IOException("Received negative length for array: " + len);
        }
        long[] ret = new long[len];
        for (int i = 0; i < ret.length; i++) {
            ret[i] = readLong();
        }
        return ret;
    }

    public double[] readDouble1D() throws IOException {
        int len = readInt();
        if (len < 0) {
            throw new IOException("Received negative length for array: " + len);
        }
        double[] ret = new double[len];
        for (int i = 0; i < ret.length; i++) {
            ret[i] = readDouble();
        }
        return ret;
    }

    public char[] readChar1D() throws IOException {
        int len = readInt();
        if (len < 0) {
            throw new IOException("Received negative length for array: " + len);
        }
        char[] ret = new char[len];
        for (int i = 0; i < ret.length; i++) {
            ret[i] = readChar();
        }
        return ret;
    }

    public boolean[] readBoolean1D() throws IOException {
        int len = readInt();
        if (len < 0) {
            throw new IOException("Received negative length for array: " + len);
        }
        boolean[] ret = new boolean[len];
        for (int i = 0; i < ret.length; i++) {
            ret[i] = readBoolean();
        }
        return ret;
    }

    public String[] readString1D() throws IOException {
        int len = readInt();
        if (len < 0) {
            throw new IOException("Received negative length for array: " + len);
        }
        String[] ret = new String[len];
        for (int i = 0; i < ret.length; i++) {
            ret[i] = readString();
        }
        return ret;
    }

    public byte[][] readByte2D() throws IOException {
        int len = readInt();
        int len2 = readInt();
        if (len < 0) {
            throw new IOException("Received negative length for array: " + len);
        }
        if (len2 < 0) {
            throw new IOException("Received negative length for array: " + len2);
        }
        byte[][] ret = new byte[len][len2];
        for (int i = 0; i < len; i++) {
            readFully(in, ret[i], 0, len2);
        }
        return ret;
    }

    public short[][] readShort2D() throws IOException {
        int len = readInt();
        int len2 = readInt();
        if (len < 0) {
            throw new IOException("Received negative length for array: " + len);
        }
        if (len2 < 0) {
            throw new IOException("Received negative length for array: " + len2);
        }
        short[][] ret = new short[len][len2];
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len2; j++) {
                ret[i][j] = readShort();
            }
        }
        return ret;
    }

    public int[][] readInt2D() throws IOException {
        int len = readInt();
        int len2 = readInt();
        if (len < 0) {
            throw new IOException("Received negative length for array: " + len);
        }
        if (len2 < 0) {
            throw new IOException("Received negative length for array: " + len2);
        }
        int[][] ret = new int[len][len2];
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len2; j++) {
                ret[i][j] = readInt();
            }
        }
        return ret;
    }

    public float[][] readFloat2D() throws IOException {
        int len = readInt();
        int len2 = readInt();
        if (len < 0) {
            throw new IOException("Received negative length for array: " + len);
        }
        if (len2 < 0) {
            throw new IOException("Received negative length for array: " + len2);
        }
        float[][] ret = new float[len][len2];
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len2; j++) {
                ret[i][j] = readFloat();
            }
        }
        return ret;
    }

    public long[][] readLong2D() throws IOException {
        int len = readInt();
        int len2 = readInt();
        if (len < 0) {
            throw new IOException("Received negative length for array: " + len);
        }
        if (len2 < 0) {
            throw new IOException("Received negative length for array: " + len2);
        }
        long[][] ret = new long[len][len2];
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len2; j++) {
                ret[i][j] = readLong();
            }
        }
        return ret;
    }

    public double[][] readDouble2D() throws IOException {
        int len = readInt();
        int len2 = readInt();
        if (len < 0) {
            throw new IOException("Received negative length for array: " + len);
        }
        if (len2 < 0) {
            throw new IOException("Received negative length for array: " + len2);
        }
        double[][] ret = new double[len][len2];
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len2; j++) {
                ret[i][j] = readDouble();
            }
        }
        return ret;
    }

    public char[][] readChar2D() throws IOException {
        int len = readInt();
        int len2 = readInt();
        if (len < 0) {
            throw new IOException("Received negative length for array: " + len);
        }
        if (len2 < 0) {
            throw new IOException("Received negative length for array: " + len2);
        }
        char[][] ret = new char[len][len2];
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len2; j++) {
                ret[i][j] = readChar();
            }
        }
        return ret;
    }

    public boolean[][] readBoolean2D() throws IOException {
        int len = readInt();
        int len2 = readInt();
        if (len < 0) {
            throw new IOException("Received negative length for array: " + len);
        }
        if (len2 < 0) {
            throw new IOException("Received negative length for array: " + len2);
        }
        boolean[][] ret = new boolean[len][len2];
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len2; j++) {
                ret[i][j] = readBoolean();
            }
        }
        return ret;
    }

    public String[][] readString2D() throws IOException {
        int len = readInt();
        int len2 = readInt();
        if (len < 0) {
            throw new IOException("Received negative length for array: " + len);
        }
        if (len2 < 0) {
            throw new IOException("Received negative length for array: " + len2);
        }
        String[][] ret = new String[len][len2];
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len2; j++) {
                ret[i][j] = readString();
            }
        }
        return ret;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?