-1

Here is how I put a string to ByteBuffer

String message="Hello\n\n";
ByteBuffer bresult = ByteBuffer.allocate(message.getBytes().length);
bresult.put(message.getBytes());
bresult.flip();

When I convert bytebuffer to string to see the result \n\n is removed from the above string. This how I convert ByteBuffer to String

print(new String(bresult.array()));

and the result is Hello without any line breaks. You can see the result in the below screenshot from my log [![enter image description here][1]][1]

but when I add spaces to hello string like message="Hello\n\n " the result is the below: [![enter image description here][2]][2] as you can see there are some line breaks under hello string.

  • why I got (-1) point to my question. I really do not know the answer and the only answer I got now is never ask any questions in stackoverflow because I will get minus score on something that I don't know. – Mohammad Javadian Jul 22 '20 at 05:58
  • Where is the part of the code where you convert back to `String` to inspect the result and see that its "wrong"? Please post a **full** [mcve]. – Zabuzard Jul 22 '20 at 06:00
  • You will only get downvoted if your question doesnt pass the requirements of [ask], also see [help]. You wont get downvoted just because you dont know something. For me, a valid reason to downvote here would be because it is **unclear** how exactly you determine that something is wrong, you only shared half of the relevant code. So it is impossible for a helper to explain what exactly in your approach might be wrong. – Zabuzard Jul 22 '20 at 06:01
  • 3
    Your question being downvoted is not a personal insult. All it means is that your question is not a good question. In this case, your question is unanswerable. Nothing in the provided example would remove the two line feed characters and you don't show the code demonstrating otherwise; nor can I reproduce the problem from the given code. You need to provide a [mre]. – Slaw Jul 22 '20 at 06:12
  • 1
    Thanks for your feedback, I think now it is in the right format – Mohammad Javadian Jul 22 '20 at 06:15
  • Please note that ur `getBytes` and `new String(...)` call both use your systems default character encoding. They should instead both use `UTF-8`, you specify that encoding explicitly here, otherwise you wilt lose characters that can not be represented in that encoding and easily run into converting artifacts if you have an encoding mismatch. – Zabuzard Jul 22 '20 at 06:16
  • You are `print`ing text that contains newlines. `\n` is a newline, you wont really visually see the newline in your output. What did you expect? That it writes `\n` literally? Could you share a screenshot of your console output? – Zabuzard Jul 22 '20 at 06:17
  • Voting to close for *can not reproduce* - as shown by @Slaw – Zabuzard Jul 22 '20 at 06:21
  • I add some screen shots. – Mohammad Javadian Jul 22 '20 at 06:26
  • why you @Zabuzard want to close my question I add the screenshots – Mohammad Javadian Jul 22 '20 at 06:29
  • As shown by my answer below, the code you've provided does not cause the problem you describe. There must be more going on in code you don't show us. Try to create a [mre] from scratch. Look to my answer for guidance; the example I give is _minimal yet complete_, which means you can copy it onto your machine and run it with little to no modifications. – Slaw Jul 22 '20 at 06:32
  • Please provide a [mre], especially show exactly how your print method. It sounds like a buffer not getting flushed. – Mark Rotteveel Jul 22 '20 at 06:36
  • Seems that your server/log system didnt print them. Has nothing to do with the bytebuffer. Id suggest you ask a new question where you put focus on that part, and share ALL of the relevant code that time. – Zabuzard Jul 22 '20 at 06:40

1 Answers1

4

I cannot reproduce the problem. The following:

import java.nio.ByteBuffer;

public class App {

  public static void main(String[] args) {
    String str = "Hello\n\n";

    ByteBuffer buf = ByteBuffer.allocate(str.getBytes().length);
    buf.put(str.getBytes());
    buf.flip();

    String str2 = new String(buf.array());

    System.out.println(str.equals(str2));
    System.out.println(str2.endsWith("\n\n"));
  }
}

Gives this output:

true
true

Which means the String created from the byte[] has all the same characters as the original String.

Some notes:

  1. The use of ByteBuffer above is a roundabout way of doing str2 = new String(str.getBytes()). I used ByteBuffer since that's what you used in the question.

  2. Be careful with String#getBytes() and String#<init>(byte[]). Both use the default platform encoding which may or may not cause problems. Consider specifying the encoding explicitly.

  3. If I replace the test with System.out.print(str2) I get the following output:

    Hello
    
    

    That's "Hello" followed by two line breaks. If println was used instead then there would be three line breaks. Note that line breaks are not typically directly visible.

Slaw
  • 37,820
  • 8
  • 53
  • 80