using DataOutputStreams

I am writing a process to write the user name together with some enrypted data which is an array of bytes.
This file will be loaded into a oracle 9i oracle database using external tables which allows files to be seen.

Now I could use the DataOutputStream class as this allows the writing of strings and bytes but not to sure if this would work correctly if loaded into an oracle table.  
Alternatively I could use the FileWriter class to write the data, I could convert the bytes to an string object and write that.

In ORacle I then could cast this String using the UTL_RAW.CAST_TO_RAW() oracle function.

Does this seem ok or am I barking up the wrong the tree.

Thanks In advance

Who is Participating?

Improve company productivity with a Business Account.Sign Up

CEHJConnect With a Mentor Commented:
>>The reason I dont think I should use DataOutputStreams is that the process to read the data is not a java component

In that case, that's actually a very good reason to use them, and not the ObjectOutputStream i mentioned before, since a non-Java program won't make sense of serialized data

Do you need Unicode support?
cjjcliffordConnect With a Mentor Commented:

Have you considered using a BLOB for the encrypted data, especially if there is a lot of data? managing BLOBs through JDBC is a little cumbersome I've found, but generally its just a matter of casting the generic JDBC Connection to the specific OracleConnection (and OracleStatement, OracleResultSet, etc), and using the extensions there... that said, there might even be mechanisms there for handling RAW types directly (if the data is short), but I've never looked at this....

RuadRauFlessaConnect With a Mentor Commented:
Your best bet would surely be to use a BLOB since when you enter or read a BLOB you do it via Input and Output Streams. If you really want to make it secure you could even use RSA or some kind of encryption on the stream as you write it to the blob.

And yes it is a verry ugly process to follow but it works like a charm. Be carefull though you won't be able to do searches on the data stored within the object so make sure that you have sufficient data around it to handle that with. Also there is another problem of allowed sizes. I have read that you have a maximum size depending on how the system is set up but I haven't reached that before.

Hope you manage
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

astorerConnect With a Mentor Commented:
You don't say how big the data is.  If this is large (several K) then a BLOB is good.
If not a large amount of data, then why not convert your binary data into a String.  Strings can be a lot easier to mess about with.  
For example, base64 encode the binary data.
Try for some code to base64 encode & decode.

If you're going to be reading again in Java, you'll probably find it convenient to use a class to encapsulate the data and then use ObjectOutputStream and ObjectInputStream

>>This file will be loaded into a oracle 9i oracle database using external tables which allows files to be seen.

What btw would be the point of looking at a file containing an encrypted byte array?
cjjcliffordConnect With a Mentor Commented:
I think what the author was suggesting using the external tables in Oracle as a way of getting the byte array data into the database - files on the OS can be defined as "tables" in the database, and contents selected from - a nice way of bulk loading data rather than using sqlLoader (bypasses sqlNet, etc, however the data for loading has to be on a disk mounted by the database server to work, so not applicable in all situations, e.g. we bulk load data into an Oracle database from several seperate machines, so we use sqlLoader to do this)
Ah - is that the case inzaghi?
inzaghiAuthor Commented:
Apologies for taking so long to reply, there was a problem with EE on Friday.

This is the case.  We want to bulk load the data into Oracle.

The datatype of this field is RAW.  My main question is how should I write the data to the file.  Shall I write it has strings and then use the oracle function to get the binary data as UTL_RAW.CAST_TO_RAW().

The reason I dont think I should use DataOutputStreams is that the process to read the data is not a java component.

Once the data has been loaded into oracle it wil be read by an oracle stored procedure using the DBMS_OBFUSCATION_TOOLKIT.DES3ENCRYPT.

inzaghiAuthor Commented:
Gents your input is valued.  Please help.
You can really write however you like, you just need to ensure you do the same when you are reading it back from the database.
objectsConnect With a Mentor Commented:


and then when you read it:

String s = dis.readUTF();
int n = dis.readInt();
byte[] encrypted = new byte[n];
int remaining = n;
while (remaining>0)
   int read =, n-remaining, remaining);
   remaining -= read;
To give yourself maximum flexibility, write your String(s?) as fixed length UTF-16 encoded fields, followed by the 'binary' data
Or to be more economical with space, get your receiving program to read a null-terminated UTF-16 string followed by the binary data:

String name = "BugsBunny";
char[] nameChars = name.toCharArray();
for(int i = 0;i < nameChars.length;i++) {
// null-terminate it
// Now write 'binary' stuff
inzaghiAuthor Commented:
The string does not need to be unicoded as there is no special characters to cater for.

I wanted to use the BufferdWriter has it is easier to use, the binary data i would have written as a string using:

String a = new String(data[]);

The oracle process would then read the binary data as each field is separated by a # key
and then convert this string object into a raw using the cast_To_raw function.

Is this ok?
> String a = new String(data[]);
> bufferedWriter.write(a);

thats not safe, as you could lose data.
Better to not use a BufferedWriter at all.

inzaghiAuthor Commented:
I will use the DataOutputStream object.
I need to write data as follows:

at the end of each line i need to enter a carriage return.
The # key is used as a field separator.
I have done an example of this using DataOutPutStreams but the text looks strange,
the username has been written to the file with a space between each character
i n z a g h i # encrypted data

why is this?  I am a little worried that the process to read the file will also include spaces in the username
>>The string does not need to be unicoded as there is no special characters to cater for.

And what about in the future?

If you want a non-future-proof solution, just do


Writers should not be used
inzaghiAuthor Commented:
The process doing the reading is oracle
>>why is this?

Probably since you have not written the name as bytes. See my last code



should have been

MogalManicConnect With a Mentor Commented:
Lets start from scratch, because I am getting confused.
Are you writing the data that will be imported into Oracle?  Or are you Reading the data from Oracle and processing it or displaying it?

The conversion from String to byte involves encoding the string using a character set (by default using ISO-8859-1).  This is because the String is meant to represent some display of words or numbers (not binary data)

The conversion from byte to String just uses Integer.toString((int)b, 10).

It probably does not matter which class you use as long as you avoid converting the String to a byte.  If you read bytes and write bytes you are guaranteed that the data will not be converted.

This would be how I would code it (pseudocode)

String name=...//Data is loaded somehow
byte[] encryptedData={..};  //Data is loaded/computed somehow

out.write(name);             //The output will encode this String as a sequence of Bytes
out.write(encryptedData); //This will be written as is
>>It probably does not matter which class you use as long as you avoid converting the String to a byte

I assume you mean 'byte array' here, but am puzzled why you should say this

Since inzaghi starts with a String it has to be converted to bytes ;-)
Please refer (in a circular manner ;-)) to

as the proposed storage scheme is potentially flawed
inzaghiAuthor Commented:
That wasn't necessary actually. But please - let's be very precise about your proposed storage scheme (which seems to be different in the new Q)
inzaghiAuthor Commented:
My storage sheme is the same
username#encrypteddata followed by \r and \n
OK, but that's not what you have in your latest Q.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.