ByteArrayByteStream.java
资源名称:src.rar [点击查看]
上传用户:gwt600
上传日期:2021-06-03
资源大小:704k
文件大小:3k
源码类别:
游戏
开发平台:
Java
- /*
- This file is part of the OdinMS Maple Story Server
- Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
- Matthias Butz <matze@odinms.de>
- Jan Christian Meyer <vimes@odinms.de>
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License version 3
- as published by the Free Software Foundation. You may not use, modify
- or distribute this program under any other version of the
- GNU Affero General Public License.
- This program 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 Affero General Public License for more details.
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package net.sf.odinms.tools.data.input;
- import java.io.IOException;
- import net.sf.odinms.tools.HexTool;
- /**
- * Provides for an abstraction layer for an array of bytes.
- *
- * @author Frz
- * @version 1.0
- * @since Revision 326
- */
- public class ByteArrayByteStream implements SeekableInputStreamBytestream {
- private int pos = 0;
- private long bytesRead = 0;
- private byte[] arr;
- /**
- * Class constructor.
- *
- * @param arr Array of bytes to wrap the stream around.
- */
- public ByteArrayByteStream(byte[] arr) {
- this.arr = arr;
- }
- /**
- * Gets the current position of the stream.
- *
- * @return The current position of the stream.
- * @see net.sf.odinms.tools.data.input.SeekableInputStreamBytestream#getPosition()
- */
- @Override
- public long getPosition() {
- return pos;
- }
- /**
- * Seeks the pointer the the specified position.
- *
- * @param offset The position you wish to seek to.
- * @see net.sf.odinms.tools.data.input.SeekableInputStreamBytestream#seek(long)
- */
- @Override
- public void seek(long offset) throws IOException {
- pos = (int) offset;
- }
- /**
- * Returns the numbers of bytes read from the stream.
- *
- * @return The number of bytes read.
- * @see net.sf.odinms.tools.data.input.ByteInputStream#getBytesRead()
- */
- @Override
- public long getBytesRead() {
- return bytesRead;
- }
- /**
- * Reads a byte from the current position.
- *
- * @return The byte as an integer.
- * @see net.sf.odinms.tools.data.input.ByteInputStream#readByte()
- */
- @Override
- public int readByte() {
- bytesRead++;
- return ((int) arr[pos++]) & 0xFF;
- }
- /**
- * Returns the current stream as a hexadecimal string of values.
- * Shows the entire stream, and the remaining data at the current position.
- *
- * @return The current stream as a string.
- * @see java.lang.Object#toString()
- */
- @Override
- public String toString() {
- String nows = "";
- if (arr.length - pos > 0) {
- byte[] now = new byte[arr.length - pos];
- System.arraycopy(arr, pos, now, 0, arr.length - pos);
- nows = HexTool.toString(now);
- }
- return "All: " + HexTool.toString(arr) + "nNow: " + nows;
- }
- /**
- * Returns the number of bytes available from the stream.
- *
- * @return Number of bytes available as a long integer.
- * @see net.sf.odinms.tools.data.input.ByteInputStream#available()
- */
- @Override
- public long available() {
- return arr.length - pos;
- }
- }