i'm writing a text based game that creates a world based on the data inside of the specified file.

however, the code i have won't let me use the function

DataInputStream.ReadFully(byte[]b)

Code:

package textgame;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Stack;

public class world {

   Stack<String> LevelData = new Stack<String>();
   int location;
   int EndLocation;

   public world(Stack<String> s, int l, int e)
   {
      s = this.LevelData;
      l = this.location;
      e = this.EndLocation;
   }

}

class world_render
{

   public world RenderWorld(String file)
   {
      Stack<String> data = new Stack<String>();
      int location = 0;
      FileInputStream LevelIn = null;
      try {
         LevelIn = new FileInputStream(file);
      } catch (IOException e) {
         System.out.println("IOException in file " + file + "!");
         e.printStackTrace();
      }

      DataInputStream DataIn = new DataInputStream(LevelIn);

      byte[] DATA = DataIn.readFully(DATA);

      for(byte x : DATA)
      {
         data.add(new String(Integer.toHexString((int)x).toUpperCase()));
      }

      int endOfLevel = data.size();

      return new world(data, location, endOfLevel);
 }

}

Not sure if it is a problem with eclipse, or with my code. ideas?

Recommended Answers

All 9 Replies

Have you read the API doc for the readFully() method. It shows how to use the method.

after veiwing the doc for DataInputStream, it says this for readFully:

Reads some bytes from an input stream and stores them into the buffer array b. The number of bytes read is equal to the length of b

therefore the code that is casuing the error:

byte[] DATA = DataIn.readFully(DATA);

should not be causing any problems, unless, i cannot use the base reference as the accessor. i.e.

byte[] d1 = {};
byte[] DATA = DataIn.readFully(d1);

instead of the above; but both ways still give me the error.

the one thing i am wondering (the doc said nothing about this) is that is it possible that
DataInputStream is setting the byte to null because it cannot find 'file'?

file is supposed to be specified later in the program by another function.

What did the API doc say was returned by the readFully() method?

it returns each byte read from the file into the specified byte array until it reaches an EOF or throws an exception.

The definition for a method shows what it returns and what its parameters are:
<returns this> methodName(<parameters) { ...}

What does readFully() return? What would the return statement in the method look like?

i opened the class DataInputStream and this is the what it had for function
readFully(byte [] b)

which is a variation of the function

DataInputStream.readFully(byte [] b, int off, int len)

Readfully(variant):

/**
 * See the general contract of the <code>readFully</code>
 * method of <code>DataInput</code>.
 * <p>
 * Bytes
 * for this operation are read from the contained
 * input stream.  
 *
 * @param      b   the buffer into which the data is read.
 * @exception  EOFException  if this input stream reaches the end before
 *             reading all the bytes.
 * @exception  IOException   the stream has been closed and the contained
 *         input stream does not support reading after close, or
 *         another I/O error occurs.
 * @see        java.io.FilterInputStream#in
 */
public final void readFully(byte b[]) throws IOException {
   readFully(b, 0, b.length);
}

Readfully(original decl):

/**
 * See the general contract of the <code>readFully</code>
 * method of <code>DataInput</code>.
 * <p>
 * Bytes
 * for this operation are read from the contained
 * input stream.
 *
 * @param      b     the buffer into which the data is read.
 * @param      off   the start offset of the data.
 * @param      len   the number of bytes to read.
 * @exception  EOFException  if this input stream reaches the end before
 *               reading all the bytes.
 * @exception  IOException   the stream has been closed and the contained
 *         input stream does not support reading after close, or
 *         another I/O error occurs.
 * @see        java.io.FilterInputStream#in
 */
public final void readFully(byte b[], int off, int len) throws IOException {
   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;
   }
}

hopefully this is what you'r looking for.

Now do you what the problem is in your code?

Start you own thread for a new question

(edit: refers to deleted post)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.