Share Pastebin
Guest
Public paste!

sh00p

By: a guest | Mar 20th, 2010 | Syntax: Java | Size: 1.19 KB | Hits: 96 | Expires: Never
Copy text to clipboard
  1. import java.io.*;
  2. import java.util.ArrayList;
  3.  
  4. public class BitFileWriter {
  5.         private FileOutputStream fileOut = null;
  6.         private String filename = null;
  7.        
  8.         ArrayList<Boolean> buffer = null;
  9.        
  10.         BitFileWriter(String filename) {
  11.                 this.filename = filename;
  12.                 this.buffer = new ArrayList<Boolean>();
  13.         }
  14.        
  15.         public void writeBit(String sBit) {
  16.                 Boolean bBit = null;
  17.                 if(sBit.equals("1")) bBit = new Boolean(true); 
  18.                 if(sBit.equals("0")) bBit = new Boolean(false);
  19.                
  20.                 if(buffer.size() < 8) {
  21.                         buffer.add(bBit);
  22.                         if(buffer.size() == 8) {
  23.                                 writeByte();
  24.                         }
  25.                 }
  26.         }
  27.        
  28.         public void writeByte() {
  29.                 int b = 0;
  30.                 for(int i = 0; i < buffer.size(); i++) {
  31.                         if(buffer.get(buffer.size() - i - 1) == true) {
  32.                                 b |= (1<<i);
  33.                         }
  34.                 }
  35.                
  36.                 buffer.clear();
  37.                 try {
  38.                         fileOut = new FileOutputStream(filename, true);
  39.                         fileOut.write(b);
  40.                         fileOut.close();
  41.                 } catch (IOException c) {
  42.                         System.out.printf("IO Exception.\n");
  43.                         System.exit(0);
  44.                 }              
  45.         }
  46.        
  47.         public int finish() {
  48.                 int padding = 0;
  49.                 if(buffer.size() == 0) {
  50.                         padding = 0;
  51.                         return padding;
  52.                 }
  53.                 else {
  54.                         padding = 8 - buffer.size();
  55.                         writeByte();
  56.                         buffer.clear();
  57.                         return padding;
  58.                 }
  59.         }
  60. }