Article View: borland.public.jbuilder.java.language
Article #7923Reading a binary stream using URLConnection
From: Harry Whitehouse
Date: Wed, 02 May 2001 00:49
Date: Wed, 02 May 2001 00:49
75 lines
3828 bytes
3828 bytes
I have a legacy server module which receives binary requests from a Web
client (historically a WIN32 client and now a Java applet), and returns a
935 byte binary response to the client. With invaluable assistance from
members on this list, I have managed to get this working at a crude level
(my code is below). Now I need to stabilze this process and am in need of
some further guidence..
This is a POST operation. The request data makes it to the server perfectly.
No problem there whatsoever using dos.write().
Now collecting the binary response has been kludged as shown below. I
appear to be on the right track in using the JDK 1.1 DataInputStream because
it sidesteps all the Unicode conversion issues which would compromise the
8bit-byte response.
But I have a bunch of unresolved questions.
a). Periodically, I don't get a complete response from the server. It may
be because I don't have use any buffering, or it might be the very crude way
I handle readByte() -- sloppily leaving 5 currently-unused response bytes in
limbo. I'm wondering if folks can comment on one or both issues.
b). My understanding of readByte() is that it should be in it's own
try...catch logic, and that's the way I should detect the end of the
transmission (rather than my crude counting scheme). But that would nest a
try...catch inside any already established try....catch. Is that
permissible? (I tried to code this and got a bunch of compiler errors).
c) Basically, I need a robust replacement for the code between the
*****************'s.
Any and all help will be most appreciated!!!
TIA
Harry
try {
String VPOURL2="http://36.168.115.100/webtest/test.dll";
URL url = new URL(VPOURL2);
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches (false);
DataOutputStream dos = new
DataOutputStream(uc.getOutputStream());
// Send the data -- this seems to pass all of the hex information
with any Unicode issues!
dos.write(RequestHEX);
dos.flush();
dos.close();
// we must use a JDK1.1 Implementation to just read 8 bit bytes!!
// *************************************************************
java.io.DataInputStream MyStream = new
DataInputStream(uc.getInputStream());
byte [] DataStream = new byte[2000];
byte ch;
int j=0;
ch = MyStream.readByte();
while(ch != -1) {
DataStream[j++]=ch;
ch = MyStream.readByte();
if(j>=930) // SB 935 long
break;
}
//
*****************************************************************
} catch(Exception exVPO) {
JOptionPane.showMessageDialog(null, "Status Check", "Data Error!",
JOptionPane.ERROR_MESSAGE);
}
Technical Details
Message-ID:
<3aef5973$1_2@dnews>Path:
arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!news.maxwell.syr.edu!news-hog.berkeley.edu!ucberkeley!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!dnewsShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,d0971040234e8a02,start X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-05-01 17:52:07 PST From: "Harry Whitehouse" <harry@envmgr.com> Newsgroups: borland.public.jbuilder.java.language Subject: Reading a binary stream using URLConnection Date: Tue, 1 May 2001 17:49:18 -0700 Lines: 78 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 NNTP-Posting-Host: 38.168.115.135 Message-ID: <3aef5973$1_2@dnews> X-Trace: dnews 988764531 38.168.115.135 (1 May 2001 17:48:51 -0700) Path: arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!news.maxwell.syr.edu!news-hog.berkeley.edu!ucberkeley!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!dnews