View Single Post
  #4 (permalink)  
Old 07-28-2008, 11:34 PM
Norm's Avatar
Norm Norm is offline
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
I reworked tim's code to use some of the built in methods. Probably not as efficient but is some shorter. Also for testing I prefer to have all the data in the program vs external files, so I used ByteArrays in place of files.

Code:
/* * Main.java * * Created on January 15, 2008, 9:35 AM * Modified July 28, 2008 by Norm */ package stringbits; import java.io.*; import java.util.*; /** * * @author tim */ public class Main { //-------------------------------------------------------------------------------- public static String[] readBinaryStrings(InputStream is, int size) throws Throwable{ boolean EOF = false; Vector<String> list = new Vector<String>(); // Save chunks of size bytes String current = ""; do{ int next = is.read(); if (next != -1){ // test EOF String add = "00000000"+Integer.toBinaryString(next); add = add.substring(add.length()-8); // get rightmost 8 chars current += add; if (current.length() >= size * 8){ list.add(new String(current)); current = ""; } } else { if (!current.equals("")) list.add(new String(current)); // add partial string EOF = true; } } while (!EOF); is.close(); // Convert the Vector to an String array return list.toArray(new String[list.size()]); // return result; } //---------------------------------------------------------------------------------- // Convert array of String representation of binary to a single String public static void writeBinaryStrings(OutputStream os, String[] bitStrings) throws Throwable{ final int ChunkSize = 8; for (String string : bitStrings){ int nbrChunks = string.length() / ChunkSize; // Get chars in 8 char chunks for (int i = 0; i < nbrChunks; i++){ int startIndex = i * ChunkSize; int endIndex = startIndex + ChunkSize; String next = string.substring(startIndex, endIndex); // next 8 bytes os.write(Byte.parseByte(next, 2)); // to a byte } // end for(i) }// end for(string) os.close(); } //----------------------------------------------------------------------------------- // Testing code here public static void main(String[] args) { // Define some test data byte[] byteData = new byte[] {0x35, 0x73, (byte)0x9A}; //0011 0101 0111 0011 1001 1010 String inputData = "This is a string that should be long enough to show that the code works"; byteData = inputData.getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(byteData); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try{ String[] bit64Strings = readBinaryStrings(bais, 8); for (String string : bit64Strings){ System.out.println(string); // show results } // Now convert back writeBinaryStrings(baos, bit64Strings); System.out.println(baos); // should be same as original // Test it equal System.out.println("output " + (baos.toString().equals(inputData) ? "matches" : "does NOT match" ) + " input"); } catch (Throwable e) { showError(e); } } public static void showError(Throwable e){ System.out.println("Problem: " + e.getMessage()); } }
Reply With Quote