datainputstream

DataInputStream and UTF-8

我的未来我决定 提交于 2019-11-29 15:40:44
I'm kind of a new programmer, and I'm having a couple of problems with the code I'm handling. Basically what the code does is receive a form from another JSP, read the bytes, parse the data, and submit the results to SalesForce, using DataInputStream. //Getting the parameters from request String contentType = request.getContentType(); DataInputStream in = new DataInputStream(request.getInputStream()); int formDataLength = request.getContentLength(); //System.out.println(formDataLength); byte dataBytes[] = new byte[formDataLength]; int byteRead = 0; int totalBytesRead = 0; while (totalBytesRead

Reading from a URL Connection Java

有些话、适合烂在心里 提交于 2019-11-29 15:12:12
I'm trying to read html code from a URL Connection. In one case the html file I'm trying to read includes 5 line breaks before the actual doc type declaration. In this case the input reader throws an exception for EOF. URL pageUrl = new URL( "http://www.nytimes.com/2011/03/15/sports/basketball/15nbaround.html" ); URLConnection getConn = pageUrl.openConnection(); getConn.connect(); DataInputStream dis = new DataInputStream(getConn.getInputStream()); //some read method here Has anyone ran into a problem like this? URL pageUrl = new URL("http://www.nytimes.com/2011/03/15/sports/basketball

“available” of DataInputStream from Socket

人盡茶涼 提交于 2019-11-29 08:18:39
I have this code on the client side : DataInputStream dis = new DataInputStream(socketChannel.socket().getInputStream()); while(dis.available()){ SomeOtherClass.method(dis); } But available() keeps returning 0 , although there is readable data in the stream. So after the actual data to be read is finished, empty data is passed to the other class to be read and this causes corruption. After a little search; I found that available() is not reliable when using with sockets, and that I should be reading first few bytes from stream to actually see if data is available to parse. But in my case; I

DataInputStream deprecated readLine() method

感情迁移 提交于 2019-11-28 21:09:26
I am on java 6. Using DataInputStream in = new DataInputStream(System.in); to read user input. When the readLine() is deprecated. What is the work around for reading user value? DataInputStream in = new DataInputStream(System.in); int num; try { num = Integer.parseInt(in.readLine()); //this works num = Integer.parseInt(in); //just in doesnt work. } catch(Exception e) { } please explain as it should when the readLine() is deprecated. InputStream is fundamentally a binary construct. If you want to read text data (e.g. from the console) you should use a Reader of some description. To convert an

DataInputStream and UTF-8

陌路散爱 提交于 2019-11-28 09:31:20
问题 I'm kind of a new programmer, and I'm having a couple of problems with the code I'm handling. Basically what the code does is receive a form from another JSP, read the bytes, parse the data, and submit the results to SalesForce, using DataInputStream. //Getting the parameters from request String contentType = request.getContentType(); DataInputStream in = new DataInputStream(request.getInputStream()); int formDataLength = request.getContentLength(); //System.out.println(formDataLength); byte

“available” of DataInputStream from Socket

你。 提交于 2019-11-28 01:44:59
问题 I have this code on the client side : DataInputStream dis = new DataInputStream(socketChannel.socket().getInputStream()); while(dis.available()){ SomeOtherClass.method(dis); } But available() keeps returning 0 , although there is readable data in the stream. So after the actual data to be read is finished, empty data is passed to the other class to be read and this causes corruption. After a little search; I found that available() is not reliable when using with sockets, and that I should be

Close multiple resources with AutoCloseable (try-with-resources)

蹲街弑〆低调 提交于 2019-11-27 18:41:10
I know that the resource you pass with a try, will be closed automatically if the resource has AutoCloseable implemented. So far so good. But what do I do when i have several resources that I want automatically closed. Example with sockets; try (Socket socket = new Socket()) { input = new DataInputStream(socket.getInputStream()); output = new DataOutputStream(socket.getOutputStream()); } catch (IOException e) { } So I know the socket will be closed properly, because it's passed as a parameter in the try, but how should the input and output be closed properly? augray Try with resources can be

DataInputStream deprecated readLine() method

☆樱花仙子☆ 提交于 2019-11-27 13:31:59
问题 I am on java 6. Using DataInputStream in = new DataInputStream(System.in); to read user input. When the readLine() is deprecated. What is the work around for reading user value? DataInputStream in = new DataInputStream(System.in); int num; try { num = Integer.parseInt(in.readLine()); //this works num = Integer.parseInt(in); //just in doesnt work. } catch(Exception e) { } please explain as it should when the readLine() is deprecated. 回答1: InputStream is fundamentally a binary construct. If you

Close multiple resources with AutoCloseable (try-with-resources)

半腔热情 提交于 2019-11-26 19:32:53
问题 I know that the resource you pass with a try, will be closed automatically if the resource has AutoCloseable implemented. So far so good. But what do I do when i have several resources that I want automatically closed. Example with sockets; try (Socket socket = new Socket()) { input = new DataInputStream(socket.getInputStream()); output = new DataOutputStream(socket.getOutputStream()); } catch (IOException e) { } So I know the socket will be closed properly, because it's passed as a parameter