下面列出了java.io.DataInputStream#skip ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void open(Configuration parameters) throws Exception {
super.open(parameters);
int subtaskIdx = getRuntimeContext().getIndexOfThisSubtask();
File partitionFile = allPartitions.get(subtaskIdx);
fileLength = partitionFile.length();
waitForFailurePos = fileLength * 3 / 4;
din = new DataInputStream(
new BufferedInputStream(
new FileInputStream(partitionFile)));
long toSkip = position;
while (toSkip > 0L) {
toSkip -= din.skip(toSkip);
}
}
@Override
public void open(Configuration parameters) throws Exception {
super.open(parameters);
int subtaskIdx = getRuntimeContext().getIndexOfThisSubtask();
File partitionFile = allPartitions.get(subtaskIdx);
fileLength = partitionFile.length();
waitForFailurePos = fileLength * 3 / 4;
din = new DataInputStream(
new BufferedInputStream(
new FileInputStream(partitionFile)));
long toSkip = position;
while (toSkip > 0L) {
toSkip -= din.skip(toSkip);
}
}
/** Create a sequence from an InputStream.
* This is the counterpart of {@link MidiSystem#getSequence(InputStream)}
* for MUS format.
*
* @param is MUS data (this method does not try to auto-detect the format.)
*/
public static Sequence getSequence(InputStream is)
throws IOException, InvalidMidiDataException {
DataInputStream dis = new DataInputStream(is);
dis.skip(6);
int rus = dis.readUnsignedShort();
short scoreStart = Swap.SHORT((char) rus);
dis.skip(scoreStart - 8);
Sequence sequence = new Sequence(Sequence.SMPTE_30, 14, 1);
Track track = sequence.getTracks()[0];
int[] chanVelocity = new int[16];
Arrays.fill(chanVelocity, 100);
EventGroup eg;
long tick = 0;
while ((eg = nextEventGroup(dis, chanVelocity)) != null) {
tick = eg.appendTo(track, tick);
}
MetaMessage endOfSequence = new MetaMessage();
endOfSequence.setMessage(47, new byte[] {0}, 1);
track.add(new MidiEvent(endOfSequence, tick));
return sequence;
}
public static DataBlockHeader fromStream(InputStream stream) throws IOException {
CountingInputStream countingStream = new CountingInputStream(stream);
DataInputStream dis = new DataInputStream(countingStream);
int magic = dis.readInt();
if (magic != MAGIC_WORD) {
throw new IOException("Data block header magic word not match. read: " + magic + " expected: " + MAGIC_WORD);
}
long headerLen = dis.readLong();
long blockLen = dis.readLong();
long firstEntryId = dis.readLong();
long toSkip = headerLen - countingStream.getCount();
if (dis.skip(toSkip) != toSkip) {
throw new EOFException("Header was too small");
}
return new DataBlockHeaderImpl(headerLen, blockLen, firstEntryId);
}
private void readVarLengthData(DataInputStream dis, HollowObjectSchema unfilteredSchema) throws IOException {
int filteredFieldIdx = 0;
for(int i=0;i<unfilteredSchema.numFields();i++) {
long numBytesInVarLengthData = VarInt.readVLong(dis);
if(schema.getPosition(unfilteredSchema.getFieldName(i)) != -1) {
if(numBytesInVarLengthData != 0) {
varLengthData[filteredFieldIdx] = new SegmentedByteArray(memoryRecycler);
varLengthData[filteredFieldIdx].readFrom(dis, numBytesInVarLengthData);
}
filteredFieldIdx++;
} else {
while(numBytesInVarLengthData > 0) {
numBytesInVarLengthData -= dis.skip(numBytesInVarLengthData);
}
}
}
}
private int readMethodsAndConstructors(
DataInputStream stream,
int numberOfMethodsAndConstructors,
RealClassConstant<?>[] constants,
List<ReferenceIndex> methodIndices) throws IOException {
int numberOfConstructors = 0;
for (int i = 0; i < numberOfMethodsAndConstructors; i++) {
stream.skip(2);
ReferenceIndex referenceIndex = readReferenceIndex(stream);
methodIndices.add(referenceIndex);
String constant = (String) constants[referenceIndex.getValue1() - 1].getValue();
if (constant.contains("<init>")) {
numberOfConstructors++;
}
skipAttributes(stream);
}
return numberOfConstructors;
}
protected String readString(DataInputStream input,int pos,int[] reads) throws IOException {
int read=reads[0];
input.skip(pos-read);
read=pos;
output.reset();
while(true) {
int c1 = input.read();
int c2 = input.read();
read+=2;
if(c1==0 && c2==0) {
break;
} else {
output.write(c1);
output.write(c2);
}
}
reads[0]=read;
return new String(output.toByteArray(),encoding);
}
public static Avp decodeAvp(byte[] in_b ) throws IOException, AvpDataException {
DataInputStream in = new DataInputStream(new ByteArrayInputStream(in_b));
int code = in.readInt();
int tmp = in.readInt();
int counter = 0;
int flags = (tmp >> 24) & 0xFF;
int length = tmp & 0xFFFFFF;
if (length < 0 || counter + length > in_b.length) {
throw new AvpDataException("Not enough data in buffer!");
}
long vendor = 0;
boolean hasVendor = false;
if ((flags & 0x80) != 0) {
vendor = in.readInt();
hasVendor = true;
}
// Determine body L = length - 4(code) -1(flags) -3(length) [-4(vendor)]
byte[] rawData = new byte[length - (8 + (hasVendor ? 4 : 0))];
in.read(rawData);
// skip remaining.
// TODO: Do we need to padd everything? Or on send stack should properly fill byte[] ... ?
if (length % 4 != 0) {
for (int i; length % 4 != 0; length += i) {
i = (int) in.skip((4 - length % 4));
}
}
AvpImpl avp = new AvpImpl(code, (short) flags, (int) vendor, rawData);
return avp;
}
private RealClassField[] readFields(DataInputStream stream) throws IOException {
RealClassField[] fields = new RealClassField[stream.readUnsignedShort()];
for (int i = 0; i < fields.length; i++) {
fields[i] = new RealClassField(stream.readUnsignedShort());
stream.skip(4);
skipAttributes(stream);
}
return fields;
}
private RealClassField[] readFields(DataInputStream stream) throws IOException {
RealClassField[] fields = new RealClassField[stream.readUnsignedShort()];
for (int i = 0; i < fields.length; i++) {
fields[i] = new RealClassField(stream.readUnsignedShort());
stream.skip(4);
skipAttributes(stream);
}
return fields;
}
public HeartbeatBody(DataInputStream buffer, long size) throws IOException
{
if(size > 0)
{
//allow other implementations to have a payload, but ignore it:
buffer.skip(size);
}
}
public static void discardFrom(DataInputStream dis) throws IOException {
long numLongs = VarInt.readVLong(dis);
long bytesToSkip = numLongs * 8;
while(bytesToSkip > 0) {
bytesToSkip -= dis.skip(bytesToSkip);
}
}
private void skipForwardsCompatibilityBytes(DataInputStream is) throws IOException {
int bytesToSkip = VarInt.readVInt(is);
while(bytesToSkip > 0) {
int skippedBytes = (int)is.skip(bytesToSkip);
if(skippedBytes < 0)
throw new EOFException();
bytesToSkip -= skippedBytes;
}
}
@Override
public void read(DataInputStream f) throws IOException {
name=DoomIO.readNullTerminatedString(f,SAVESTRINGSIZE);
vcheck=DoomIO.readNullTerminatedString(f,VERSIONSIZE);
String vcheckb= ("version "+VERSION);
// no more unpacking, and report it.
if (wrongversion = !(vcheckb.equalsIgnoreCase(vcheck))) return;
gameskill = f.readByte();
gameepisode = f.readByte();
gamemap = f.readByte();
playeringame=new boolean[MAXPLAYERS];
for (int i=0 ; i<MAXPLAYERS ; i++)
playeringame[i] = f.readBoolean();
// load a base level (this doesn't advance the pointer?)
//G_InitNew (gameskill, gameepisode, gamemap);
// get the times
int a = f.readUnsignedByte();
int b = f.readUnsignedByte();
int c = f.readUnsignedByte();
// Quite anomalous, leveltime is stored as a BIG ENDIAN, 24-bit unsigned integer :-S
leveltime = (a<<16) + (b<<8) + c;
// Mark this position...
//long mark=f.getFilePointer();
//f.seek(f.length()-1);
//if (f.readByte() != 0x1d) properend=false; else
// properend=true;
//f.seek(mark);
long available=f.available();
f.skip(available-1);
if (f.readByte() != 0x1d) properend=false; else
properend=true;
// We've loaded whatever consistutes "header" info, the rest must be unpacked by proper
// methods in the game engine itself.
}
public MidiSequence getSequence(InputStream stream)throws MidiFileException, IOException{
DataInputStream in = new DataInputStream(stream);
if (in.readInt() != HEADER_MAGIC){
throw new MidiFileException("not a MIDI file: wrong header magic");
}
int headerLength = in.readInt();
if (headerLength < HEADER_LENGTH){
throw new MidiFileException("corrupt MIDI file: wrong header length");
}
int type = in.readShort();
if (type < 0 || type > 2){
throw new MidiFileException("corrupt MIDI file: illegal type");
}
if (type == 2){
throw new MidiFileException("this implementation doesn't support type 2 MIDI files");
}
int trackCount = in.readShort();
if (trackCount <= 0){
throw new MidiFileException("corrupt MIDI file: number of tracks must be positive");
}
if (type == 0 && trackCount != 1){
throw new MidiFileException("corrupt MIDI file: type 0 files must contain exactely one track");
}
float divisionType = -1.0F;
int resolution = -1;
int division = in.readUnsignedShort();
if ((division & 0x8000) != 0){
int frameType = -((division >>> 8) & 0xFF);
if(frameType == 24){
divisionType = MidiSequence.SMPTE_24;
}else if(frameType == 25){
divisionType = MidiSequence.SMPTE_25;
}else if(frameType == 29){
divisionType = MidiSequence.SMPTE_30DROP;
}else if(frameType == 30){
divisionType = MidiSequence.SMPTE_30;
}else{
throw new MidiFileException("corrupt MIDI file: illegal frame division type");
}
resolution = division & 0xff;
}else{
divisionType = MidiSequence.PPQ;
resolution = division & 0x7fff;
}
in.skip(headerLength - HEADER_LENGTH);
MidiSequence sequence = new MidiSequence(divisionType,resolution);
for (int i = 0; i < trackCount; i++){
MidiTrack track = new MidiTrack();
sequence.addTrack(track);
readTrack(in, track);
}
in.close();
return sequence;
}
private void skipThisClass(DataInputStream stream) throws IOException {
stream.skip(2);
}
public void readLayout(DataInputStream dis) throws IOException {
if (dis.readInt() != LAYOUT_SIGNATURE) {
throw new IOException("file signature not found");
}
int version = dis.readInt();
if (version != LAYOUT_VERSION && version != LAYOUT_OLD_VERSION) {
throw new IOException("incompatible file version");
}
while (true) {
int block = dis.readInt();
int length = dis.readInt();
if (block == LAYOUT_EOF) {
break;
}
int count;
switch (block) {
case LAYOUT_KEYS:
count = dis.readInt();
int hash;
boolean found;
for (int i = 0; i < count; i++) {
hash = dis.readInt();
found = false;
for (int key = 0; key < keypad.length; key++) {
if (keypad[key].hashCode() == hash) {
if (version == LAYOUT_VERSION) {
keypad[key].setVisible(dis.readBoolean());
}
snapOrigins[key] = dis.readInt();
snapModes[key] = dis.readInt();
snapOffsets[key].x = dis.readFloat();
snapOffsets[key].y = dis.readFloat();
found = true;
break;
}
}
if (!found) {
dis.skip(16);
}
}
break;
case LAYOUT_SCALES:
count = dis.readInt();
if (count == keyScales.length) {
for (int i = 0; i < count; i++) {
keyScales[i] = dis.readFloat();
}
} else {
dis.skip(count * 4);
}
break;
case LAYOUT_COLORS:
count = dis.readInt();
if (count == colors.length) {
for (int i = 0; i < count; i++) {
colors[i] = dis.readInt();
}
} else {
dis.skip(count * 4);
}
break;
default:
dis.skip(length);
break;
}
}
}
public static void discardEncodedDeltaOrdinals(DataInputStream dis) throws IOException {
long numBytesToSkip = VarInt.readVLong(dis);
while(numBytesToSkip > 0) {
numBytesToSkip -= dis.skip(numBytesToSkip);
}
}
@SuppressWarnings("unused")
public Geometry(InputStream inputStream, long oidToUse) throws IOException {
DataInputStream dataInputStream = new DataInputStream(inputStream);
String protocol = dataInputStream.readUTF();
byte version = dataInputStream.readByte();
dataInputStream.readFloat();
dataInputStream.readFloat();
dataInputStream.readFloat();
dataInputStream.readFloat();
dataInputStream.readFloat();
dataInputStream.readFloat();
int nrObjects = dataInputStream.readInt();
int offset = 2;
for (int i=0; i<nrObjects; i++) {
String type = dataInputStream.readUTF();
long oid = dataInputStream.readLong();
byte geometryType = dataInputStream.readByte();
offset += 3 + type.getBytes("UTF-8").length;
int skip = 4 - (offset % 4);
if (skip != 0 && skip != 4) {
dataInputStream.skip(skip);
}
offset = 0;
for (int x=0; x<16; x++) {
dataInputStream.readFloat();
}
if (geometryType == GEOMETRY_TYPE_INSTANCE) {
dataInputStream.readLong();
} else if (geometryType == GEOMETRY_TYPE_TRIANGLES) {
dataInputStream.readLong();
minX = dataInputStream.readFloat();
minY = dataInputStream.readFloat();
minZ = dataInputStream.readFloat();
maxX = dataInputStream.readFloat();
maxY = dataInputStream.readFloat();
maxZ = dataInputStream.readFloat();
int nrIndices = dataInputStream.readInt();
IntBuffer indices = readIntBuffer(dataInputStream, nrIndices);
int nrVertices = dataInputStream.readInt();
FloatBuffer vertices = readFloatBuffer(dataInputStream, nrVertices);
int nrNormals = dataInputStream.readInt();
FloatBuffer normals = readFloatBuffer(dataInputStream, nrNormals);
int nrMaterials = dataInputStream.readInt();
FloatBuffer materials = readFloatBuffer(dataInputStream, nrMaterials * 4);
if (oidToUse == oid) {
this.indices = indices;
this.vertices = vertices;
this.normals = normals;
this.materials = materials;
}
}
}
}
private void skipThisClass(DataInputStream stream) throws IOException {
stream.skip(2);
}