private static void checkAttributesSanity(
      final int attributeRecordId,
      final IntArrayList usedAttributeRecordIds,
      final IntArrayList validAttributeIds)
      throws IOException {
    assert !usedAttributeRecordIds.contains(attributeRecordId);
    usedAttributeRecordIds.add(attributeRecordId);

    final DataInputStream dataInputStream = getAttributesStorage().readStream(attributeRecordId);
    try {
      while (dataInputStream.available() > 0) {
        int attId = DataInputOutputUtil.readINT(dataInputStream);
        int attDataRecordId = DataInputOutputUtil.readINT(dataInputStream);
        assert !usedAttributeRecordIds.contains(attDataRecordId);
        usedAttributeRecordIds.add(attDataRecordId);
        if (!validAttributeIds.contains(attId)) {
          assert getNames().valueOf(attId).length() > 0;
          validAttributeIds.add(attId);
        }
        getAttributesStorage().checkSanity(attDataRecordId);
      }
    } finally {
      dataInputStream.close();
    }
  }
  private static void deleteContentAndAttributes(int id) throws IOException {
    int content_page = getContentRecordId(id);
    if (content_page != 0) {
      getContentStorage().releaseRecord(content_page);
    }

    int att_page = getAttributeRecordId(id);
    if (att_page != 0) {
      final DataInputStream attStream = getAttributesStorage().readStream(att_page);
      while (attStream.available() > 0) {
        DataInputOutputUtil.readINT(attStream); // Attribute ID;
        int attAddress = DataInputOutputUtil.readINT(attStream);
        getAttributesStorage().deleteRecord(attAddress);
      }
      attStream.close();
      getAttributesStorage().deleteRecord(att_page);
    }
  }
  private static int findAttributePage(int fileId, String attrId, boolean toWrite)
      throws IOException {
    checkFileIsValid(fileId);

    Storage storage = getAttributesStorage();

    int encodedAttrId = DbConnection.getAttributeId(attrId);
    int recordId = getAttributeRecordId(fileId);

    if (recordId == 0) {
      if (!toWrite) return 0;

      recordId = storage.createNewRecord();
      setAttributeRecordId(fileId, recordId);
    } else {
      DataInputStream attrRefs = storage.readStream(recordId);
      try {
        while (attrRefs.available() > 0) {
          final int attIdOnPage = DataInputOutputUtil.readINT(attrRefs);
          final int attrAddress = DataInputOutputUtil.readINT(attrRefs);

          if (attIdOnPage == encodedAttrId) return attrAddress;
        }
      } finally {
        attrRefs.close();
      }
    }

    if (toWrite) {
      Storage.AppenderStream appender = storage.appendStream(recordId);
      DataInputOutputUtil.writeINT(appender, encodedAttrId);
      int attrAddress = storage.createNewRecord();
      DataInputOutputUtil.writeINT(appender, attrAddress);
      DbConnection.REASONABLY_SMALL.myAttrPageRequested = true;
      try {
        appender.close();
      } finally {
        DbConnection.REASONABLY_SMALL.myAttrPageRequested = false;
      }
      return attrAddress;
    }

    return 0;
  }
示例#4
0
    @Override
    public void run() {

      try {
        // Create data input and output streams
        DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
        DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());

        // Set up a byte buffer to capture the file from the client
        byte[] buffer = new byte[BUFFER_SIZE];
        OutputStream outputStream = null;
        String fileName = "";
        boolean createFile = true;
        int bytesReceived = 0;
        long totalBytesReceived = 0;
        long fileSize = 0;

        // Continuously serve the client
        while (true) {
          bytesReceived = inputFromClient.read(buffer);

          if (bytesReceived
              > 0) { // Get the file transmission header from the initial client packet
            String transmitHeader = new String(buffer, 0, bytesReceived);
            // transmitHeader = transmitHeader.substring(0, bytesReceived).trim().;
            String[] header = transmitHeader.split(HEADER_DEL);
            fileSize = Long.parseLong(header[0]);
            fileName = header[1];

            // Send receipt acknowledgment back to the client. Just send back the number of bytes
            // received.
            outputToClient.writeInt(bytesReceived);

            // Reinitialize buffer values
            buffer = new byte[BUFFER_SIZE];
            bytesReceived = 0;
          }

          // Wait for client to send bytes
          while ((bytesReceived = inputFromClient.read(buffer)) != -1) {
            if (inputFromClient.available() > 0) {
              if (createFile) { // Get a unique name for the file to be received
                fileName = textFolder.getText() + fileName; // getUniqueFileName();
                outputStream = createFile(fileName);
                createFile = false;
                textArea.append("Receiving file from client.\n");
              }

              // Write bytes to file
              outputStream.write(buffer, 0, bytesReceived);
            } else { // We get here if no more data is available, but some bytes were already
              // received

              // If bytes were received and the file wasn't already created,
              // it means that the file was smaller than our buffer size.
              // Create the file...
              if (bytesReceived > 0
                  && createFile) { // Get a unique name for the file to be received
                fileName = textFolder.getText() + fileName; // getUniqueFileName();
                outputStream = createFile(fileName);
                createFile = false;
                textArea.append("Receiving file from client.\n");
              }

              if (outputStream != null) {
                if (bytesReceived > 0) { // Write remaining bytes to file, if any
                  outputStream.write(buffer, 0, bytesReceived);
                }

                outputStream.flush();
                outputStream.close();
                textArea.append("Received file successfully. Saved as " + fileName + "\n");

                // Return success to client.
                outputToClient.writeInt(0);
              }

              // Reset creation flag
              createFile = true;
              break;
            }

            // Reinitialize buffer values
            buffer = new byte[BUFFER_SIZE];
            bytesReceived = 0;
          }
        }
      } catch (IOException e) {
        System.err.println(e);
      }
    }