Solved

using DataOutputStreams

Posted on 2004-08-17
28
314 Views
Last Modified: 2010-03-31
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

0
Comment
Question by:inzaghi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 7
  • 3
  • +4
28 Comments
 
LVL 11

Assisted Solution

by:cjjclifford
cjjclifford earned 150 total points
ID: 11823174
Hi,

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....

Cheers,
C.
0
 
LVL 3

Assisted Solution

by:RuadRauFlessa
RuadRauFlessa earned 50 total points
ID: 11823957
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
0
 
LVL 4

Assisted Solution

by:astorer
astorer earned 50 total points
ID: 11828840
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 http://iharder.sourceforge.net/base64/ for some code to base64 encode & decode.

0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 86

Expert Comment

by:CEHJ
ID: 11829621
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?
0
 
LVL 11

Assisted Solution

by:cjjclifford
cjjclifford earned 150 total points
ID: 11829727
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)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11829782
Ah - is that the case inzaghi?
0
 

Author Comment

by:inzaghi
ID: 11869417
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.

Thanks
0
 

Author Comment

by:inzaghi
ID: 11879240
Gents your input is valued.  Please help.
0
 
LVL 92

Expert Comment

by:objects
ID: 11879277
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.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 11879284
>>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?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 75 total points
ID: 11879312
eg.

dos.writeUTF(s);
dos.writeInt(encrypted.length);
dos.write(encypted);

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 = dis.read(encrypted, n-remaining, remaining);
   remaining -= read;
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11879341
To give yourself maximum flexibility, write your String(s?) as fixed length UTF-16 encoded fields, followed by the 'binary' data
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11879406
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++) {
      dataOut.writeChar(nameChars[i]);
}
// null-terminate it
dataOut.writeChar(0);
// Now write 'binary' stuff
0
 

Author Comment

by:inzaghi
ID: 11879433
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[]);
bufferedWriter.write(a);

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?
0
 
LVL 92

Expert Comment

by:objects
ID: 11879479
> String a = new String(data[]);
> bufferedWriter.write(a);

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

0
 

Author Comment

by:inzaghi
ID: 11879542
I will use the DataOutputStream object.
I need to write data as follows:
userName#encyrptedData
userName#encryptedData

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
eg
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11879553
>>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

dataOut.write(someString.getBytes());

Writers should not be used
0
 

Author Comment

by:inzaghi
ID: 11879554
The process doing the reading is oracle
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11879560
>>why is this?

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

Expert Comment

by:CEHJ
ID: 11879577
dataOut.write(userName.getBytes());
dataOut.writeByte('#');
dataOut.writeByte(encryptedByteArray);
dataOut.writeByte('\r');
dataOut.writeByte('\n');




0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11879581
Oops

>>dataOut.writeByte(encryptedByteArray);

should have been

dataOut.write(encryptedByteArray);
0
 
LVL 21

Assisted Solution

by:MogalManic
MogalManic earned 75 total points
ID: 11879680
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 Java.io 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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11880485
>>It probably does not matter which Java.io 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 ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11882697
Please refer (in a circular manner ;-)) to

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21105258.html

as the proposed storage scheme is potentially flawed
0
 

Author Comment

by:inzaghi
ID: 11882736
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11882799
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)
0
 

Author Comment

by:inzaghi
ID: 11882885
My storage sheme is the same
username#encrypteddata followed by \r and \n
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11882947
OK, but that's not what you have in your latest Q.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question