Little-endian And Big-endian
Here is a program which converts data types written in Java (big endian) to little endian.
ProtocolBuffer converts integer to byte array and String to bytes. Using FileHandler class contents are written on a binary file.
ProtocolBuffer.java
package com;
public class ProtocolBuffer
{
byte[] content;
int allocated;
int consumed;
public ProtocolBuffer(int size)
{
content = new byte[size];
allocated = size;
consumed = 0;
}
boolean addInteger(int x)
{
content[consumed++] = (byte)(x&255);
content[consumed++] = (byte)((x>>8)&255);
content[consumed++] = (byte)((x>>16)&255);
content[consumed++] = (byte)((x>>24)&255);
return true;
}
boolean addString(String s)
{
byte[] arr = s.getBytes();
int len = arr.length;
for(int i = 0; i < len; i++)
{
content[consumed++] = arr[i];
}
return true;
}
byte[] getBytes()
{
return content;
}
}
FileHandler.java to writes to and reads from binary file.
/**
*
*/
package com;
import java.io.RandomAccessFile;
import java.io.File;
/**
* @author Godel
*
*/
public class FileHandler {
String fname;
int size;
public FileHandler(String name)
{
fname = name;
size = 0;
}
int getLength()
{
File file = new File(fname);
if(file.exists())
size = (int)file.length();
else
size = 0;
return size;
}
void writeBytes(byte[] content,int off,int len) throws Exception
{
RandomAccessFile raf = new RandomAccessFile(fname,"rw");
raf.write(content,off,len);
raf.close();
}
int readBytes(byte[] content,int off,int len) throws Exception
{
RandomAccessFile raf = new RandomAccessFile(fname,"rw");
int val = raf.read(content,off,len);
raf.close();
return val;
}
}
Testing the above program ProtocolBuffer.java
package com;
public class Test {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ProtocolBuffer a = new ProtocolBuffer(1024);
a.addInteger(100);
a.addInteger(5);
a.addString("hello");
System.out.println(a.consumed);
FileHandler fh = new FileHandler("HandShakeProtocol4.bin");
fh.writeBytes(a.getBytes(), 0, a.consumed);
ProtocolBuffer b = new ProtocolBuffer(1024);
b.addInteger(101);
b.addInteger(28);
for(int i = 1001;i < 1008; i++)
{
b.addInteger(i);
}
System.out.println(b.consumed);
FileHandler fh2 = new FileHandler("HandShakeProtocol2.bin");
fh2.writeBytes(b.getBytes(), 0, b.consumed);
}
}
Java Program (stores data type in big endian) reads a binary file stored in little endian.
/**
*
*/
package com;
/**
* @author Godel
*
*/
public class ProtocolBufferDecoder {
byte[] content;
int consumed;
int allocated;
public ProtocolBufferDecoder(byte[] b,int size)
{
content = b;
consumed = 0;
allocated = size;
}
int getInteger()
{
int x = (content[consumed++] & 0xFF) |
((content[consumed++] & 0xFF) << 8) |
((content[consumed++] & 0xFF) << 16) |
((content[consumed++] & 0xFF) << 24);
return x;
}
String getString()
{
int size = allocated - consumed;
byte[] arr = new byte[size];
int j = 0;
for(int i = consumed; i < allocated; i++)
{
arr[j] = content[consumed++];
j++;
}
String s = new String(arr);
return s;
}
}
/**
*
*/
package com;
import java.io.RandomAccessFile;
import java.io.File;
/**
* @author Godel
*
*/
public class FileHandler {
String fname;
int size;
public FileHandler(String name)
{
fname = name;
size = 0;
}
int getLength()
{
File file = new File(fname);
if(file.exists())
size = (int)file.length();
else
size = 0;
return size;
}
void writeBytes(byte[] content,int off,int len) throws Exception
{
RandomAccessFile raf = new RandomAccessFile(fname,"rw");
raf.write(content,off,len);
raf.close();
}
int readBytes(byte[] content,int off,int len) throws Exception
{
RandomAccessFile raf = new RandomAccessFile(fname,"rw");
int val = raf.read(content,off,len);
raf.close();
return val;
}
}
package com;
public class Test {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ProtocolBuffer a = new ProtocolBuffer(1024);
a.addInteger(100);
a.addInteger(5);
a.addString("elloo");
System.out.println(a.consumed);
FileHandler handler = new FileHandler("HandShakeProtocol3.bin");
handler.writeBytes(a.getBytes(), 0, a.consumed);
int len = handler.getLength();
System.out.println(len);
byte[] b = new byte[1024];
int num = handler.readBytes(b, 0, len);
ProtocolBufferDecoder decode = new ProtocolBufferDecoder(b, num);
System.out.println("Integer value:"+decode.getInteger());
System.out.println("Integer value:"+decode.getInteger());
System.out.println("Integer value:"+decode.getString());
}
}
Comments
Post a Comment