Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6544437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:27:55+00:00 2026-05-25T11:27:55+00:00

I need some help about optimisation. I am trying to improve this open-source game

  • 0

I need some help about optimisation. I am trying to improve this open-source game server made with JAVA. Each player has its own thread, and each thread goes something like this:

BufferedReader _in = new BufferedReader(new InputStreamReader(_socket.getInputStream()));

String packet = "";
char charCur[] = new char[1];

while(_in.read(charCur, 0, 1)!=-1)
{
    if (charCur[0] != '\u0000' && charCur[0] != '\n' && charCur[0] != '\r')
    {
        packet += charCur[0];

    }else if(!packet.isEmpty())
    {
        parsePlayerPacket(packet);
        packet = "";
    }
}

I have been told so many times that this code is stupid, and I agree because when profiling it I see that reading each byte and appending it using packet += "" is just stupid and slow.

I want to improve this but I don’t know how.. I’m sure I can find something, but I’m afraid it will be even slower than this, because I have to split packets based on the '\u0000‘, '\n', or '\r' to parse them. And I know that splitting 3 times is verry slow.

Can someone give me an idea? Or a piece of code for this? It will make my day.

If you’re going to explain, please, please use verry simple words, with code examples, I’m just a JAVA beginner. Thank’s

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:27:56+00:00Added an answer on May 25, 2026 at 11:27 am

    Perhaps you should look into the readLine() method of BufferedReader. Looks like you’re reading Strings, calling BufferedReader.readLine() gives you the next line (sans the newline/linefeed).

    Something like this:

    String packet = _in.readLine();
    while(packet!=null) {
        parsePlayerPacket(packet);
        packet = _in.readLine();
    }
    

    Just like you’re implementation, readLine() will block until either the stream is closed or there’s a newline/linefeed.

    EDIT: yeah, this isn’t going to split ‘\0’. You’re best bet is probably a PushbackReader, read in some buffer of chars (like David Oliván Ubieto suggests)

    PushbackReader _in = new PushbackReader(new InputStreamReader(_socket.getInputStream()));
    StringBuilder packet = new StringBuilder();
    char[] buffer = new char[1024];
    // read in as much as we can
    int bytesRead = _in.read(buffer);
    
    while(bytesRead > 0) {
        boolean process = false;
        int index = 0;
        // see if what we've read contains a delimiter
        for(index = 0;index<bytesRead;index++) {
            if(buffer[index]=='\n' ||
               buffer[index]=='\r' ||
               buffer[index]=='\u0000') {
                process = true;
                break;
            }
        }
        if(process) {
             // got a delimiter, process entire packet and push back the stuff we don't care about
             _in.unread(buffer,index+1,bytesRead-(index+1));  // we don't want to push back our delimiter
             packet.append(buffer,0,index);
             parsePlayerPacket(packet);
             packet = new StringBuilder();
        }
        else {
             // no delimiter, append to current packet and read some more
             packet.append(buffer,0,bytesRead);
        }
        bytesRead = _in.read(buffer);
    }
    

    I didn’t debug that, but you get the idea.

    Note that using String.split(‘\u0000’) has the problem where a packet ending with ‘\u0000’ won’t get processed until a newline/linefeed is sent across the stream. Since you’re writing some kind of game, I assume it’s important to process an incoming packet as soon as you get it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help about xsl. I have a date attribute in source xml
I need some help on this problem. It is about ASP.NET MVC3. I have
need ask you about some help. I have web app running in Net 2.0.
I need some help calculating Pi. I am trying to write a python program
I need some help with jQuery script again :-) Just trying to play with
I need some help to complete my idea about regexes. Introduction There was a
I need some help! I have no doubt this is a dumb problem that
I need some help with my xpath query. I can get this code to
i need some help with switch/case statement syntax im trying to use onClick to
I need some informative help about animation. Want to add animation in iPhone Application..

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.