如何包含BufferedReader忽略的newLine字符

时间:2017-10-26 04:29:14

标签: java sockets cryptography bufferedreader

我需要使用缓冲读取器通过套接字进行通信,我只是想知道如何保留新行。我希望最终在密码学中使用它,所以我不能假设新行在消息的末尾。

我对套接字是新手,所以任何帮助都会非常感激。

客户端发送的文本

   ct.txt3nd0fh34d3rl1n31663nd0fh34d3rl1n3This is a test message to see if the file decrypts properly. Nothing special in this file.
   Just a pure test to see if my code works. Two chemists walk into a bar...

字符串的长度为205

在服务器端我得到

    ct.txt3nd0fh34d3rl1n31663nd0fh34d3rl1n3This is a test message to see if the file decrypts properly. Nothing special in this file.
    Just a pure test to see if my code works. Two chemists walk into a bar...

这一个的长度为206,最后添加1个新行字符。这是在我尝试实施我发现的一些解决方案之后,但他们没有工作,因为我关闭1个字符。 以下是读取客户端输入的代码

while (incoming != null)
    {
        /* If the client has sent "exit", instruct the server to
         * remove this thread from the vector of active connections.
         * Then close the socket and exit.
         */
        if (incoming.compareTo("exit") == 0)
        {
            parent.kill (this);
            try {
                in.close ();
                sock.close ();
            }
            catch (IOException e)
            {/*nothing to do*/}
            return;
        }

        /* If the client has sent "die", instruct the server to
         * signal all threads to shutdown, then exit.
         */
        else if (incoming.compareTo("die") == 0)
        {
            parent.killall ();
            return;
        }   

        System.out.println("LINE: " + incoming.length() + " " + incoming);
        if(incoming.equals("3nd0f5krr4hf1l3") && !msgGotten)
        {
            //Decrypt
            SecretKeySpec key = CryptoUtilities.key_from_seed("H".getBytes());

            System.out.print(message);
            System.out.println(message.length());
            byte[] encryptedMessageAndDigest = message.getBytes();
            byte[] decryptedMessageAndDigest = CryptoUtilities.decrypt(encryptedMessageAndDigest, key);

            if(CryptoUtilities.verify_hash(decryptedMessageAndDigest, key))
            {
                byte[] headerMessage = CryptoUtilities.extract_message(decryptedMessageAndDigest);

                String payload = new String(headerMessage);

                String[] plainTextArray = payload.split("3nd0fh34d3rl1n3", 3);
                String fileName = plainTextArray[0];
                byte[] plaintextBytes = plainTextArray[2].getBytes();

                try 
                {
                    FileOutputStream fos = new FileOutputStream(fileName);
                    fos.write(plaintextBytes);
                    fos.close();
                } 
                catch (IOException e) 
                {
                }
            }
            else
            {

            }
        }
        else
        {
            /* Otherwise, just echo what was received. */
            //System.out.println ("Client " + idnum + ": " + incoming);

            if(incoming.length() == 0)
            {
                message = message + "\n";
            }
            else
            {
                message = message + incoming + "\n";
            }

        }

提前致谢!

编辑:在我的初始传输结束时,我发送3nd0f5krr4hf1l3表示它。我需要readLine()所以我可以一次解析,但如果read()真的是最好的方法,我将如何实现这一点? \ n(\ r \ n)charatcers会在阅读中出现吗?

1 个答案:

答案 0 :(得分:1)

如果你不能假设有行终止符,显然你不能首先使用gitk --all。您应该使用其中一个readLine()重载。

  

read()\n)charatcers会在阅读中出现吗?

所有字符都会在\r\n中显示。