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; } }
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); } }