/**
   * This method reads the contents of a file, and then populates an array with integers inside of
   * it
   *
   * @param inputFile The file being read
   * @param outputArray The array that the numbers will be output to
   */
  public static void readArray(String inputFile, int[] outputArray) {
    boolean flag = false;
    Scanner keyboard = new Scanner(System.in);
    String checkFile = inputFile;
    while (!flag) {

      try {
        DataInputStream inFile = new DataInputStream(new FileInputStream(checkFile));
        for (int i = 0; i < outputArray.length; i++) {
          outputArray[i] = inFile.readInt();
        }
        inFile.close();
        flag = true;
      } catch (EOFException e) {
        System.out.println(e.getMessage());
      } catch (FileNotFoundException e) {
        System.out.println("File not found.");
        System.out.println("Please input the name of the file you'd like to read from: ");
        Scanner checkKeyboard = new Scanner(System.in);
        checkFile = (checkKeyboard.next() + ".dat");
      } catch (IOException e) {
        System.out.println(e.getMessage());
      }
    }
  }
Example #2
0
 private void handleEOF(EOFException exp, Snapshot snapshot) {
   if (debugLevel > 0) {
     exp.printStackTrace();
   }
   warn("Unexpected EOF. Will miss information...");
   // we have EOF, we have to tolerate missing references
   snapshot.setUnresolvedObjectsOK(true);
 }
Example #3
0
 public Tuple getNext() throws NoSuchElementException, TransactionAbortedException {
   try {
     Tuple tuple = new Tuple(td);
     for (int i = 0; i < td.numFields(); i++) {
       IntField intf = IntField.createIntField(in.readInt());
       tuple.setField(i, intf);
     }
     return tuple;
   } catch (EOFException eof) {
     throw new NoSuchElementException(eof.getMessage());
   } catch (Exception e) {
     e.printStackTrace();
     BufferPool.Instance().abortTransaction(tid);
     closeConnection();
     throw new TransactionAbortedException(e);
   }
 }
 protected synchronized Message receiveMessage() throws IOException {
   if (messageBuffer.size() > 0) {
     Message m = (Message) messageBuffer.get(0);
     messageBuffer.remove(0);
     return m;
   }
   try {
     InetSocketAddress remoteAddress = (InetSocketAddress) channel.receive(receiveBuffer);
     if (remoteAddress != null) {
       int len = receiveBuffer.position();
       receiveBuffer.rewind();
       receiveBuffer.get(buf, 0, len);
       try {
         IP address = IP.fromInetAddress(remoteAddress.getAddress());
         int port = remoteAddress.getPort();
         extractor.appendData(buf, 0, len, new SocketDescriptor(address, port));
         receiveBuffer.clear();
         extractor.updateAvailableMessages();
         return extractor.nextMessage();
       } catch (EOFException exc) {
         exc.printStackTrace();
         System.err.println(buf.length + ", " + len);
       } catch (InvocationTargetException exc) {
         exc.printStackTrace();
       } catch (IllegalAccessException exc) {
         exc.printStackTrace();
       } catch (InstantiationException exc) {
         exc.printStackTrace();
       } catch (IllegalArgumentException e) {
         e.printStackTrace();
       } catch (InvalidCompressionMethodException e) {
         e.printStackTrace();
       }
     }
   } catch (ClosedChannelException exc) {
     if (isKeepAlive()) {
       throw exc;
     }
   }
   return null;
 }
Example #5
0
  // see DbFile.java for javadocs
  public Page readPage(PageId pid) {
    // some code goes here
    if (pid.pageNumber() >= numPages()) {
      throw new IllegalArgumentException("page not in file");
    }
    Page returnme = null;

    byte[] data = HeapPage.createEmptyPageData();
    long offset = (long) BufferPool.PAGE_SIZE * pid.pageNumber();
    try {
      raf.seek(offset);
      for (int i = 0; i < data.length; i++) {
        data[i] = raf.readByte();
      }
      returnme = new HeapPage((HeapPageId) pid, data);
    } catch (EOFException eofe) {
      eofe.printStackTrace();
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
    return returnme;
  }
  // check if compressed file seems ok
  public static long testValid(String ufilename) throws IOException {
    boolean lookForHeader = false;

    // gotta make it
    RandomAccessFile raf = new RandomAccessFile(ufilename, "r");
    raf.order(RandomAccessFile.BIG_ENDIAN);
    raf.seek(0);
    byte[] b = new byte[8];
    raf.read(b);
    String test = new String(b);
    if (test.equals(Level2VolumeScan.ARCHIVE2) || test.equals(Level2VolumeScan.AR2V0001)) {
      System.out.println("--Good header= " + test);
      raf.seek(24);
    } else {
      System.out.println("--No header ");
      lookForHeader = true;
      raf.seek(0);
    }

    boolean eof = false;
    int numCompBytes;
    try {

      while (!eof) {

        if (lookForHeader) {
          raf.read(b);
          test = new String(b);
          if (test.equals(Level2VolumeScan.ARCHIVE2) || test.equals(Level2VolumeScan.AR2V0001)) {
            System.out.println("  found header= " + test);
            raf.skipBytes(16);
            lookForHeader = false;
          } else {
            raf.skipBytes(-8);
          }
        }

        try {
          numCompBytes = raf.readInt();
          if (numCompBytes == -1) {
            System.out.println("\n--done: numCompBytes=-1 ");
            break;
          }
        } catch (EOFException ee) {
          System.out.println("\n--got EOFException ");
          break; // assume this is ok
        }

        System.out.print(" " + numCompBytes + ",");
        if (numCompBytes < 0) {
          System.out.println("\n--last block " + numCompBytes);
          numCompBytes = -numCompBytes;
          if (!lookForHeader) eof = true;
        }

        raf.skipBytes(numCompBytes);
      }
    } catch (EOFException e) {
      e.printStackTrace();
    }

    return raf.getFilePointer();
  }