3

Simple Question.

What is Difference with dis.read() and dis.readUTF()?

For example, dis.read() only read to byte array, and dis.readUTF() access String type.

Is it correct?

If Server has implements dis.readUTF(), it can not read byte stream?

@Override
public void run() {
    // TODO Auto-generated method stub
    while(mEnabled)
    {
        if (!mFileReceive) {
            try {
                // read
                String tmpStr = dis.readUTF();
                // here come `dis.readUTF()` <- it is can not read byte array?

                mStringBuffer += tmpStr;

                if (tmpStr.length() >= 4096)
                    continue;
                System.out.println("Print : " + mStringBuffer);

                parse = new ParseJSON(null, mStringBuffer.toString());
                // Ack Message
                if (mAckEnabled) {
                    mFileName = "{opcode:0x06,ACK:C" + parse.getParsedData().get("ACK").substring(1) + "}";
                    dos.writeUTF(mFileName);
                    dos.flush();
                    System.out.println("Ack Message Send : " + mFileName);
                    mFileName = null;
                }
                if (parse.getParsedData().get("opcode").equals("155")) {
                    mFileReceive = true;
                }
                parse.clear();
                parse = null;

            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("ServerThread disconnect");
                break;
            }
Surender Thakran
  • 3,958
  • 11
  • 47
  • 81
reinhard.lee
  • 503
  • 4
  • 10
  • 24
  • (1) What part of the Javadoc didn't you understand? (2) "If Server has implements dis.readUTF(), it can not read byte stream?" is meaningless. `DataInputStream` and `ObjectInputStream` implement both methods. – user207421 Jun 05 '12 at 05:54
  • @EJP My Question is 'dis.readUTF' can read byte stream. JAVADOC says **Bytes for this operation are read from the contained input stream.** Client send to Server Byte array. But Server didn't catch it. – reinhard.lee Jun 05 '12 at 06:11
  • 1
    *All* those APIs read from the byte stream. However `readUTF()` reads the format that `writeUTF() writes. If you're not calling `writeUTF()` I don't understand why you would want to call `readUTF()`. – user207421 Jun 05 '12 at 07:25
  • @EJP Thank you so much, `readUTF()`, `writeUTF()` look like partner. – reinhard.lee Jun 05 '12 at 08:45
  • @EJP My Ubuntu uses Locale KO_KR.utf8. So, I intent to Use 'readUTF()'. Thank you So Much. Your Advice is very helpful. – reinhard.lee Jun 05 '12 at 08:53

1 Answers1

2

readUTF() reads from the stream in a representation of a Unicode character string encoded in modified UTF-8 format; this string of characters is then returned as a String.

You should use read method which takes bytes array as an argument. Here is its explanation:

public final int read(byte[] b) throws IOException

Reads some number of bytes from the contained input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer.