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; } }
public int uncompress(InputStream in, OutputStream out) throws IOException { BitInputStream bitsIn = new BitInputStream(in); int magic = bitsIn.readBits(BITS_PER_INT); if (magic != STORE_COUNTS) { throw new IOException("magic number not right"); } myLetterCount = new int[ALPH_SIZE]; for (int k = 0; k < ALPH_SIZE; k++) { int bits = bitsIn.readBits(BITS_PER_INT); myLetterCount[k] = bits; } myRoot = HuffTree(myLetterCount); myViewer.update("\n\nHUFF TREE LOOKS LIKE: \n\n" + treeToString(myRoot, "")); myMapping = new TreeMap<Integer, String>(); recreateMapping(myRoot, myMapping, ""); myViewer.update("\n\nTHE CODE TABLE LOOKS LIKE: \n\n"); printMapping(myMapping); // Step 5: Uncompress that file BitOutputStream bitsOut = new BitOutputStream(out); TreeNode tnode = myRoot; myUncompBits = 0; while (tnode != null) { int bits = bitsIn.readBits(1); if (bits == -1) { throw new IOException("error reading bits, no PSEUDO-EOF"); } if ((bits & 1) == 0) tnode = tnode.myLeft; else tnode = tnode.myRight; if ((tnode.myLeft == null) && (tnode.myRight == null)) { if (tnode.myValue == PSEUDO_EOF) break; // out of while-loop else { bitsOut.write(tnode.myValue); myUncompBits += BITS_PER_WORD; tnode = myRoot; // start back at top } } } myViewer.update("\n\n uncompressed file size " + myUncompBits); return myUncompBits; }
/** * 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(); } }
protected void writeCompFile(int word, BitOutputStream out) { String code = myMapping.get(word); for (char c : code.toCharArray()) { int bin = Integer.parseInt("" + c); out.writeBits(1, bin); } myCompBits += code.length(); }
/** * Write the SWF representation of this object to <code>out</code>. * * @param out the output stream to write on * @exception IOException if an I/O error occurs. */ public void write(BitOutputStream out) throws IOException { int entryLength = getEntryLength(); // Write the edge record and edge type flags first... out.writeBits(3, 2); // The SWF file holds entryLength-2, not the length itself... out.writeBits(entryLength - 2, 4); if (isHorizontal()) { // Write the flags for "no general line, not vertical" out.writeBits(0, 2); // And write the X value... out.writeBits(getX(), entryLength); } else if (isVertical()) { // Write the flags for "no general line, vertical" out.writeBits(1, 2); // And write the > value... out.writeBits(getY(), entryLength); } else { // Write the flag for "general line" out.writeBits(1, 1); // ... and the actual data out.writeBits(getX(), entryLength); out.writeBits(getY(), entryLength); } }
/** * 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(); }
/** * Flushes this and the underlying output stream and forces any buffered bits to be written out. * * @throws IOException if an I/O error occurs * @see java.io.OutputStream#flush() */ public void flush() throws IOException { writePadding(); output.flush(); super.flush(); }
public int evaluate(Map<?, ?> fieldValues, BitOutputStream out) { return (int) out.bytesWritten(); }