content: package com.r00t.response;
  
  import java.io.EOFException;
  import java.io.IOException;
  import java.io.InputStream;
  
  public class NCDataFileReader {
      public static class NCDataInfo {
          public boolean encrypted;
          public boolean compressed;
          public int dataLength;
          public boolean isValid;
          public byte[] rawData;
      }
      public static NCDataInfo readNCDataFromStream(InputStream inputStream) throws IOException {
          NCDataInfo info = new NCDataInfo();
  
          // 1. Read data length
          info.dataLength = readInt(inputStream);
  
          // 2. Verify NC stream header
          int magicCode = inputStream.read() | inputStream.read() << 8 | inputStream.read() << 16;
          if (magicCode != 9007474) {
              info.isValid = false;
              return info;
          }
  
          // 3. Read flag byte
          int header = inputStream.read();
          info.encrypted = ((header & 0x1) != 0);
          info.compressed = ((header & 0x2) != 0);
  
          // 4. Read remaining data
          int remainingLength = info.dataLength - 4; // Subtract the 4 bytes already read
          info.rawData = new byte[remainingLength];
          int totalRead = 0;
  
          while (totalRead < remainingLength) {
              int bytesRead = inputStream.read(info.rawData, totalRead, remainingLength - totalRead);
              if (bytesRead == -1) {
                  throw new EOFException("Unexpected end of stream");
              }
              totalRead += bytesRead;
          }
  
          info.isValid = true;
          return info;
      }
  
      private static int readInt(InputStream in) throws IOException {
          int ch1 = in.read();
          int ch2 = in.read();
          int ch3 = in.read();
          int ch4 = in.read();
          if ((ch1 | ch2 | ch3 | ch4) < 0)
              throw new EOFException();
          return (ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0);
      }
  }