(To the Moderators ) please note that i had posted a related problem before but this is a more complete post so please dont close it as duplicate . you may close the previous post.
whenever i get -1 in the console output no data is written in outputstream, whenever i get 3 in console output valid data is writtien in outputstream. the occurance of -1 and 3 is random on different occasions.
this is the code
public void decrypt(InputStream in, OutputStream out) {
try {
// Bytes read from in will be decrypted
in = new CipherInputStream(in, dcipher);
// Read in the decrypte开发者_JS百科d bytes and write the cleartext to out
int numRead = 0;
System.out.println(in.read(buf));
while ((numRead = in.read(buf)) >= 0) {
out.write(buf, 0, numRead);
}
//out.close();
}
catch (java.io.IOException e) {
}
}
here is the console output
the Cell Content : ¼OKs>N?h¸GX&ŸH
-1
the Cell Content : 3Ëù%¥þ-]'±ŠM݆
3
the Cell Content : ´`?û…óï>»†µýÆ$
-1
the Cell Content : 9ûÊ‘øxIÐ8`ýÊeú
3
the Cell Content : •x€ÌWã’ç4æ~?Gû{
-1
the Cell Content : ÉJ‹SD
-1
the Cell Content : ¯'´öƒ²wCÐ)/
3
the Cell Content : ¼?\š
-1
the Cell Content : 4¤¢ÃUÚړ‹ïEk?É•
-1
the Cell Content : vì=¨;°e¼~{GÀ“È"?
3
the Cell Content : 0ò*"MoañôefU?ô?
-1
the Cell Content : –çä,M9"kIF FJÅ
3
the Cell Content : ¬¼aâÅ•bé‰-SoP~Æ
3
the Cell Content : œ¦LKØ•0I¾>n=á
3
the Cell Content : Å'?X °¡¯“nJ/0è˜
3
the Cell Content : 3™æ&‡õvâr`õ_4¾õ
3
the Cell Content : l羚jT/`‚«h™&
3
the Cell Content : ~à‘_X;eÜ$8º!šŒì
3
the Cell Content : ùݳ9ˆ>‰Liœr‡
3
the Cell Content : UzÛ,»è–Üí‡AB®µ
3
the Cell Content : ’ùZnë¥æFó¦–ñ?~
-1
the Cell Content : 4ê¶È˜¬ ”Ôÿ4vä
3
this is the call to the decrypt function.
InputStream excelResource=new FileInputStream(path);
Workbook rwb=Workbook.getWorkbook(excelResource);
int sheetCount=rwb.getNumberOfSheets();
Sheet rs = rwb.getSheet(0);
int rows = rs.getRows();
int cols = rs.getColumns();
for(int i=0; i<rows; i++) {
for(int j=0; j<Col.length; j++) {
String theCell_00 = rs.getCell(j,i).getContents();
System.out.println("the Cell Content : " + theCell_00);
in = new ByteArrayInputStream(theCell_00.getBytes());
out = new FileOutputStream("c:\\Decrypted.txt");
encrypter.decrypt(in, out);
the excel file from which the inputstream is created has data on every cell which is shown in the cell content in console output .. please help me to find out why even with valid inputstream ( as it looks like ) i get -1 console output .
Remove this nonsensicial line:
System.out.println(in.read(buf));
It does read the data into the buffer, but you're ignoring it further! The next call which you do inside the while
statement does not read the same data back, but only the next bytes which are beyond the buffer length. Better do a sysout of numRead
inside the loop if you're really interested.
The other problem is that you're replacing the in
argument with another InputStream
:
in = new CipherInputStream(in, dcipher);
Assign it to its own local variable and keep method arguments unchanged. Best would be to declare them final
in the future so that the compiler will give error on that. E.g.
public void decrypt(final InputStream in, final OutputStream out)
Alternatively you can also configure the IDE to show warnings when you override method arguments (which is usually a bad practice). In for example Eclipse you can do this through Java > Compiler > Errors/Warnings > Name shadowing and conflicts > set all to Warning or maybe Error.
Just a thought, try not using in
for the ciphered/clear input stream. As in,
public void decrypt(InputStream ciph_in, OutputStream out) {
InputStream in;
try {
// Bytes read from in will be decrypted
in = new CipherInputStream(ciph_in, dcipher);
and see what happens.
Besides the other problems detected here and in the other question I think your real problem might be related to encoding. What's the encoding of the Excel file?
Once you know what the encoding is, you can do the following:
theCell_00.getBytes("MYENCODING");
Also, can you show us how are you creating the cipher and if possible the key?
精彩评论