Results 1 to 11 of 11
  1. #1
    Bill88 is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default Java InputStream

    I've been trying to figure this out for a while, but I seem to be stuck.

    Java Code:
    if(gameServerInputStream.available() > 0) {
    byte[] Data = new byte[10000];
    
    int bytes = gameServerInputStream.read(Data);
    				
    ourClientOutputStream.write(Data, 0, bytes);
    ourClientOutputStream.flush();
    }
    ^ That is currently how I read from the input stream in my proxy server, and then send it to the client. Which seems to work fine.

    I'm trying to do something like this:

    Java Code:
    if(gameServerInputStream.available() > 0) {				
    	int packetLength = readPacket(packetData);
    		if (packetLength > 0)
    		       checkIncomingPacket(packetData[0] & 0xff, packetLength);
    }
    And then the methods that are called in the above code:

    Java Code:
    protected final void checkIncomingPacket(int command, int length) {
    System.out.println("Command: " + command);
    }
    
        public int readPacket(byte data[]) {
            try {
                packetReadCount++;
                if (1000 > 0 && packetReadCount > 1000) {
                    error = true;
                    return 0;
                }
                if (length == 0 && gameServerInputStream.available() >= 2) {
                    length = gameServerInputStream.read();
                    if (length >= 160)
                        length = (length - 160) * 256+gameServerInputStream.read();
                }
                if (length > 0 && gameServerInputStream.available() >= length) {
                    if (length >= 160) {
                        readInputStream(length, 0, data);
                    } else {
                        data[length - 1] = (byte) gameServerInputStream.read();
                        if (length > 1)
                            readInputStream(length - 1, 0, data);
                    }
                    int readBytes = length;
                    length = 0;
                    packetReadCount = 0;
                    return readBytes;
                }
            }
            catch (IOException ioexception) {
            }
            return 0;
        }
    
    public void readInputStream(int length, int offset, byte abyte0[])
    throws IOException {
    	int k = 0;
    	int l;
    for (; k < length; k += l)
    if ((l = gameServerInputStream.read(abyte0, k + offset, length - k)) <= 0)
    	throw new IOException("EOF");
    
    }
    This would work fine, except that this does not send anything that is received from the server, to the client.
    And this is where my problem is. I want to do this, and still send what is received to the client.

    If I were to do something like this:

    Java Code:
    if(gameServerInputStream.available() > 0) {
    	byte[] Data = new byte[10000];
    	int bytes = gameServerInputStream.read(Data);
    				
    	int packetLength = readPacket(packetData);
    	if (packetLength > 0) 
    	checkIncomingPacket(packetData[0] & 0xff, packetLength);
    				
    	ourClientOutputStream.write(Data, 0, bytes);
    	ourClientOutputStream.flush();
    }
    It wouldn't work because, when something is sent from the server, it is all stored in Data[] and then there is nothing left to read in the readPacket() method.

    So basically what I am trying to do is, read from the input stream and send it to the client.
    Without making whatever it reads unavailable for the next time it reads from the input stream (readPacket() method).

    Sorry if I didn't explain it very well, I've been trying to figure this out for a while, and still no luck.

    I would appreciate any replies.
    Last edited by Bill88; 09-18-2009 at 07:59 AM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    "available()" is completely useless on a network connection, and not all that much use on most other inputstreams. Just simply let the read call block until it gets something, or the connection is closed. You do have this reader in it's own thread, right?

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    I don't understand why you're reading twice.
    Can't you simply use the data you've already read to do whatever it is you're doing in readPacket?

    Leaving aside I don't know what it is you're doing (bar reading from one thing and writing that to another)...there's an awful lot of details missing here in terms of what each of the variables are.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    As far as "going back" in a stream (if the stream is capable of it) see the API docs for inputstream and the mark and reset methods.

  5. #5
    Bill88 is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    As far as "going back" in a stream (if the stream is capable of it) see the API docs for inputstream and the mark and reset methods.
    Thanks, but it doesn't seem to be supported. Probably would of worked great.

  6. #6
    ducreative is offline Member
    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0

    Default Input technique for unknown lines of input

    Suppose, i have to take several lines of input and i don't know how many number of lines are given by user. After getting all lines of input string i have to display all lines accordingly.
    here is my code
    Java Code:
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.Scanner;
    
    public class InputTechnique {
        public static void main(String args[]){
             Scanner scan = new Scanner(System.in);
             ArrayList<String> aList = new ArrayList<String>();
             while(scan.hasNextLine()){
                 aList.add(scan.nextLine());
             }
             Iterator it = aList.iterator();
             while(it.hasNext())
                 System.out.println(it.next());
        }
    }

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Java Code:
    while(scan.hasNextLine()){
                 aList.add(scan.nextLine());
    }
    I don't think that will ever terminate unless I'm mistaken -- since you're reading from System.in, unless System.in is piped in (such as parameters, or a file). But if System.in is live user input, it'll loop forever -- in which case you'll need to add a code word of some sort like "done" or "go" or something and then check for it each round.

    The rest of the code looks fine.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    Of course that does raise the question of what that has to do with the OPs original question.

  9. #9
    ducreative is offline Member
    Join Date
    Mar 2009
    Posts
    9
    Rep Power
    0

    Default re:Input technique for unknown lines of input

    Yes, both of you are right. But i want to know that is there any method by which i can detect the end of lines. Let clear the problem once again. Suppose user can feed up program 3 lines as:
    Java Code:
    Department of Computer science
    and 
    engineering University of Dhaka
    But our programmer does not know how many lines are there and we have to process one line and then to next line. After reading all the lines i have to display all inputs accordingly. But my program hangs on the first while loop and cannt break it.
    Output should be

    Java Code:
    Department of Computer science
    and 
    engineering University of Dhaka

    Thanks
    Last edited by ducreative; 09-21-2009 at 02:20 PM.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    You are hijacking someone elses thread, which is rather rude.
    Why don't you post your own thread?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Creating an InputStream
    By c26354 in forum New To Java
    Replies: 10
    Last Post: 07-12-2011, 04:48 AM
  2. [SOLVED] InputStream into ByteBuffer
    By OrangeDog in forum Advanced Java
    Replies: 7
    Last Post: 04-29-2009, 12:19 PM
  3. Which InputStream takes the least memory?
    By arnab321 in forum New To Java
    Replies: 9
    Last Post: 01-13-2009, 05:45 PM
  4. Converting InputStream to OutputStream
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 01-11-2008, 10:13 PM
  5. Reading bytes from InputStream
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-25-2007, 07:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •