BITŲ PAKAVIMO KODAS
BitInputStream.java failas:
import java.io.*;
public class BitInputStream extends ByteArrayInputStream {
private int bitsRead;
private int bitPosition;
private int currentByte;
private int myMark;
private final static int NUM_BITS_IN_BYTE = 8;
private final static int END_POSITION = -1;
private boolean readingStarted;
/**
* Create a BitInputStream for a File on disk.
*/
public BitInputStream( byte[] buf ) throws IOException {
super( buf );
myMark = 0;
bitsRead = 0;
bitPosition = NUM_BITS_IN_BYTE-1;
currentByte = 0;
readingStarted = false;
}
/**
* Read a binary „„1“ or „0“ from the File.
*/
public int readBit() throws IOException {
int theBit = -1;
if( bitPosition == END_POSITION || !readingStarted ) {
currentByte = super.read();
bitPosition = NUM_BITS_IN_BYTE-1;
readingStarted = true;
}
theBit = (0x01 << bitPosition) & currentByte;
bitPosition–;
if( theBit > 0 ) {
theBit = 1;
}
return( theBit );
}
/**
* Return the next byte in the File as lowest 8 bits of int.
*/
public int read() {
currentByte = super.read();
bitPosition = END_POSITION;
readingStarted = true;
return( currentByte );
}
/**
*
*/
public void mark( int readAheadLimit ) {
super.mark(readAheadLimit);
myMark = bitPosition;
}
/**
* Add needed functionality to super’s reset() method. Reset to
* the last valid pposition marked in the input stream.
*/
public void reset() {
super.pos = super.mark-1;
currentByte = super.read();
bitPosition = myMark;
}
/**
* Returns the number of bits still available to be read.
*/
public int availableBits() throws IOException {
return( ((super.available() * 8) + (bitPosition + 1)) );
}
}
BitOutputStream.java failas:
import java.io.*;
import java.lang.Exception;
public class BitOutputStream extends ByteArrayOutputStream {
private FileOutputStream myFileOS;
private int bitsWritten;
private int bitPosition;
private byte[] myByte;
private final static int NUM_BITS_IN_BYTE = 8;
private final static int END_POSITION = -1;
/**
* Default constructor.
*/
public BitOutputStream() {
this(null);
}
/**
* Constuct based upon a FileOutputSteam to write to
*/
public BitOutputStream( FileOutputStream fos ) {
super();
myFileOS = fos;
bitsWritten = 0;
myByte = nnew byte[1];
resetByte();
}
/**
* Reset the byte and position marker.
*/
private void resetByte() {
bitPosition = NUM_BITS_IN_BYTE-1;
myByte[0] = (byte)0x00;
}
/**
* Write to my super’s buffer.
*/
private void flushByte() {
write( myByte, 0, 1 );
bitsWritten = bitsWritten + 8;
resetByte();
}
/**
* Write a bit to the stream.
* If bit == „0“, writes a ‘0’ bit.
* If bit == „1“, writes a ‘1’ bit.
*/
public void writeBit( int bit )) {
if( bit != 0 && bit != 1 ) {
System.err.println( „: only 1’s and 0’s can be written“ );
}
if( bitPosition == END_POSITION ) {
flushByte();
}
if( bit == 0 ) {
bitPosition–;
}
else if( bit == 1 ) {
myByte[0] = (byte)(myByte[0] | (0x01 << bitPosition));
bitPosition–;
}
}
/**
* Writes to the file if a FileOutputStream is set.
*/
public void flushToFile() {
if( myFileOS == null ) {
System.err.println( „No FileOutputStream set“ );
}
else {
try {
// Flush any excess to my super’s buffer
if( bitPosition >= END_POSITION && bitPosition != NUM_BITS_IN_BYTE-1 ) {
flushByte();
}
// Have my super write to the File
writeTo( (OutputStream)myFileOS );
}
catch( Exception e ) {
System.err.println(„: error flushing to file“);
e.printStackTrace();
}
}
}
/**
* Close the stream.
*/
public void closeStream() throws Exception {
try {
super.close();
}
catch( Exception e ) {
System.err.println(„: error while closing“);
e.printStackTrace();
throw( new Exception( „: error closing stream“ ));
}
}
}
Bitpack.java failas. Sitas failas yra pagrindinis:
import java.io.*;
import java.util.*;
import java.lang.*;
public class Bitpack {
//———————- Stream’ai ———————————————-
public static FileInputStream inFile;
public static FileOutputStream outFile;
public static BitOutputStream outBit;
public static BitInputStream inBit;
//———————– Masyvai ————————————————
public static int[][] CodeTable;
public sstatic char[] charr = new char[128];
public static int[] CoutSmb = new int[256];
//——————— Kintamieji ————————————————
public static int kiek=0;
public static int buf=0;
public static int count=0;
public static int failoIlgis=0;
//8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
//8888888888888888888888888888888888888 FUNKCIJOS 888888888888888888888888888888888888
//8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
//——————————————————————————-
public static void clear(){
for (int i = 0; i < 50; i++)
System.out.println();
}
//——————————————————————————-
public static int minBit(int differentCase) {
if(differentCase == 1)
return 1;
else
return (int)Math.ceil(Math.log(differentCase)/Math.log(2));
}
//——————————————————————————-
public static int Till(int failoilgis, int usefullBit) {
if(usefullBit == 0) {
return failoIlgis*8;
}
else {
return (failoIlgis – 1)*8 + usefullBit;
}
}
//——————————————————————————–
public static int usefullBit(int kiek, int bitu) throws IOException {
return (bitu*kiek) % 8;
}
//——————————————————————————–
public static void CodeTable(int co,int rei){
CodeTable = new int[co][rei];
int divisor = 2;
for (int j = rei – 1; j >= 0; j–)
{
for (int i = 0; i < co; i++)
{
if ((i % divisor) < (divisor / 2))
CodeTable[i][j] = 0;
else
CodeTable[i][j] = 1;
}
divisor *= 2;
}
}
//——————————————————————————–
public static int skirtinguSimb() throws IOException {
int count1=0;
int ind=0;
Arrays.fill(CoutSmb, 0);
int b=inFile.read();
while(b>0)
{
CoutSmb[b]++;
b=inFile.read();
}
inFile.close();
for(int i=0; i<256; i++)
{
if(CoutSmb[i]!=0)
++count1;
}
if(count1>127) throw new IOException(„Nenaudinga spausti sito failo, nes jame rasta „+count1+“ skirtingu simboliu“);
if(count1==0) throw new IOException(„Sito failo nereikia spausti, jis tuscias!!!“);
return count1;
}
//———————————————————————————
public static float entrop(String argument)throws IOException{
inFile = new FileInputStream(argument);
float[] P = new float[count];
Arrays.fill(P, 0);
int t = inFile.read();
while(t>0) {
for(int i=0; i0) {{
for(int i=0; i 0) {
for (int i = 0; i < reikia; i++) {
s = s + inBit.readBit();
}
for (int i = 0; i < count; i++) {
if (s.equals(Compare[i])) {
outFile.write(charr[i]);
}
}
s = „“;
end = end – reikia;
}
clear();
System.out.println(“ INFORMACIJA ISPAKUOJANT……nn“);
System.out.println(„—————————————————————————-„);
System.out.println(„|Faile yra skirtingu simboliu: | “ +count);
System.out.println(„—————————————————————————-„);
System.out.println(„|Failo dydis (baitais): | “ +failoIlgisFirst);
System.out.println(„—————————————————————————–„);
System.out.println(„|Vienam simboliui kuoduoti reikejo (bitai): | “ +minBit(count));
System.out.println(„—————————————————————————–„);
}
//———————————- MaiN’as————————————————-
//———————————- MaiN’as————————————————-
//———————————- MaiN’as————————————————-
public static void main(String[] args) throws IOException {
try{
boolean tikrinti=true;
BufferedReader userIn =new BufferedReader(new InputStreamReader( System.in ) );
while (tikrinti==true)
{
clear();
System.out.println(„KA NORITE DARYTI(zip/unzip):“);
String line = userIn.readLine();
if (line.compareToIgnoreCase(„zip“)==0)
{
clear();
System.out.println(„IVESKITE IVEDIMO FAILA (in) :“);
line = userIn.readLine();
inFile = new FileInputStream(line);
clear();
System.out.println(„IVESKITE ISVEDIMO FAILA (out):“);
String line2 = userIn.readLine();
outFile = new FileOutputStream(line2);
Suspausti(minBit(RBKBABC()),line);
inFile.close();
outFile.close();
System.out.println(“ nn FAILAS: ‘“+line+“‘ SEKMINGAI SUPAKUOTAS.“);
tikrinti=false;
}
else if (line.compareToIgnoreCase(„unzip“)==0)
{
clear();
System.out.println(„IVESKITE IVEDIMO FAILA (in) :“);
line = userIn.readLine();
inFile = new FileInputStream(line);
clear();
System.out.println(„IVESKITE ISVEDIMO FAILA (out):“);
String line2 = userIn.readLine();
outFile = new FileOutputStream(line2);
Isspausti(line,line2);
System.out.println(“ nn FAILAS: ‘“+line+“‘ SEKMINGAI ISPAKUOTAS.“);
inFile.close();
outFile.close();
tikrinti=false;
}
else if(line.compareToIgnoreCase(„exit“)==0)
{
clear();
tikrinti=false;
System.out.println(“ IKI!!!“);
line =
userIn.readLine();
}
else
{
clear();
System.out.println(“ Veskite ka norite daryti, pvz.: ‘zip’ arba ‘unzip’n . tada sekite nurodymus.nnn“);
System.out.println(“ ————————————–„);
System.out.println(“ | JEI NORITE BAIGTI VESKITE ‘exit’ |“);
System.out.println(“ ————————————–„);
line = userIn.readLine();
}
}
} catch(IOException ioe) {System.out.println(„Klaida: „+ioe.getMessage());}
}
}