What does this do?

Good afternoon all!

I found some sample encryption code and I am wondering what the statement below does. Currently the code encrypts or decrypts file and I am going to be using it as a "roadmap" to create a program to encrypt and decrypt byte[].

byte[] buf = cipherMode == Cipher.ENCRYPT_MODE?newbyte[100] :newbyte[128];

Thanks,

Doug

Here is a bigger code piece if needed:

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

String textLine =null;

//RSA encryption data size limitations are slightly less than the key modulus size,

//depending on the actual padding scheme used (e.g. with 1024 bit (128 byte) RSA key,

//the size limit is 117 bytes for PKCS#1 v 1.5 padding. (http://www.jensign.com/JavaScience/dotnet/RSAEncrypt/)

byte[] buf = cipherMode == Cipher.ENCRYPT_MODE?newbyte[100] :newbyte[128];

int bufl;

// init the Cipher object for Encryption...

[1506 byte] By [DougJrSa] at [2007-11-27 1:23:47]
# 1

You mean the [url=http://www.devdaily.com/java/edu/pj/pj010018/]ternary operator[/url]? These two code snippets accomplish the same thing.

boolean a = //some boolean value

int i = a ? 1 : 2;

/* */

int i;

if(a) {

i = 1;

} else {

i = 2;

}

CaptainMorgan08a at 2007-7-12 0:13:54 > top of Java-index,Java Essentials,Java Programming...
# 2

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op2.html

The conditional operator acts a bit like an if-then-else statement, whilst

being an expression.

That code could be rewritten:

byte[] buf;

if (cipherMode == Cipher.ENCRYPT_MODE)

buf = new byte[100] ;

else

buf = new byte[128];

Me? I would have written:

int size = cipherMode == Cipher.ENCRYPT_MODE ? 100 : 128;

byte[] buf = new byte[size];

DrLaszloJamfa at 2007-7-12 0:13:54 > top of Java-index,Java Essentials,Java Programming...
# 3
orbyte[] buf = new byte[cipherMode == Cipher.ENCRYPT_MODE ? 100 : 128];;-)
masijade.a at 2007-7-12 0:13:54 > top of Java-index,Java Essentials,Java Programming...