下面列出了java.io.DataInputStream#read ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected
NetworkAdminNATUDPRequest(
DataInputStream is,
long connection_id ,
int trans_id )
throws IOException
{
super( NetworkAdminNATUDPCodecs.ACT_NAT_REQUEST, connection_id, trans_id );
short len = is.readShort();
if ( len <= 0 ){
throw( new IOException( "invalid length" ));
}
byte[] bytes = new byte[len];
is.read( bytes );
payload = BDecoder.decode( bytes );
}
public String doCurl(String urlString) throws IOException {
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setInstanceFollowRedirects(true);
con.setDoOutput(true);
con.setDoInput(true);
DataOutputStream output = new DataOutputStream(con.getOutputStream());
output.close();
DataInputStream input = new DataInputStream(con.getInputStream());
int c;
StringBuilder resultBuf = new StringBuilder();
while ((c = input.read()) != -1) {
resultBuf.append((char) c);
}
input.close();
return resultBuf.toString();
}
public static ByteSequence decodeByteSequence(DataInputStream in) throws IOException {
int length = in.readInt();
if (length == -1) {
return null;
}
if (length == 0) {
return new ArrayByteSequence(new byte[0]);
}
byte[] data = new byte[length];
int read = in.read(data);
if (read != length) {
throw new IOException("Expected " + length + " bytes, found " + read + " bytes");
}
return new ArrayByteSequence(data);
}
private static boolean isSequenceFile(FileSystem fs,
Path f) throws IOException {
DataInputStream in = fs.open(f);
try {
byte[] seq = "SEQ".getBytes();
for (int i = 0; i < seq.length; ++i) {
if (seq[i] != in.read()) {
return false;
}
}
} finally {
in.close();
}
return true;
}
public final static void read26(DataInputStream din)
throws IOException
{
for(int i=0; i<26; i++){
din.read();
}
}
protected void build(OutputStream os) throws IOException {
DataInputStream dis = new DataInputStream(classLoader.getResourceAsStream(resourceName));
byte[] buf = new byte[256];
int sz;
while( (sz=dis.read(buf))>0 )
os.write(buf,0,sz);
dis.close();
}
/**
* Read a number of bytes or throw.
*
* @param size The number of bytes read
* @return The array of bytes read.
* @throws IOException
*/
public static byte[] readBytes(DataInputStream stream, int size) throws IOException {
byte[] buf = new byte[size];
int toRead = size;
int done = 0;
while (toRead > 0) {
int read = stream.read(buf, done, toRead);
if (read == -1) {
throw new IOException();
}
done += read;
toRead -= read;
}
return buf;
}
protected static byte[] read(final DataInputStream stream, final int numBytes) throws IOException {
final byte[] retval = new byte[numBytes];
final int bytesRead = stream.read(retval);
if (bytesRead < numBytes) {
throw new IllegalTokenException("Not enough bits to generate a Token");
}
return retval;
}
private byte[] readIntAsBytesFrom(final DataInputStream dataStream) throws IOException {
final byte[] buf;
final int length = dataStream.readInt();
buf = new byte[length];
// FindBugs exposed possible bug of not checking for correct output from InputStream
if (dataStream.read(buf, 0, length) != length) {
throw new IOException("Could not read int as bytes");
}
return buf;
}
public static void deserialize(Node node, DataInputStream stream) throws IOException {
int ipLength = stream.readInt();
byte[] ipBytes = new byte[ipLength];
int readSize = stream.read(ipBytes);
if (readSize != ipLength) {
throw new IOException(String.format("No sufficient bytes read when deserializing the ip of "
+ "a "
+ "node: %d/%d", readSize, ipLength));
}
node.setIp(new String(ipBytes));
node.setMetaPort(stream.readInt());
node.setNodeIdentifier(stream.readInt());
node.setDataPort(stream.readInt());
}
Code_attribute(DataInputStream in) throws IOException {
max_stack = in.readUnsignedShort();
max_locals = in.readUnsignedShort();
code_length = in.readInt();
code = new byte[code_length];
in.read(code);
exception_table_length = in.readUnsignedShort();
attributes_count = in.readUnsignedShort();
attributes = new attribute_info[attributes_count];
for (int i = 0; i < attributes_count; i++) {
attributes[i] = new attribute_info(in);
}
}
public static byte[] readBytes(DataInputStream outputStream) throws IOException {
//Read size, then key-values
int size = outputStream.readInt();
byte[] data = new byte[size];
outputStream.read(data, 0, size);
return data;
}
private void loadVersion12Fields(DataInputStream indexStream, PrereadHashFunction headerHash) throws IOException {
if (mVersion >= FULL_SEQUENCE_NAME_VERSION) {
final int hasSuffixesByte = indexStream.read();
headerHash.irvineHash(hasSuffixesByte);
mHasSuffixes = hasSuffixesByte != 0;
} else {
mHasSuffixes = false;
}
}
private void loadCoreStreamBodyToBuffer( DataInputStream stream )
throws IOException
{
byte[] datas = new byte[1024];
int length = -1;
ByteArrayOutputStream byteOut = new ByteArrayOutputStream( );
while ( ( length = stream.read( datas ) ) > 0 )
{
byteOut.write( datas, 0, length );
}
bodyData = byteOut.toByteArray( );
}
/**
* Deserialize key from the stream in {@link KeyRecord_Format_V1}
* @param dataStream the {@link DataInputStream} from which key needs to be deserialized
* @return the deserialized key in the form of {@link DeserializedKey}
* @throws IOException
* @throws MessageFormatException
*/
private static DeserializedKey deserializeKeyRecord(DataInputStream dataStream)
throws IOException, MessageFormatException {
int keySize = dataStream.readInt();
byte[] encodedKey = new byte[keySize];
dataStream.read(encodedKey);
String keyAlgo = Utils.readIntString(dataStream);
return new DeserializedKey(encodedKey, keyAlgo);
}
@Override
public void read(DataInputStream f)
throws IOException {
readbuffer.position(0);
readbuffer.order(ByteOrder.LITTLE_ENDIAN);
f.read(readbuffer.array());
unpack(readbuffer);
}
private ContentClaimTriple consumeContent(final DataInputStream in, final OutputStream out, final ContentClaim contentClaim, final long claimOffset,
final String peerDescription, final boolean compressed) throws IOException {
logger.debug("Consuming content from Peer {}", peerDescription);
int dataFrameIndicator = in.read();
if (dataFrameIndicator < 0) {
throw new EOFException("Encountered End-of-File when expecting to read Data Frame Indicator from Peer " + peerDescription);
}
if (dataFrameIndicator == NO_DATA_FRAME) {
logger.debug("Peer {} indicates that there is no Data Frame for the FlowFile", peerDescription);
return new ContentClaimTriple(null, 0L, 0L);
}
if (dataFrameIndicator == ABORT_TRANSACTION) {
throw new TransactionAbortedException("Peer " + peerDescription + " requested that transaction be aborted");
}
if (dataFrameIndicator != DATA_FRAME_FOLLOWS) {
throw new IOException("Expected a Data Frame Indicator from Peer " + peerDescription + " but received a value of " + dataFrameIndicator);
}
int dataFrameLength = in.readInt();
logger.trace("Received Data Frame Length of {} for {}", dataFrameLength, peerDescription);
byte[] buffer = getDataBuffer();
long claimLength = 0;
while (true) {
final InputStream limitedIn = new LimitedInputStream(in, dataFrameLength);
final ByteCountingInputStream bcis = new ByteCountingInputStream(limitedIn);
final InputStream contentIn = compressed ? new GZIPInputStream(bcis) : bcis;
final int decompressedSize = StreamUtils.fillBuffer(contentIn, buffer, false);
if (bcis.getBytesRead() < dataFrameLength) {
throw new EOFException("Expected to receive a Data Frame of length " + dataFrameLength + " bytes but received only " + bcis.getBytesRead() + " bytes");
}
out.write(buffer, 0, decompressedSize);
claimLength += decompressedSize;
dataFrameIndicator = in.read();
if (dataFrameIndicator < 0) {
throw new EOFException("Encountered End-of-File when expecting to receive a Data Frame Indicator");
}
if (dataFrameIndicator == NO_DATA_FRAME) {
logger.debug("Peer {} indicated that no more data frames are available", peerDescription);
break;
}
if (dataFrameIndicator == ABORT_TRANSACTION) {
logger.debug("Peer {} requested that transaction be aborted by sending Data Frame Length of {}", peerDescription, dataFrameLength);
throw new TransactionAbortedException("Peer " + peerDescription + " requested that transaction be aborted");
}
if (dataFrameIndicator != DATA_FRAME_FOLLOWS) {
throw new IOException("Expected a Data Frame Indicator from Peer " + peerDescription + " but received a value of " + dataFrameIndicator);
}
dataFrameLength = in.readInt();
logger.trace("Received Data Frame Length of {} for {}", dataFrameLength, peerDescription);
}
return new ContentClaimTriple(contentClaim, claimOffset, claimLength);
}
@Override
public void run() {
try {
while (running) {
Log.v("SERVER", "Ready to accept connections on port: " + PORT);
socket = serverSocket.accept();
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream = new DataOutputStream(socket.getOutputStream());
while (running) {
Log.v("", "ready");
byte[] buffer = new byte[24000];
int c = 0;
int i = 0;
while ((c = dataInputStream.read()) != 16 && c != -1) {
// Log.v("", "read " + c);
buffer[i++] = (byte) c;
}
// connessione caduta
if (c == -1) {
Log.v("SERVER", "ServerSocket on PORT " + PORT + ": offline");
notifyDisconnect();
break;
}
byte[] buffer_trim = new byte[i];
for (int j = 0; j < i; j++)
buffer_trim[j] = buffer[j];
Map<String, String> message = MessagePacker.unpack(buffer_trim);
if (message != null) {
String s = message.get("type");
Log.v("RipperService", s);
long seqNum = Long.parseLong(message.get("index"));
if (seqNum >= curIndex) {
curIndex = seqNum;
this.notifyReceived(message);
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static int uint32FromStream(DataInputStream stream) throws IOException {
return (int) (((stream.read() & 0xFFL) << 0) | ((stream.read() & 0xFFL) << 8) | ((stream.read() & 0xFFL) << 16) | ((stream
.read() & 0xFFL) << 24));
}
public static String thisClass(byte[] classBytes) {
Map<Integer,Integer> offsetTable =
new HashMap<Integer,Integer>();
Map<Integer,String> classNameTable =
new HashMap<Integer,String>();
try {
DataInputStream data = new DataInputStream(
new ByteArrayInputStream(classBytes));
int magic = data.readInt(); // 0xcafebabe
int minorVersion = data.readShort();
int majorVersion = data.readShort();
int constant_pool_count = data.readShort();
int[] constant_pool = new int[constant_pool_count];
for(int i = 1; i < constant_pool_count; i++) {
int tag = data.read();
int tableSize;
switch(tag) {
case 1: // UTF
int length = data.readShort();
char[] bytes = new char[length];
for(int k = 0; k < bytes.length; k++)
bytes[k] = (char)data.read();
String className = new String(bytes);
classNameTable.put(i, className);
break;
case 5: // LONG
case 6: // DOUBLE
data.readLong(); // discard 8 bytes
i++; // Special skip necessary
break;
case 7: // CLASS
int offset = data.readShort();
offsetTable.put(i, offset);
break;
case 8: // STRING
data.readShort(); // discard 2 bytes
break;
case 3: // INTEGER
case 4: // FLOAT
case 9: // FIELD_REF
case 10: // METHOD_REF
case 11: // INTERFACE_METHOD_REF
case 12: // NAME_AND_TYPE
data.readInt(); // discard 4 bytes;
break;
default:
throw new RuntimeException("Bad tag " + tag);
}
}
short access_flags = data.readShort();
int this_class = data.readShort();
int super_class = data.readShort();
return classNameTable.get(
offsetTable.get(this_class)).replace('/', '.');
} catch(Exception e) {
throw new RuntimeException(e);
}
}