/**
*
* @author eagle.sakura
* @version 2009/11/15 :
*/
package eagle.io;
import java.io.IOException;
import java.io.InputStream;
import eagle.util.Disposable;
import eagle.util.EagleUtil;
/**
*
*
* @author eagle.sakura
* @version 2009/11/15 :
*/
public class DataInputStream implements Disposable {
/**
*
*/
private IBufferReader reader = null;
/**
*
*/
private ByteOrder order = ByteOrder.eThrough;
/**
*
*
* @author eagle.sakura
* @param br
* @version 2009/11/15 :
*/
public DataInputStream(IBufferReader br) {
reader = br;
}
/**
*
* @author eagle.sakura
* @param is
* @version 2010/06/23 :
*/
public DataInputStream(InputStream is) {
this(new InputStreamBufferReader(is));
}
/**
*
*
* @author eagle.sakura
* @return
* @version 2010/06/21 :
*/
public IBufferReader getReader() {
return reader;
}
/**
* <BR>
* {@link ByteOrder#eReversing}
* {@link ByteOrder#eThrough}<BR>
* {@link ByteOrder#eThrough}
*
* @author eagle.sakura
* @param set
* @version 2010/06/21 :
*/
public void setByteOrder(ByteOrder set) {
order = set;
}
/**
*
*
* @author eagle.sakura
* @return
* @version 2009/11/15 :
*/
public byte readS8() throws IOException {
byte[] n = { 0 };
reader.readBuffer(n, 0, n.length);
order.encode(n, 1, 1);
return n[0];
}
/**
* 2
*
* @author eagle.sakura
* @return
* @version 2009/11/15 :
*/
public short readS16() throws IOException {
byte[] n = { 0, 0 };
reader.readBuffer(n, 0, n.length);
order.encode(n, 2, 1);
int n0 = ((int) n[0] & 0xff);
int n1 = ((int) n[1] & 0xff);
return (short) ((n0 << 8) | (n1 << 0));
}
/**
* 3<BR>
*
*
* @author eagle.sakura
* @return
* @version 2009/11/15 :
*/
public int readS24() throws IOException {
byte[] n = { 0, 0, 0 };
reader.readBuffer(n, 0, n.length);
order.encode(n, 3, 1);
return (int) (((((int) n[0]) & 0xff) << 16) | ((((int) n[1]) & 0xff) << 8) | ((((int) n[2]) & 0xff) << 0));
}
/**
*
*
* @author eagle.sakura
* @return int
* @version 2009/08/28 :
*/
public int readU8() throws IOException {
return (((int) readS8()) & 0xff);
}
/**
*
*
* @author eagle.sakura
* @return int
* @version 2009/09/20 :
*/
public int readU16() throws IOException {
return (((int) readS16()) & 0xffff);
}
/**
* 4
*
* @author eagle.sakura
* @return
* @version 2009/11/15 :
*/
public int readS32() throws IOException {
byte[] n = { 0, 0, 0, 0 };
reader.readBuffer(n, 0, n.length);
order.encode(n, 4, 1);
int n0 = ((int) n[0] & 0xff);
int n1 = ((int) n[1] & 0xff);
int n2 = ((int) n[2] & 0xff);
int n3 = ((int) n[3] & 0xff);
return (n0 << 24) | (n1 << 16) | (n2 << 8) | (n3 << 0);
}
/**
*
*
* @author eagle.sakura
* @return
* @version 2010/03/24 :
*/
public long readS64() throws IOException {
byte[] n = { 0, 0, 0, 0, 0, 0, 0, 0 };
reader.readBuffer(n, 0, n.length);
order.encode(n, 8, 1);
int n0 = ((int) n[0] & 0xff);
int n1 = ((int) n[1] & 0xff);
int n2 = ((int) n[2] & 0xff);
int n3 = ((int) n[3] & 0xff);
int n4 = ((int) n[4] & 0xff);
int n5 = ((int) n[5] & 0xff);
int n6 = ((int) n[6] & 0xff);
int n7 = ((int) n[7] & 0xff);
return (((n0 << 24) | (n1 << 16) | (n2 << 8) | (n3 << 0)) << 32) | ((n4 << 24) | (n5 << 16) | (n6 << 8) | (n7 << 0));
}
/**
* float<BR>
* GL1 15 16
*
* @author eagle.sakura
* @return
* @version 2009/11/23 :
*/
public float readGLFixedFloat() throws IOException {
return ((float) readS32()) / (float) EagleUtil.eGLFixed1_0;
}
/**
* double<BR>
* GL1 47 16
*
* @author eagle.sakura
* @return
* @version 2010/03/24 :
*/
public double readGLFixedDouble() throws IOException {
return ((double) readS64()) / (double) EagleUtil.eGLFixed1_0;
}
/**
* IEEE754float
*
* @author eagle.sakura
* @return
* @throws IOException
* @version 2010/04/19 :
*/
public float readFloat() throws IOException {
return Float.intBitsToFloat(readS32());
}
/**
* IEEE754double
*
* @author eagle.sakura
* @return
* @throws IOException
* @version 2010/04/19 :
*/
public double readDouble() throws IOException {
return Double.longBitsToDouble(readS64());
}
/**
* <BR>
* 1byte0falsetrue
*
* @author eagle.sakura
* @return
* @throws IOException
* @version 2010/05/28 :
*/
public boolean readBoolean() throws IOException {
return readS8() == 0 ? false : true;
}
/**
* <BR>
* ShiftJIS<BR>
* 2byte
*
* @author eagle.sakura
* @return
* @version 2009/11/15 :
*/
public String readString() throws IOException {
int len = readS16();
if (len <= 0) {
return "";
}
byte[] buf = new byte[len];
readBuffer(buf, len);
return new String(buf, EagleUtil.eEncodeSJIS);
}
/**
*
*
* @author eagle.sakura
* @param length
* @return
* @version 2009/11/15 :
*/
public byte[] readBuffer(int length) throws IOException {
byte[] ret = new byte[length];
readBuffer(ret, length);
return ret;
}
/**
*
*
* @author eagle.sakura
* @return
* @version 2010/02/22 :
*/
public byte[] readFile() throws IOException {
int len = readS32();
byte[] ret = readBuffer(len);
return ret;
}
/**
*
*
* @author eagle.sakura
* @param buf
* @param length
* @version 2009/11/15 :
*/
public void readBuffer(byte[] buf, int length) throws IOException {
readBuffer(buf, 0, length);
}
/**
*
* @author eagle.sakura
* @param buf
* @param index
* @param length
* @version 2009/11/15 :
*/
public int readBuffer(byte[] buf, int index, int length) throws IOException {
return reader.readBuffer(buf, index, length);
}
/**
* <BR>
* {@link #reader}dispose()
*
* @author eagle.sakura
* @version 2009/11/15 :
*/
public void dispose() {
if (reader != null) {
reader.dispose();
reader = null;
}
}
/**
*
* @author eagle.sakura
* @throws Throwable
* @version 2010/07/12 :
*/
protected void finalize() throws Throwable {
super.finalize();
dispose();
}
/**
*
*
* @author eagle.sakura
* @param eSeekType
* @param pos
* @version 2009/11/15 :
*/
public void seek(int eSeekType, int pos) throws IOException {
reader.seek(eSeekType, pos);
}
}
|