Ejemplo n.º 1
0
 /**
  * Closes this stream and the underlying OutputStream. If called when this bit stream is not at a
  * byte boundary, then the minimum number of zeros (between 0 and 7) are written as padding to
  * reach a byte boundary.
  *
  * @throws IOException if an I/O error occurs
  * @see java.io.OutputStream#close()
  */
 public void close() throws IOException {
   if (!closed) {
     writePadding();
     output.close();
     super.close();
   }
 }
  public int compress(InputStream in, OutputStream out, boolean force) throws IOException {
    if ((mySavedBits <= 0) && (force == false)) {
      myViewer.showError(
          "compression uses " + (-mySavedBits) + " more bits\nuse force compression to compress");
      return 0;
    } else {
      BitOutputStream bitsOut = new BitOutputStream(out);
      myCompBits = 0;
      // write store tree magic number
      bitsOut.writeBits(BITS_PER_INT, STORE_COUNTS);
      myCompBits += BITS_PER_INT;

      // write header, non-saving-space code using SCF
      for (int k = 0; k < ALPH_SIZE; k++) {
        bitsOut.writeBits(BITS_PER_INT, myLetterCount[k]);
        myCompBits += BITS_PER_INT;
      }

      // write compressed file
      BitInputStream bitsin = new BitInputStream(in);
      int curWord = bitsin.readBits(BITS_PER_WORD);
      while (curWord != -1) {
        writeCompFile(curWord, bitsOut);
        curWord = bitsin.readBits(BITS_PER_WORD);
      }

      // Write pseudo EOF
      writeCompFile(PSEUDO_EOF, bitsOut);
      bitsOut.close();
      if (myCompBits % 8 != 0) myCompBits = (myCompBits / 8 + 1) * 8;
      return myCompBits;
    }
  }
Ejemplo n.º 3
0
  /**
   * Transforms the alerts into the byte array that is send to the client
   *
   * @return byte representation of the alerts
   * @throws NotificationException
   */
  public byte[] getBytes() throws NotificationException {
    if (this.alerts == null) {
      throw new NotificationException("No alerts to build the message body");
    }

    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    BitOutputStream bout = new BitOutputStream(byteOut);
    try {
      // get the number of syncs in the body
      int syncNr = 0;
      for (int i = 0; i < this.alerts.length; i++) {
        ArrayList items = this.alerts[i].getItems();
        for (int j = 0; j < items.size(); j++) {
          syncNr++;
        }
      }
      if (log.isTraceEnabled()) {
        log.trace("Number of sync items:" + syncNr);
      }
      int[] intRepresentation = getIntBinaryRepresentation(syncNr, NUM_BIT_NUMBER_SYNCS);
      for (int i = 0; i < NUM_BIT_NUMBER_SYNCS; i++) {
        bout.writeBit(intRepresentation[i]);
      }

      // put 0000 in the body - future use
      for (int i = 0; i < NUM_BIT_FUTURE_USE; i++) {
        bout.writeBit(0);
      }

      // for each sync info from each alert
      for (int i = 0; i < this.alerts.length; i++) {
        Alert alert = this.alerts[i];
        ArrayList items = this.alerts[i].getItems();
        for (int j = 0; j < items.size(); j++) {
          Item item = (Item) items.get(j);
          // put in the body syncType -200
          if ((alert.getData() < 206) || (alert.getData() > 210)) {
            throw new NotificationException("Invalid alert data type " + alert.getData());
          }
          intRepresentation = null;
          intRepresentation = getIntBinaryRepresentation(alert.getData() - 200, NUM_BIT_ALERT_DATA);
          for (int k = 0; k < NUM_BIT_ALERT_DATA; k++) {
            bout.writeBit(intRepresentation[k]);
          }
          // put 0000 -future use
          for (int k = 0; k < NUM_BIT_FUTURE_USE; k++) {
            bout.writeBit(0);
          }
          // content type - map between string and value !!!!!
          int contentTypeCode = 0;
          if ((item.getMeta() != null) && (item.getMeta().getType() != null)) {
            String type = item.getMeta().getType();
            Integer tmp = contentTypeCodes.get(type);
            if (tmp != null) {
              contentTypeCode = tmp;
            }
          }
          intRepresentation = null;
          intRepresentation = getIntBinaryRepresentation(contentTypeCode, NUM_BIT_CONTENT_TYPE);
          for (int k = 0; k < NUM_BIT_CONTENT_TYPE; k++) {
            bout.writeBit(intRepresentation[k]);
          }

          //
          // SyncSource: length + uri
          //
          String syncSourceURI = null;

          if (item.getSource() != null) {
            //
            // Trying to use item' source
            //
            syncSourceURI = item.getSource().getLocURI();
          } else if (item.getTarget() != null) {
            //
            // Trying to use item' target
            //
            syncSourceURI = item.getTarget().getLocURI();
          } else {
            throw new NotificationException(
                "The Alert command n. " + i + " doesn't contain an item with source or target uri");
          }

          if (log.isTraceEnabled()) {
            log.trace("SourceURI: " + syncSourceURI);
          }

          // syncsource uri length
          intRepresentation = null;
          intRepresentation =
              getIntBinaryRepresentation(syncSourceURI.length(), NUM_BIT_URI_LENGTH);
          for (int k = 0; k < NUM_BIT_URI_LENGTH; k++) {
            bout.writeBit(intRepresentation[k]);
          }

          // syncsource uri
          byte[] uri = syncSourceURI.getBytes();
          for (int k = 0; k < uri.length; k++) {
            // transform each byte of the server uri string into an 8 bit number
            intRepresentation = getIntBinaryRepresentation(uri[k], 8);
            // write the bit representation of each byte of the uri in the bit output stream
            for (int jj = 0; jj < 8; jj++) {
              bout.writeBit(intRepresentation[jj]);
            }
          }
        }
      }
      bout.close();
    } catch (IOException e) {
      throw new NotificationException("Error during body computing", e);
    }

    return byteOut.toByteArray();
  }