Hi everyone.
This code protects a file.
I change some bytes of the 3gp video.
Greetings.
Code:
public static void abrirFile(boolean corruptFile)
{
Connection ci =null;
Connection co =null;
FileConnection fileIn=null;
FileConnection fileOut=null;
InputStream is=null;
OutputStream os=null;
try
{
ci = Connector.open("file:///E:/video/original.3gp");
if(corruptFile)
{
co = Connector.open("file:///E:/video/protected.3gp",Connector.READ_WRITE);
}
else
{
co = Connector.open("file:///E:/video/unprotected.3gp",Connector.READ_WRITE);
}
if(ci!=null)
{
fileIn = (FileConnection)ci;
fileIn.setWritable(false);
is = fileIn.openInputStream();
fileOut = (FileConnection)co;
if(!fileOut.exists())
{
fileOut.create();
}
else
{
fileOut.truncate(0);
}
os= fileOut.openOutputStream();
byte []c=new byte[2000];
int count=0;
boolean first = true;
// first read/write cycle. for modify only the first bytes !
while ((is.read(c, count, c.length)) != -1)
{
if(first)
{
if(corruptFile==true) // if i want to make the file unusable.
{
c[0]=0x0;
c[1]=0x0;
c[2]=0x0;
c[3]=0x0;
c[4]=0x0;
c[5]=0x0;
c[6]=0x0;
c[7]=0x0;
c[8]=0x0;
c[9]=0x0;
}
else
{
// if i want to fix the file, or do nothing.
// (these are the correct bytes)
c[0]=0x0;
c[1]=0x0;
c[2]=0x0;
c[3]=0x14;
c[4]=0x66;
c[5]=0x74;
c[6]=0x79;
c[7]=0x70;
c[8]=0x33;
c[9]=0x67;
}
os.write(c);
first = false;
}
else
{
// normal write/copy
os.write(c);
}
}
os.flush();
}
}
catch (Exception e)
{
debug(""+e);
e.printStackTrace();
}
// close is,os, etc...
try
{
is.close();
os.close();
fileIn.close();
fileOut.close();
ci.close();
co.close();
}
catch (Exception e)
{}
}