我想知道C#中等效的Java DataInputStream类是什么
每当我使用HttpConnection的类Java ME,Android或者BlackBerry,我使用DataInputStream/ DataOutputStream类在远程服务器上读,写DATAS.然而,还有其他类似InputStream/ OutputStream可用于相同目的.我看到了关于InputStream/ OutputStream课的问题HttpConnection.所以我想从专家那里了解到这两者之间的区别是什么?
我在客户端上有以下代码:
DataInputStream dis = new DataInputStream(socketChannel.socket().getInputStream());
while(dis.available()){
SomeOtherClass.method(dis);
}
Run Code Online (Sandbox Code Playgroud)
但是available()不断返回0,尽管流中有可读数据。因此,在要读取的实际数据完成之后,空数据将传递到另一个要读取的类,这将导致损坏。
经过一番搜索;我发现available()在与套接字一起使用时这是不可靠的,我应该从流中读取前几个字节以实际查看是否有数据可解析。
但就我而言 我必须将DataInputStream我从套接字获得的引用传递给我无法更改的其他类。
是否可以从中读取一些字节DataInputStream而不破坏它,或者有其他建议?
有没有办法询问DataInputStream是否有要读取的内容?.readByte()只会将其挂起,等待读取一个字节:(还是我总是必须发送一个Dummy-Byte,以确保它总是看到东西?
我是一个新的程序员,我正在处理的代码有几个问题.
基本上代码所做的是从另一个JSP接收表单,读取字节,解析数据,并使用DataInputStream将结果提交给SalesForce.
//Getting the parameters from request
String contentType = request.getContentType();
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
//System.out.println(formDataLength);
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength)
{
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但只有代码处理正常字符.每当它试图处理特殊字符(如法语字符:àâäæçéèêëîïôùûü)时,我会得到以下乱码:
ÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃ
我知道它可能是DataInputStream的问题,以及它如何不返回UTF-8编码的文本.你们对如何解决这个问题提出任何建议吗?
所有.jsp文件都包含<%@ page pageEncoding ="UTF-8"contentType ="text/html; charset = UTF-8"%>并且Tomcat的设置很好(URI = UTF-8等).我尝试添加:
request.setCharacterEncoding("UTF-8");
和
response.setCharacterEncoding("UTF-8");
无济于事.
以下是解析数据的示例:
//Getting the notes for the Case
String notes = new String(dataBytes);
System.out.println(notes);
String savenotes …Run Code Online (Sandbox Code Playgroud) 我试图理解这段代码
DataInputStream stream =
new DataInputStream(
new ByteArrayInputStream(messageBuffer));
int messageLength = stream.readInt();
char recordType = (char) stream.readByte();
byte padding = stream.readByte();
short numberRecords = stream.readShort();
Run Code Online (Sandbox Code Playgroud)
messageBuffer初始化为新字节[32768],通过Socket.read()方法填充.我不明白的是,一旦messageLength初始化为stream.readInt(),第二个第二个语句将如何工作,即recordType?
第一个语句不会从字节数组的开头读取一个int而下一个语句从字节数组的开头读取一个字节吗?究竟是如何知道从哪个点读取字节,整数,短路等?
我的守则 -
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectStreamExample {
/**
* @param args
*/
public static void main(String[] args) {
Person person = new Person();
person.setFirstName("Abhishek");
person.setLastName("Choudhary");
person.setAge(25);
person.setHouseNum(256);
ObjectOutputStream stream = null;
try {
stream = new ObjectOutputStream(new FileOutputStream(new File("Serialize.txt")));
stream.writeUTF(person.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(stream != null)
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
} …Run Code Online (Sandbox Code Playgroud) 我下面的代码只解析数据文件一次.我试图让它解析整个文件.每次找到标记时,都会解析数据并将其附加到输出文件中.目前它成功解析数据然后停止.无法弄清楚如何让它循环直到eof.数据是4字节对齐的,并且在输入二进制文件中.
private static void startParse(File inFile) throws IOException {
boolean markerFound = false;
for (int offset = 0; !markerFound && offset < 4; offset++){
DataInputStream dis = new DataInputStream(new FileInputStream(inFile));
for (int i = 0; i < offset; i++){
dis.read();
}
try {
int integer;
long l;
while((l = (integer = dis.readInt())) != MARKER) {
//Don't do anything
}
markerFound = true;
for (int i = 0; i < 11; i++){
dis.read();
}
// ********************** data **********************
byte[] data = …Run Code Online (Sandbox Code Playgroud) 我有一个名为的字节数组byteArr[]。我需要从中删除前4个字节。我的代码如下所示。在这里我使用字节数组存储输入字符串。我在输出中得到了一些不需要的字节,即从第五个开始不需要前四个字节是正确的。我的程序是使用rfid机器从受尊敬的rfid标签中获取ID。
public class Serverc {
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public static void connection() throws IOException {
ServerSocket ss = new ServerSocket(9888);//exce
ss.setSoTimeout(300000000);//exce
System.out.println("Waiting for client …Run Code Online (Sandbox Code Playgroud) 我是网络编程的新手,之前从未使用过Java进行网络编程.我正在使用Java编写服务器,我从客户端处理消息时遇到了一些问题.我用了
DataInputStream inputFromClient = new DataInputStream( socket.getInputStream() );
while ( true ) {
// Receive radius from the client
byte[] r=new byte[256000];
inputFromClient.read(r);
String Ffss =new String(r);
System.out.println( "Received from client: " + Ffss );
System.out.print("Found Index :" );
System.out.println(Ffss.indexOf( '\a' ));
System.out.print("Found Index :" );
System.out.println(Ffss.indexOf( ' '));
String Str = new String("add 12341\n13243423");
String SubStr1 = new String("\n");
System.out.print("Found Index :" );
System.out.println( Str.indexOf( SubStr1 ));
}
Run Code Online (Sandbox Code Playgroud)
如果我这样做,并有一个示例输入asg 23\aag,它将返回:
Found Index :-1
Found Index :3
Found Index :9 …Run Code Online (Sandbox Code Playgroud)