Java Code Examples for java.io.RandomAccessFile#skipBytes()

The following examples show how to use java.io.RandomAccessFile#skipBytes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HeaderReader.java    From zip4j with Apache License 2.0 6 votes vote down vote up
private List<ExtraDataRecord> readExtraDataRecords(RandomAccessFile zip4jRaf, int extraFieldLength)
    throws IOException {

  if (extraFieldLength < 4) {
    if (extraFieldLength > 0) {
      zip4jRaf.skipBytes(extraFieldLength);
    }

    return null;
  }

  byte[] extraFieldBuf = new byte[extraFieldLength];
  zip4jRaf.read(extraFieldBuf);

  try {
    return parseExtraDataRecords(extraFieldBuf, extraFieldLength);
  } catch (Exception e) {
    // Ignore any errors when parsing extra data records
    return Collections.emptyList();
  }
}
 
Example 2
Source File: AuFileWriter.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {

        // throws IllegalArgumentException if not supported
        AuFileFormat auFileFormat = (AuFileFormat)getAudioFileFormat(fileType, stream);

        // first write the file without worrying about length fields
        FileOutputStream fos = new FileOutputStream( out );     // throws IOException
        BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
        int bytesWritten = writeAuFile(stream, auFileFormat, bos );
        bos.close();

        // now, if length fields were not specified, calculate them,
        // open as a random access file, write the appropriate fields,
        // close again....
        if( auFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {

            // $$kk: 10.22.99: jan: please either implement this or throw an exception!
            // $$fb: 2001-07-13: done. Fixes Bug 4479981
            RandomAccessFile raf=new RandomAccessFile(out, "rw");
            if (raf.length()<=0x7FFFFFFFl) {
                // skip AU magic and data offset field
                raf.skipBytes(8);
                raf.writeInt(bytesWritten-AuFileFormat.AU_HEADERSIZE);
                // that's all
            }
            raf.close();
        }

        return bytesWritten;
    }
 
Example 3
Source File: QFiles.java    From pacaya with Apache License 2.0 5 votes vote down vote up
public static String tail(File logFile) {
    try {
        RandomAccessFile raf = new RandomAccessFile(logFile, "r");
        byte[] bytes = new byte[500];
        raf.skipBytes((int)raf.length() - bytes.length);
        int read = raf.read(bytes);
        raf.close();
        return new String(Arrays.copyOf(bytes, read));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 4
Source File: ID3v2_3Frame.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
public void read(final RandomAccessFile file) throws IOException, InvalidTagException {
    byte b;
    long filePointer;
    final byte[] buffer = new byte[4];

    // lets scan for a non-zero byte;
    do {
        filePointer = file.getFilePointer();
        b = file.readByte();
        org.farng.mp3.id3.AbstractID3v2.incrementPaddingCounter();
    } while (b == 0);
    file.seek(filePointer);
    org.farng.mp3.id3.AbstractID3v2.decrementPaddingCounter();

    // read the four character identifier
    file.read(buffer, 0, 4);
    final String identifier = new String(buffer, 0, 4);

    // is this a valid identifier?
    if (isValidID3v2FrameIdentifier(identifier) == false) {
        file.seek(file.getFilePointer() - 3);
        throw new InvalidTagException(identifier + " is not a valid ID3v2.30 frame");
    }
    filePointer = file.getFilePointer();

    // skip the 4 byte size
    file.skipBytes(4);

    // read the flag bytes
    file.read(buffer, 0, 2);
    this.tagAlterPreservation = (buffer[0] & TagConstant.MASK_V23_TAG_ALTER_PRESERVATION) != 0;
    this.fileAlterPreservation = (buffer[0] & TagConstant.MASK_V23_FILE_ALTER_PRESERVATION) != 0;
    this.readOnly = (buffer[0] & TagConstant.MASK_V23_READ_ONLY) != 0;
    this.compression = (buffer[1] & TagConstant.MASK_V23_COMPRESSION) != 0;
    this.encryption = (buffer[1] & TagConstant.MASK_V23_ENCRYPTION) != 0;
    this.groupingIdentity = (buffer[1] & TagConstant.MASK_V23_GROUPING_IDENTITY) != 0;
    file.seek(filePointer);
    this.setBody(readBody(identifier, file));
}
 
Example 5
Source File: ZipUtil.java    From GPT with Apache License 2.0 5 votes vote down vote up
/**
 * findCentralDirectory
 *
 * @param raf RandomAccessFile
 * @return CentralDirectory
 * @throws IOException,ZipException
 */
static CentralDirectory findCentralDirectory(RandomAccessFile raf) throws IOException,
        ZipException {
    long scanOffset = raf.length() - ENDHDR;
    if (scanOffset < 0) {
        throw new ZipException("File too short to be a zip file: " + raf.length());
    }

    long stopOffset = scanOffset - 0x10000 /* ".ZIP file comment"'s max length */;
    if (stopOffset < 0) {
        stopOffset = 0;
    }

    int endSig = Integer.reverseBytes(ENDSIG);
    while (true) {
        raf.seek(scanOffset);
        if (raf.readInt() == endSig) {
            break;
        }

        scanOffset--;
        if (scanOffset < stopOffset) {
            throw new ZipException("End Of Central Directory signature not found");
        }
    }
    // Read the End Of Central Directory. ENDHDR includes the signature
    // bytes, which we've already read.

    // Pull out the information we need.
    raf.skipBytes(2); // diskNumber
    raf.skipBytes(2); // diskWithCentralDir
    raf.skipBytes(2); // numEntries
    raf.skipBytes(2); // totalNumEntries
    CentralDirectory dir = new CentralDirectory();
    dir.size = Integer.reverseBytes(raf.readInt()) & 0xFFFFFFFFL;
    dir.offset = Integer.reverseBytes(raf.readInt()) & 0xFFFFFFFFL;
    return dir;
}
 
Example 6
Source File: MM5DataInfo.java    From MeteoInfo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Read big header
 *
 * @param br The randomAccessFile
 * @param isSequential If is sequential
 * @return The big header
 * @throws IOException
 */
public BigHeader readBigHeader(RandomAccessFile br, boolean isSequential) throws IOException {
    BigHeader bh = new BigHeader();
    if (isSequential) {
        br.skipBytes(4);
    }
    byte[] bytes = new byte[80];
    int i, j;
    for (i = 0; i < 20; i++) {
        for (j = 0; j < 50; j++) {
            bh.bhi[j][i] = br.readInt();
        }
    }
    for (i = 0; i < 20; i++) {
        for (j = 0; j < 20; j++) {
            bh.bhr[j][i] = br.readFloat();
        }
    }
    for (i = 0; i < 20; i++) {
        for (j = 0; j < 50; j++) {
            br.read(bytes);
            bh.bhic[j][i] = new String(bytes).trim();
        }
    }
    for (i = 0; i < 20; i++) {
        for (j = 0; j < 20; j++) {
            br.read(bytes);
            bh.bhrc[j][i] = new String(bytes).trim();
        }
    }

    if (isSequential) {
        br.skipBytes(4);
    }

    return bh;
}
 
Example 7
Source File: ZipUtil.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
static CentralDirectory findCentralDirectory(RandomAccessFile raf) throws IOException,
        ZipException {
    long scanOffset = raf.length() - ENDHDR;
    if (scanOffset < 0) {
        throw new ZipException("File too short to be a zip file: " + raf.length());
    }

    long stopOffset = scanOffset - 0x10000 /* ".ZIP file comment"'s max length */;
    if (stopOffset < 0) {
        stopOffset = 0;
    }

    int endSig = Integer.reverseBytes(ENDSIG);
    while (true) {
        raf.seek(scanOffset);
        if (raf.readInt() == endSig) {
            break;
        }

        scanOffset--;
        if (scanOffset < stopOffset) {
            throw new ZipException("End Of Central Directory signature not found");
        }
    }
    // Read the End Of Central Directory. ENDHDR includes the signature
    // bytes,
    // which we've already read.

    // Pull out the information we need.
    raf.skipBytes(2); // diskNumber
    raf.skipBytes(2); // diskWithCentralDir
    raf.skipBytes(2); // numEntries
    raf.skipBytes(2); // totalNumEntries
    CentralDirectory dir = new CentralDirectory();
    dir.size = Integer.reverseBytes(raf.readInt()) & 0xFFFFFFFFL;
    dir.offset = Integer.reverseBytes(raf.readInt()) & 0xFFFFFFFFL;
    return dir;
}
 
Example 8
Source File: AuFileWriter.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {

        // throws IllegalArgumentException if not supported
        AuFileFormat auFileFormat = (AuFileFormat)getAudioFileFormat(fileType, stream);

        // first write the file without worrying about length fields
        FileOutputStream fos = new FileOutputStream( out );     // throws IOException
        BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
        int bytesWritten = writeAuFile(stream, auFileFormat, bos );
        bos.close();

        // now, if length fields were not specified, calculate them,
        // open as a random access file, write the appropriate fields,
        // close again....
        if( auFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {

            // $$kk: 10.22.99: jan: please either implement this or throw an exception!
            // $$fb: 2001-07-13: done. Fixes Bug 4479981
            RandomAccessFile raf=new RandomAccessFile(out, "rw");
            if (raf.length()<=0x7FFFFFFFl) {
                // skip AU magic and data offset field
                raf.skipBytes(8);
                raf.writeInt(bytesWritten-AuFileFormat.AU_HEADERSIZE);
                // that's all
            }
            raf.close();
        }

        return bytesWritten;
    }
 
Example 9
Source File: AuFileWriter.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {

        // throws IllegalArgumentException if not supported
        AuFileFormat auFileFormat = (AuFileFormat)getAudioFileFormat(fileType, stream);

        // first write the file without worrying about length fields
        FileOutputStream fos = new FileOutputStream( out );     // throws IOException
        BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
        int bytesWritten = writeAuFile(stream, auFileFormat, bos );
        bos.close();

        // now, if length fields were not specified, calculate them,
        // open as a random access file, write the appropriate fields,
        // close again....
        if( auFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {

            // $$kk: 10.22.99: jan: please either implement this or throw an exception!
            // $$fb: 2001-07-13: done. Fixes Bug 4479981
            RandomAccessFile raf=new RandomAccessFile(out, "rw");
            if (raf.length()<=0x7FFFFFFFl) {
                // skip AU magic and data offset field
                raf.skipBytes(8);
                raf.writeInt(bytesWritten-AuFileFormat.AU_HEADERSIZE);
                // that's all
            }
            raf.close();
        }

        return bytesWritten;
    }
 
Example 10
Source File: AuFileWriter.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {

        // throws IllegalArgumentException if not supported
        AuFileFormat auFileFormat = (AuFileFormat)getAudioFileFormat(fileType, stream);

        // first write the file without worrying about length fields
        FileOutputStream fos = new FileOutputStream( out );     // throws IOException
        BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
        int bytesWritten = writeAuFile(stream, auFileFormat, bos );
        bos.close();

        // now, if length fields were not specified, calculate them,
        // open as a random access file, write the appropriate fields,
        // close again....
        if( auFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {

            // $$kk: 10.22.99: jan: please either implement this or throw an exception!
            // $$fb: 2001-07-13: done. Fixes Bug 4479981
            RandomAccessFile raf=new RandomAccessFile(out, "rw");
            if (raf.length()<=0x7FFFFFFFl) {
                // skip AU magic and data offset field
                raf.skipBytes(8);
                raf.writeInt(bytesWritten-AuFileFormat.AU_HEADERSIZE);
                // that's all
            }
            raf.close();
        }

        return bytesWritten;
    }
 
Example 11
Source File: WaveFileWriter.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {

        // throws IllegalArgumentException if not supported
        WaveFileFormat waveFileFormat = (WaveFileFormat)getAudioFileFormat(fileType, stream);

        // first write the file without worrying about length fields
        FileOutputStream fos = new FileOutputStream( out );     // throws IOException
        BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
        int bytesWritten = writeWaveFile(stream, waveFileFormat, bos );
        bos.close();

        // now, if length fields were not specified, calculate them,
        // open as a random access file, write the appropriate fields,
        // close again....
        if( waveFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {

            int dataLength=bytesWritten-waveFileFormat.getHeaderSize();
            int riffLength=dataLength + waveFileFormat.getHeaderSize() - 8;

            RandomAccessFile raf=new RandomAccessFile(out, "rw");
            // skip RIFF magic
            raf.skipBytes(4);
            raf.writeInt(big2little( riffLength ));
            // skip WAVE magic, fmt_ magic, fmt_ length, fmt_ chunk, data magic
            raf.skipBytes(4+4+4+WaveFileFormat.getFmtChunkSize(waveFileFormat.getWaveType())+4);
            raf.writeInt(big2little( dataLength ));
            // that's all
            raf.close();
        }

        return bytesWritten;
    }
 
Example 12
Source File: WaveFileWriter.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {

        // throws IllegalArgumentException if not supported
        WaveFileFormat waveFileFormat = (WaveFileFormat)getAudioFileFormat(fileType, stream);

        // first write the file without worrying about length fields
        FileOutputStream fos = new FileOutputStream( out );     // throws IOException
        BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
        int bytesWritten = writeWaveFile(stream, waveFileFormat, bos );
        bos.close();

        // now, if length fields were not specified, calculate them,
        // open as a random access file, write the appropriate fields,
        // close again....
        if( waveFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {

            int dataLength=bytesWritten-waveFileFormat.getHeaderSize();
            int riffLength=dataLength + waveFileFormat.getHeaderSize() - 8;

            RandomAccessFile raf=new RandomAccessFile(out, "rw");
            // skip RIFF magic
            raf.skipBytes(4);
            raf.writeInt(big2little( riffLength ));
            // skip WAVE magic, fmt_ magic, fmt_ length, fmt_ chunk, data magic
            raf.skipBytes(4+4+4+WaveFileFormat.getFmtChunkSize(waveFileFormat.getWaveType())+4);
            raf.writeInt(big2little( dataLength ));
            // that's all
            raf.close();
        }

        return bytesWritten;
    }
 
Example 13
Source File: SMF.java    From jmg with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Reads a MIDI track without doing anything to the data
 */
private void skipATrack( RandomAccessFile dos) throws IOException{
               if(VERBOSE) System.out.println("Skipping the tempo track . . .");
	dos.readInt();
	dos.skipBytes(dos.readInt());
}
 
Example 14
Source File: AiffFileWriter.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {

        // throws IllegalArgumentException if not supported
        AiffFileFormat aiffFileFormat = (AiffFileFormat)getAudioFileFormat(fileType, stream);

        // first write the file without worrying about length fields
        FileOutputStream fos = new FileOutputStream( out );     // throws IOException
        BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
        int bytesWritten = writeAiffFile(stream, aiffFileFormat, bos );
        bos.close();

        // now, if length fields were not specified, calculate them,
        // open as a random access file, write the appropriate fields,
        // close again....
        if( aiffFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {

            // $$kk: 10.22.99: jan: please either implement this or throw an exception!
            // $$fb: 2001-07-13: done. Fixes Bug 4479981
            int ssndBlockSize           = (aiffFileFormat.getFormat().getChannels() * aiffFileFormat.getFormat().getSampleSizeInBits());

            int aiffLength=bytesWritten;
            int ssndChunkSize=aiffLength-aiffFileFormat.getHeaderSize()+16;
            long dataSize=ssndChunkSize-16;
            int numFrames=(int) (dataSize*8/ssndBlockSize);

            RandomAccessFile raf=new RandomAccessFile(out, "rw");
            // skip FORM magic
            raf.skipBytes(4);
            raf.writeInt(aiffLength-8);
            // skip aiff2 magic, fver chunk, comm magic, comm size, channel count,
            raf.skipBytes(4+aiffFileFormat.getFverChunkSize()+4+4+2);
            // write frame count
            raf.writeInt(numFrames);
            // skip sample size, samplerate, SSND magic
            raf.skipBytes(2+10+4);
            raf.writeInt(ssndChunkSize-8);
            // that's all
            raf.close();
        }

        return bytesWritten;
    }
 
Example 15
Source File: AiffFileWriter.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {

        // throws IllegalArgumentException if not supported
        AiffFileFormat aiffFileFormat = (AiffFileFormat)getAudioFileFormat(fileType, stream);

        // first write the file without worrying about length fields
        FileOutputStream fos = new FileOutputStream( out );     // throws IOException
        BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
        int bytesWritten = writeAiffFile(stream, aiffFileFormat, bos );
        bos.close();

        // now, if length fields were not specified, calculate them,
        // open as a random access file, write the appropriate fields,
        // close again....
        if( aiffFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {

            // $$kk: 10.22.99: jan: please either implement this or throw an exception!
            // $$fb: 2001-07-13: done. Fixes Bug 4479981
            int ssndBlockSize           = (aiffFileFormat.getFormat().getChannels() * aiffFileFormat.getFormat().getSampleSizeInBits());

            int aiffLength=bytesWritten;
            int ssndChunkSize=aiffLength-aiffFileFormat.getHeaderSize()+16;
            long dataSize=ssndChunkSize-16;
            int numFrames=(int) (dataSize*8/ssndBlockSize);

            RandomAccessFile raf=new RandomAccessFile(out, "rw");
            // skip FORM magic
            raf.skipBytes(4);
            raf.writeInt(aiffLength-8);
            // skip aiff2 magic, fver chunk, comm magic, comm size, channel count,
            raf.skipBytes(4+aiffFileFormat.getFverChunkSize()+4+4+2);
            // write frame count
            raf.writeInt(numFrames);
            // skip sample size, samplerate, SSND magic
            raf.skipBytes(2+10+4);
            raf.writeInt(ssndChunkSize-8);
            // that's all
            raf.close();
        }

        return bytesWritten;
    }
 
Example 16
Source File: GrADSDataInfo.java    From MeteoInfo with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public GridData getGridData_LevelLat(int lonIdx, String varName, int timeIdx) {
    try {
        int varIdx = this.getVariableIndex(varName);
        int xNum, yNum;
        xNum = YNum;
        yNum = VARDEF.getVars().get(varIdx).getLevelNum();
        double[][] gridData = new double[yNum][xNum];

        String filePath = DSET;
        int tIdx = timeIdx;
        if (OPTIONS.template) {
            Object[] result = getFilePath_Template(timeIdx);
            filePath = (String) result[0];
            tIdx = (int) result[1];
        }

        RandomAccessFile br = new RandomAccessFile(filePath, "r");
        int i, j, lNum;

        br.seek(FILEHEADER + tIdx * RecLenPerTime);

        for (i = 0; i < varIdx; i++) {
            lNum = VARDEF.getVars().get(i).getLevelNum();
            if (lNum == 0) {
                lNum = 1;
            }
            br.seek(br.getFilePointer() + lNum * RecordLen);
        }

        if (br.getFilePointer() >= br.length()) {
            System.out.println("Erro");
        }

        for (i = 0; i < yNum; i++) //Levels
        {
            if (OPTIONS.sequential) {
                br.seek(br.getFilePointer() + 4);
            }

            byte[] aBytes = new byte[4];
            for (j = 0; j < YNum; j++) {
                br.skipBytes(lonIdx * 4);
                br.read(aBytes);
                gridData[i][j] = DataConvert.bytes2Float(aBytes, _byteOrder);
                br.skipBytes((XNum - lonIdx - 1) * 4);
            }
        }
        br.close();

        GridData aGridData = new GridData();
        aGridData.data = gridData;
        aGridData.missingValue = this.getMissingValue();
        aGridData.xArray = Y;
        double[] levels = new double[VARDEF.getVars().get(varIdx).getLevelNum()];
        for (i = 0; i < levels.length; i++) {
            levels[i] = ZDEF.ZLevels[i];
        }
        aGridData.yArray = levels;

        return aGridData;
    } catch (IOException ex) {
        Logger.getLogger(GrADSDataInfo.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
}
 
Example 17
Source File: AiffFileWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {
    Objects.requireNonNull(stream);
    Objects.requireNonNull(fileType);
    Objects.requireNonNull(out);

    // throws IllegalArgumentException if not supported
    AiffFileFormat aiffFileFormat = (AiffFileFormat)getAudioFileFormat(fileType, stream);

    // first write the file without worrying about length fields
    FileOutputStream fos = new FileOutputStream( out );     // throws IOException
    BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
    int bytesWritten = writeAiffFile(stream, aiffFileFormat, bos );
    bos.close();

    // now, if length fields were not specified, calculate them,
    // open as a random access file, write the appropriate fields,
    // close again....
    if( aiffFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {

        // $$kk: 10.22.99: jan: please either implement this or throw an exception!
        // $$fb: 2001-07-13: done. Fixes Bug 4479981
        int channels = aiffFileFormat.getFormat().getChannels();
        int sampleSize = aiffFileFormat.getFormat().getSampleSizeInBits();
        int ssndBlockSize = channels * ((sampleSize + 7) / 8);

        int aiffLength=bytesWritten;
        int ssndChunkSize=aiffLength-aiffFileFormat.getHeaderSize()+16;
        long dataSize=ssndChunkSize-16;
        //TODO possibly incorrect round
        int numFrames = (int) (dataSize / ssndBlockSize);

        RandomAccessFile raf=new RandomAccessFile(out, "rw");
        // skip FORM magic
        raf.skipBytes(4);
        raf.writeInt(aiffLength-8);
        // skip aiff2 magic, fver chunk, comm magic, comm size, channel count,
        raf.skipBytes(4+aiffFileFormat.getFverChunkSize()+4+4+2);
        // write frame count
        raf.writeInt(numFrames);
        // skip sample size, samplerate, SSND magic
        raf.skipBytes(2+10+4);
        raf.writeInt(ssndChunkSize-8);
        // that's all
        raf.close();
    }

    return bytesWritten;
}
 
Example 18
Source File: MM5IMDataInfo.java    From MeteoInfo with GNU Lesser General Public License v3.0 4 votes vote down vote up
private DataHead readDataHead(RandomAccessFile br) throws IOException {
    DataHead dh = new DataHead();
    byte[] bytes;

    //Record 1: 4 + 8 bytes
    br.skipBytes(4);
    bytes = new byte[4];
    br.read(bytes);
    dh.iversion = DataConvert.bytes2Int(bytes, _byteOrder);
    br.skipBytes(4);

    //Record 2: 124 + 8 bytes       
    br.skipBytes(4);
    bytes = new byte[24];
    br.read(bytes);
    dh.hdate = new String(bytes).trim();
    bytes = new byte[4];
    br.read(bytes);
    dh.xfcst = DataConvert.bytes2Float(bytes, _byteOrder);
    bytes = new byte[9];
    br.read(bytes);
    dh.field = new String(bytes).trim();
    dh.field = dh.field.split("\\s+")[0];
    bytes = new byte[25];
    br.read(bytes);
    dh.units = new String(bytes).trim();
    bytes = new byte[46];
    br.read(bytes);
    dh.desc = new String(bytes).trim();
    bytes = new byte[4];
    br.read(bytes);
    dh.level = DataConvert.bytes2Float(bytes, _byteOrder);
    br.read(bytes);
    dh.idim = DataConvert.bytes2Int(bytes, _byteOrder);
    br.read(bytes);
    dh.jdim = DataConvert.bytes2Int(bytes, _byteOrder);
    br.read(bytes);
    dh.llflag = DataConvert.bytes2Int(bytes, _byteOrder);
    br.skipBytes(4);

    //Record 3: 16 + 8 bytes
    br.skipBytes(4);
    if (dh.llflag == 0) {
        br.read(bytes);
        dh.startlat = DataConvert.bytes2Float(bytes, _byteOrder);
        br.read(bytes);
        dh.startlon = DataConvert.bytes2Float(bytes, _byteOrder);
        br.read(bytes);
        dh.deltalat = DataConvert.bytes2Float(bytes, _byteOrder);
        br.read(bytes);
        dh.deltalon = DataConvert.bytes2Float(bytes, _byteOrder);
    }
    br.skipBytes(4);

    return dh;
}
 
Example 19
Source File: HYSPLITPartDataInfo.java    From MeteoInfo with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Read array data of the variable
 *
 * @param varName Variable name
 * @param origin The origin array
 * @param size The size array
 * @param stride The stride array
 * @return Array data
 */
@Override
public Array read(String varName, int[] origin, int[] size, int[] stride) {
    try {
        Variable var = this.getVariable(varName);
        int timeIdx = (int)var.findAttribute("time_index").getNumericValue();
        int particleNum = _parameters.get(timeIdx).get(0);
        int pollutantNum = _parameters.get(timeIdx).get(1);
        int pos = _parameters.get(timeIdx).get(2);
        Array r = Array.factory(var.getDataType(), new int[]{particleNum});

        RandomAccessFile br = new RandomAccessFile(this.getFileName(), "r");
        int i, j;
        float lon, lat, alt;

        br.seek(pos);
        br.skipBytes(28);
        for (i = 0; i < particleNum; i++) {
            br.skipBytes(8);
            for (j = 0; j < pollutantNum; j++) {
                br.skipBytes(4);
            }
            br.skipBytes(8);
            lat = br.readFloat();
            lon = br.readFloat();
            alt = br.readFloat();

            if (varName.startsWith("lon"))
                r.setFloat(i, lon);
            else if (varName.startsWith("lat"))
                r.setFloat(i, lat);
            else
                r.setFloat(i, alt);

            br.skipBytes(40);
        }

        return r;
    } catch (IOException e) {
        return null;
    }
}
 
Example 20
Source File: HYSPLITPartDataInfo.java    From MeteoInfo with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void readDataInfo(String fileName) {
    try {
        this.setFileName(fileName);
        RandomAccessFile br = new RandomAccessFile(fileName, "r");
        int year, month, day, hour;
        List<LocalDateTime> times = new ArrayList<>();
        _parameters = new ArrayList<List<Integer>>();
        List<Variable> variables = new ArrayList<>();

        this.addAttribute(new Attribute("data_format", "HYSPLIT Particles"));

        int i = 0;
        String[] varNames = new String[]{"lat", "lon", "height"};
        while (br.getFilePointer() < br.length() - 28) {
            //Read head
            int pos = (int) br.getFilePointer();
            br.skipBytes(4);
            int particleNum = br.readInt();
            int pollutantNum = br.readInt();
            year = br.readInt();
            month = br.readInt();
            day = br.readInt();
            hour = br.readInt();                
            if (year < 50) {
                year = 2000 + year;
            } else {
                year = 1900 + year;
            }
            times.add(LocalDateTime.of(year, month, day, hour, 0, 0));
            List<Integer> data = new ArrayList<Integer>();
            data.add(particleNum);
            data.add(pollutantNum);
            data.add(pos);
            _parameters.add(data);

            Dimension dim = new Dimension();
            dim.setName(String.format("pnum_t%d", i));
            dim.setValues(new float[particleNum]);
            this.addDimension(dim);

            for (String varName : varNames) {
                Variable var = new Variable();
                var.setStation(true);
                var.setName(String.format("%s_t%d", varName, i));
                var.setDimension(dim);
                var.setDataType(DataType.FLOAT);
                var.addAttribute(new Attribute("time_index", i));
                if (varName == "lon")
                    var.addAttribute("long_name", "longitude");
                else if (varName == "lat")
                    var.addAttribute("long_name", "latitude");
                else
                    var.addAttribute("long_name", "height");
                variables.add(var);
            }

            //Skip data
            int len = (8 + pollutantNum * 4 + 60) * particleNum + 4;
            br.skipBytes(len);

            i ++;
        }

        br.close();

        List<Double> values = new ArrayList<Double>();
        for (LocalDateTime t : times) {
            values.add(JDateUtil.toOADate(t));
        }
        Dimension tDim = new Dimension(DimensionType.T);
        tDim.setValues(values);
        this.setTimeDimension(tDim);
        this.addDimension(tDim);

        this.setVariables(variables);
        
    } catch (IOException ex) {
        Logger.getLogger(HYSPLITPartDataInfo.class.getName()).log(Level.SEVERE, null, ex);
    }
}