Search code examples
javadata-stream

Having trouble getting readAllBytes() method to work


Other students in my class have gotten this code to work, but I get an error. I am trying to use the readAllBytes() method for an assignment and I can't get it to work. My teacher there is another way he intended it to work using That method is not defined for DataInputStream. Take a look at the read(byte[]) method and the first integer in the datastream shows how large the byte Array is. Either way will work, I just am getting an error on line 39, and if I delete "import java.io.DataInputStream;" it fixes that error but I get an error on line 31 instead. Any help would be much appreciated enter image description here

package edu.ics211.h03;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.io.InputStream;

/**
 * Represents a ReadFile.
 *
 * @author Constantine Peros
 */
public class ReadFile implements IReadFile {

  /**
   * Creates a New ReadFile.
   */
  public ReadFile() {
    // TODO Auto-generated constructor stub
  }

  @Override
  public String readFile(String fileName) throws IOException {
    // Create a FileInputStream from the file name
    // Create a DataInputStream from the FileInputStream
    DataInputStream data = new DataInputStream(new FileInputStream(fileName));
    // Read the number of bytes using readInt
    int size = data.readInt();
    // Read the encoding using readByte
    byte encoding = data.readByte();
    // Create a byte array number of bytes long
    byte[] byteArray = new byte[size];
    // Fill the byte array
    byteArray = data.readAllBytes();
    // Close my inputs
    data.close();
    // Switch the encoding or use if statements
    switch (encoding) {
      case 1:
        return new String(byteArray, StandardCharsets.US_ASCII);
        break;
      case 2:
        return new String(byteArray, StandardCharsets.UTF_16LE);     
        break;
      case 3:
        return new String(byteArray, StandardCharsets.UTF_8);     
        break;
      case 4:
        return new String(byteArray, StandardCharsets.UTF_16);     
        break;
      default:
        return null;
    }
  }
}

Solution

  • Seems to me that all you need to do is replace this line of the code

    byteArray = data.readAllBytes();
    

    with this line

    data.read(byteArray);
    

    Here is the entire method.

    public String readFile(String fileName) throws IOException {
        // Create a FileInputStream from the file name
        // Create a DataInputStream from the FileInputStream
        DataInputStream data = new DataInputStream(new FileInputStream(fileName));
        // Read the number of bytes using readInt
        int size = data.readInt();
        // Read the encoding using readByte
        byte encoding = data.readByte();
        // Create a byte array number of bytes long
        byte[] byteArray = new byte[size];
        // Fill the byte array
        data.read(byteArray);
    //  byteArray = data.readAllBytes();
        // Close my inputs
        data.close();
        // Switch the encoding or use if statements
        switch (encoding) {
            case 1:
                return new String(byteArray, StandardCharsets.US_ASCII);
            case 2:
                return new String(byteArray, StandardCharsets.UTF_16LE);     
            case 3:
                return new String(byteArray, StandardCharsets.UTF_8);     
            case 4:
                return new String(byteArray, StandardCharsets.UTF_16);     
            default:
                return null;
        }
    }