如何在java中将接收的字节显示为文本?

时间:2016-09-13 23:51:09

标签: java sockets delphi

首先,我对编程非常陌生,我想要完成的任务是:

  

从我的delphi程序发送字节到java服务器(完成!)

     

在java服务器中显示收到的字节

T H E问题:如何在java中显示收到的字节?

如果你能提供帮助,那就非常感谢你,这将至少让我失去一个不眠之夜。

来自delphi应用程序的工作代码示例:

DocumentRoot "e:/Archives"

我能够组合起来的java服务器代码,可能是一个不存在的。

procedure sendInt(val: integer);
begin
  Socket.SendByte(byte(val shr 24));
  Socket.SendByte(byte(val shr 16));
  Socket.SendByte(byte(val shr 8));
  Socket.SendByte(byte(val));
end;

做什么:如果我运行java服务器,并发送字节 - 返回null。 如果我发送连续字节 - 服务器什么也不做,只有在我完成发送后才显示一大堆空值,不确定是否有帮助,只是尝试尽可能提供mutch信息。谢谢 !

2 个答案:

答案 0 :(得分:1)

假设您想要传递并显示在流中格式化的int32(您正在发送big-endian),那么概念上最简单的方法是:

InputStream is = s.getInputStream();
int val = 0;
for(int i = 0; i<4; i++) {
    val <<= 8; // does nothing first time
    val |= is.read();
}
System.out.println(val);

将收到的字节汇编成int32并将其打印为带换行符的格式化字符串。

答案 1 :(得分:0)

假设你打算重建一个字符串,那么InputStreamReaderLineNumberReader就是你的朋友。

Java是少数几个难以强制执行字符集管理(例如Unicode)的平台之一,因此您不能轻易地交换字节和字符。但你可以这样做:

Coverity Scan analysis selected for branch coverity_scan.
Coverity Scan analysis authorized per quota.
$ curl -s https://scan.coverity.com/scripts/travisci_build_coverity_scan.sh | COVERITY_SCAN_PROJECT_NAME="$PROJECT_NAME" COVERITY_SCAN_NOTIFICATION_EMAIL="<MY_EMAIL>" COVERITY_SCAN_BUILD_COMMAND="--no-command" COVERITY_SCAN_BUILD_COMMAND_PREPEND="" COVERITY_SCAN_BRANCH_PATTERN=coverity_scan bash
Note: COVERITY_SCAN_PROJECT_NAME and COVERITY_SCAN_TOKEN are available on Project Settings page on scan.coverity.com
Coverity Scan configured to run on branch coverity_scan
Coverity Scan analysis authorized per quota.
Downloading Coverity Scan Analysis Tool...
2016-09-13 23:26:36 URL:https://scan.coverity.com/download/Linux [449455458/449455458] -> "/tmp/cov-analysis-Linux.tgz" [1]
Extracting Coverity Scan Analysis Tool...
/tmp/coverity-scan-analysis ~/build/<PROJECT_NAME>
~/build/<PROJECT_NAME>
Running Coverity Scan Analysis Tool...
Coverity Build Capture (64-bit) version 8.5.0.3 on Linux 3.13.0-92-generic x86_64
Internal version numbers: db70178643 p-kent-push-26368.949
[WARNING] No files were emitted. This may be due to a problem with your configuration
or because no files were actually compiled by your build command.
Please make sure you have configured the compilers actually used in the compilation.
 For more details, please look at: 
    /home/travis/build/<PROJECT_NAME>/cov-int/build-log.txt
Extracting SCM data for 0 files...
Please see the log file '/home/travis/build/<PROJECT_NAME>/cov-int/scm_log.txt' for warnings and SCM command issues.
[WARNING] Unable to gather all SCM data - see log at /home/travis/build/<PROJECT_NAME>/cov-int/scm_log.txt for details.
Successfully added SCM data for 0 files
Tarring Coverity Scan Analysis results...
Uploading Coverity Scan Analysis results...

......当然还有处理例外情况。 InputStreamReader允许你对字节流执行read()作为字符(并且,注意,UTF8编码是默认的,但不是你可以使用的唯一编码),LineNumberReader让你逐行而不是char-由炭。

当然,如果你想要char-by-char,你可以使用read()并完全丢失LineNumberReader。类似的东西:

LineNumberReader lines = new LineNumberReader(
    new InputStreamReader(
        s.getInputStream()
    )
);
String someLine = lines.readLine();

另外,请注意无效的Unicode字节序列。有关处理Charsets的指示,请参阅InputStreamReader文档。