1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
InputStream in_stream;
//lecturre des données, et stockage dans un ByteArrayOutputStream
public int ReadData(ByteArrayOutputStream bytearray,int sizeData)
{
//declarations des variables locales
//nombre d'octets restant à lire
int remaingbytes = sizeData;
//nombre d'octets lus à la derniere lecture
int read = 0;
//si il y a une erreur
boolean arret = false;
//tableau intermediaire
byte[] data = new byte[sizeData];
byte[] tabData = new byte[sizeData];
//nombres courant d'octets lus
int totalread = 0;
try{
//tant qu'il reste des octets à lire
while(remaingbytes > 0)
{
//on essaye de lire le plus grand nombre à la fois
read = in_stream.read(data, 0, remaingbytes);
//il y a une erreur, ou on arrive en fin de lecture prematurement
if (read == -1)
{
arret = true;
break;
}
//on insere le bloc qui vient d'etre lu dans le buffer de sortie
else{
bytearray.write(data,totalread , read);
for(int ind = 0;ind<read;ind++ )
tabData[ind + totalread] = data[ind];
}
//on calcul le nombre d'octets restant à lire, selon les octects qui viennent
//d'etre recupérés
remaingbytes = remaingbytes - read;
//on ajoute à la taille totale
totalread += read;
}
data = null;
//verification des données
if(VerifyData(bytearray,sizeData))
addDebug("Verify2 OK !!!!", null,5);
else
addDebug("Verify2 KO ####", null,5);
if (arret || read == -1 || totalread != sizeData)
{
//addDebug(" Error "+ remaingbytes +"/" +totalread, null,2);
}
}
catch(IOException e)
{
System.out.println(" IOException "+ remaingbytes +"/" +totalread);
}catch(NullPointerException e)
{
System.out.println(" NPException"+ remaingbytes +"/" +totalread);
}
//on renvoie la taille du bloc lu
return sizeData;
}
//verification des données
public boolean VerifyData(ByteArrayOutputStream bytearray,int sizeData)
{
byte[] tabData =bytearray.toByteArray();
byte compt = 0;
for(int ind = 0 ; ind < sizeData ; ind ++)
{
if(compt >= 255)
compt = 0;
if(tabData[ind] != compt)
{
addDebug("-Byte ind "+ind+":"+(new Byte(compt)).toString()
+ "/"+(new Byte(tabData[ind])).toString(), null, 4);
return false;
}
compt ++;
}
return true;
} |
Partager