RandomAccessByteStream.java
Upload User: gwt600
Upload Date: 2021-06-03
Package Size: 704k
Code Size: 3k
Category:

Games

Development Platform:

Java

  1. /*
  2. This file is part of the OdinMS Maple Story Server
  3.     Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc> 
  4.                        Matthias Butz <matze@odinms.de>
  5.                        Jan Christian Meyer <vimes@odinms.de>
  6.     This program is free software: you can redistribute it and/or modify
  7.     it under the terms of the GNU Affero General Public License version 3
  8.     as published by the Free Software Foundation. You may not use, modify
  9.     or distribute this program under any other version of the
  10.     GNU Affero General Public License.
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU Affero General Public License for more details.
  15.     You should have received a copy of the GNU Affero General Public License
  16.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  */
  18. package net.sf.odinms.tools.data.input;
  19. import java.io.IOException;
  20. import java.io.RandomAccessFile;
  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. /**
  24.  * Provides an abstract layer to a byte stream. This layer can be accessed
  25.  * randomly.
  26.  * 
  27.  * @author Frz
  28.  * @version 1.0
  29.  * @since Revision 323
  30.  */
  31. public class RandomAccessByteStream implements SeekableInputStreamBytestream {
  32. private RandomAccessFile raf;
  33. private long read = 0;
  34. private static Logger log = LoggerFactory
  35. .getLogger(RandomAccessByteStream.class);
  36. /**
  37.  * Class constructor. Wraps this object around a RandomAccessFile.
  38.  * 
  39.  * @param raf
  40.  *            The RandomAccessFile instance to wrap this around.
  41.  * @see java.io.RandomAccessFile
  42.  */
  43. public RandomAccessByteStream(RandomAccessFile raf) {
  44. super();
  45. this.raf = raf;
  46. }
  47. /**
  48.  * Reads a byte off of the file.
  49.  * 
  50.  * @return The byte read as an integer.
  51.  */
  52. @Override
  53. public int readByte() {
  54. int temp;
  55. try {
  56. temp = raf.read();
  57. if (temp == -1)
  58. throw new RuntimeException("EOF");
  59. read++;
  60. return temp;
  61. } catch (IOException e) {
  62. throw new RuntimeException(e);
  63. }
  64. }
  65. /**
  66.  * @see net.sf.odinms.tools.data.input.SeekableInputStreamBytestream#seek(long)
  67.  */
  68. @Override
  69. public void seek(long offset) throws IOException {
  70. raf.seek(offset);
  71. }
  72. /**
  73.  * @see net.sf.odinms.tools.data.input.SeekableInputStreamBytestream#getPosition()
  74.  */
  75. @Override
  76. public long getPosition() throws IOException {
  77. return raf.getFilePointer();
  78. }
  79. /**
  80.  * Get the number of bytes read.
  81.  * 
  82.  * @return The number of bytes read as a long integer.
  83.  */
  84. @Override
  85. public long getBytesRead() {
  86. return read;
  87. }
  88. /**
  89.  * Get the number of bytes available for reading.
  90.  * 
  91.  * @return The number of bytes available for reading as a long integer.
  92.  */
  93. @Override
  94. public long available() {
  95. try {
  96. return raf.length() - raf.getFilePointer();
  97. } catch (IOException e) {
  98. log.error("ERROR", e);
  99. return 0;
  100. }
  101. }
  102. }