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

fr.dyade.aaa.agent.ByteArrayMessageInputStream Maven / Gradle / Ivy

There is a newer version: 5.22.0-EFLUID
Show newest version
/*
 * Copyright (C) 2008 ScalAgent Distributed Technologies 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA.
 */
package fr.dyade.aaa.agent;

import java.io.IOException;

/**
 * Class used to recv messages through a stream in a byte array.
 * 

* Be careful this InputStream is not synchronized. */ public abstract class ByteArrayMessageInputStream extends MessageInputStream { /** * Creates a ByteArrayMessageInputStream that uses * buf as its buffer array. *

* Be careful, the buffer array is not copied. The initial value of * pos is 0 and the initial value of count * is the length of buf. * * @param buf the input buffer. */ public ByteArrayMessageInputStream(byte[] buf) { this(buf, 0, buf.length); } /** * Creates ByteArrayMessageInputStream that uses * buf as its buffer array. *

* Be careful, the buffer array is not copied. The initial value of * pos is offset and the initial value of * count is the minimum of offset+length and * buf.length. * * @param buf the input buffer. * @param offset the offset in the buffer of the first byte to read. * @param length the maximum number of bytes to read from the buffer. */ public ByteArrayMessageInputStream(byte[] buf, int offset, int length) { this.buf = buf; pos = offset; count = Math.min(offset + length, buf.length); } /** * Reads the next byte of data from the input stream. The value byte is * returned as an int in the range 0 to * 255. If no byte is available because the end of the stream * has been reached, the value -1 is returned. This method * blocks until input data is available, the end of the stream is detected, * or an exception is thrown. * * @return the next byte of data, or -1 if the end of the * stream is reached. */ public final int read() { return (pos < count)?(buf[pos++] & 0xff):-1; } /** * Reads up to len bytes of data from the input stream into * an array of bytes. An attempt is made to read as many as * len bytes, but a smaller number may be read. * The number of bytes actually read is returned as an integer. *

* This method blocks until input data is available, end of file is * detected, or an exception is thrown. * * @param b the buffer into which the data is read. * @param off the start offset in array b * at which the data is written. * @param len the maximum number of bytes to read. * @return the total number of bytes read into the buffer, or * -1 if there is no more data because the end of * the stream has been reached. * @exception NullPointerException If b is null. * @exception IndexOutOfBoundsException If off is negative, * len is negative, or len is greater than * b.length - off */ public final int read(byte b[], int off, int len) throws IOException { if (b == null) throw new NullPointerException(); if (off < 0 || len < 0 || len > b.length - off) throw new IndexOutOfBoundsException(); if (pos >= count) return -1; if (pos + len > count) len = count - pos; if (len <= 0) return 0; System.arraycopy(buf, pos, b, off, len); pos += len; return len; } /** * Reads length bytes of data from the input stream. */ protected final void readFully(int length) throws IOException { // Data are fully available in the buffer. } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy