private void setOffset(int x, int z, int offset) throws IOException { offsets[x + (z << 5)] = offset; file.seek((x + (z << 5)) << 2); file.writeInt(offset); }
/** * Writes a big-endian 32-bit float to this file, starting at the current file pointer. * The bytes are those returned by {@link Float#floatToIntBits(float)}, meaning a canonical NaN * is used. * * @param val * the float to write to this file. * @throws IOException * if an I/O error occurs while writing to this file. * @see #readFloat() */ public final void writeFloat(float val) throws IOException { writeInt(Float.floatToIntBits(val)); }
private void setTimestamp(int x, int z, int value) throws IOException { chunkTimestamps[x + (z << 5)] = value; file.seek(SECTOR_BYTES + ((x + (z << 5)) << 2)); file.writeInt(value); }
private void writeFileHeader(RandomAccessFile randomAccessFile) throws IOException { // Write the start of the header as big endian data. randomAccessFile.writeInt(WavUtil.RIFF_FOURCC); randomAccessFile.writeInt(-1); randomAccessFile.writeInt(WavUtil.WAVE_FOURCC); randomAccessFile.writeInt(WavUtil.FMT_FOURCC); // Write the rest of the header as little endian data. scratchByteBuffer.clear(); scratchByteBuffer.putInt(16); scratchByteBuffer.putShort((short) WavUtil.getTypeForEncoding(encoding)); scratchByteBuffer.putShort((short) channelCount); scratchByteBuffer.putInt(sampleRateHz); int bytesPerSample = Util.getPcmFrameSize(encoding, channelCount); scratchByteBuffer.putInt(bytesPerSample * sampleRateHz); scratchByteBuffer.putShort((short) bytesPerSample); scratchByteBuffer.putShort((short) (8 * bytesPerSample / channelCount)); randomAccessFile.write(scratchBuffer, 0, scratchByteBuffer.position()); // Write the start of the data chunk as big endian data. randomAccessFile.writeInt(WavUtil.DATA_FOURCC); randomAccessFile.writeInt(-1); }
/** * Writes some internal data into the beginning of the specified file. */ protected void writeHeader(RandomAccessFile file, long length, int segmentSize) throws IOException { file.seek(0); file.writeUTF("GH"); file.writeLong(length); file.writeInt(segmentSize); for (int i = 0; i < header.length; i++) { file.writeInt(header[i]); } }
private void writeSector(int sectorNumber, byte[] data, int length) throws IOException { file.seek(sectorNumber * SECTOR_BYTES); file.writeInt(length + 1); // chunk length file.writeByte(VERSION_DEFLATE); // chunk version number file.write(data, 0, length); // chunk data }
@Override public Bytes writeInt(int offset, int i) { checkWrite(offset, INTEGER); try { seekToOffset(offset); randomAccessFile.writeInt(i); } catch (IOException e) { throw new RuntimeException(e); } return this; }
@Private static RandomAccessFile initializeFromFile(File file, boolean forceLegacy) throws IOException { if (!file.exists()) { // Use a temp file so we don't leave a partially-initialized file. File tempFile = new File(file.getPath() + ".tmp"); RandomAccessFile raf = open(tempFile); try { raf.setLength(INITIAL_LENGTH); raf.seek(0); if (forceLegacy) { raf.writeInt(INITIAL_LENGTH); } else { raf.writeInt(VERSIONED_HEADER); raf.writeLong(INITIAL_LENGTH); } } finally { raf.close(); } // A rename is atomic. if (!tempFile.renameTo(file)) { throw new IOException("Rename failed!"); } } return open(file); }
private synchronized void writeChunkMetaData(int lastChunkId, TreeSet<Long> removedPages) { try { chunkMetaData.setLength(0); chunkMetaData.seek(0); chunkMetaData.writeInt(lastChunkId); chunkMetaData.writeInt(removedPages.size()); for (long pos : removedPages) { chunkMetaData.writeLong(pos); } chunkMetaData.writeInt(hashCodeToHostIdMap.size()); for (String hostId : hashCodeToHostIdMap.values()) { chunkMetaData.writeUTF(hostId); } // chunkMetaData.setLength(4 + 4 + removedPages.size() * 8); chunkMetaData.getFD().sync(); } catch (IOException e) { throw panic(DataUtils.newIllegalStateException(DataUtils.ERROR_WRITING_FAILED, "Failed to writeChunkMetaData", e)); } }
@Override public void writeInt(int i) throws IOException { try { getRaf().writeInt(i); } catch (IOException ioe) { handleException(); throw ioe; } }
Writer(File file, int logFileID, long maxFileSize, long usableSpaceRefreshInterval) throws IOException { super(file, logFileID, maxFileSize, null, usableSpaceRefreshInterval, true, 0); RandomAccessFile writeFileHandle = getFileHandle(); writeFileHandle.writeInt(getVersion()); writeFileHandle.writeInt(logFileID); // checkpoint marker writeFileHandle.writeLong(0L); // timestamp placeholder writeFileHandle.writeLong(0L); getFileChannel().force(true); }
gemfFile.writeInt(VERSION); gemfFile.writeInt(TILE_SIZE); gemfFile.writeInt(sourceIndex.size()); gemfFile.writeInt(sourceIndex.get(source)); gemfFile.writeInt(source.length()); gemfFile.write(source.getBytes()); gemfFile.writeInt(ranges.size()); gemfFile.writeInt(range.zoom); gemfFile.writeInt(range.xMin); gemfFile.writeInt(range.xMax); gemfFile.writeInt(range.yMin); gemfFile.writeInt(range.yMax); gemfFile.writeInt(range.sourceIndex); gemfFile.writeLong(range.offset); indexSource.get( range.sourceIndex)).get(range.zoom).get(x).get(y).length(); gemfFile.writeInt((int)fileSize); offset += fileSize;
@Test public void testNegativeSizeInHeaderThrows() throws IOException { RandomAccessFile emptyFile = new RandomAccessFile(file, "rwd"); emptyFile.seek(0); emptyFile.writeInt(-2147483648); emptyFile.setLength(INITIAL_LENGTH); emptyFile.getChannel().force(true); emptyFile.close(); try { newQueueFile(); fail("Should have thrown about bad header length"); } catch (IOException ex) { assertThat(ex.getMessage()).isIn( Arrays.asList("File is corrupt; length stored in header (-2147483648) is invalid.", "Unable to read version 0 format. Supported versions are 1 and legacy.")); } }
@Test public void testSizeLessThanHeaderThrows() throws IOException { RandomAccessFile emptyFile = new RandomAccessFile(file, "rwd"); emptyFile.setLength(INITIAL_LENGTH); if (forceLegacy) { emptyFile.writeInt(headerLength - 1); } else { emptyFile.writeInt(0x80000001); emptyFile.writeLong(headerLength - 1); } emptyFile.getChannel().force(true); emptyFile.close(); try { newQueueFile(); fail(); } catch (IOException ex) { assertThat(ex.getMessage()).isIn( Arrays.asList("File is corrupt; length stored in header (15) is invalid.", "File is corrupt; length stored in header (31) is invalid.")); } }
randomAccessWriter.writeInt(0); // Final file size not known yet, write 0 randomAccessWriter.writeBytes("WAVE"); randomAccessWriter.writeBytes("fmt "); randomAccessWriter.writeInt(Integer.reverseBytes(16)); // Sub-chunk size, 16 for PCM randomAccessWriter.writeShort(Short.reverseBytes((short) 1)); // AudioFormat, 1 for PCM randomAccessWriter.writeInt(Integer.reverseBytes(sRate)); // Sample rate randomAccessWriter.writeInt(Integer.reverseBytes(sRate*bSamples*nChannels/8)); // Byte rate, SampleRate*NumberOfChannels*BitsPerSample/8 randomAccessWriter.writeShort(Short.reverseBytes((short)(nChannels*bSamples/8))); // Block align, NumberOfChannels*BitsPerSample/8 randomAccessWriter.writeInt(0); // Data chunk size not known yet, write 0 randomAccessWriter.writeInt(Integer.reverseBytes(36+payloadSize)); randomAccessWriter.writeInt(Integer.reverseBytes(payloadSize));
hF.writeInt(x); fnumArr[x][y]++;
public void writeInt(int v) throws IOException { delegate.writeInt(v); } public void writeLong(long v) throws IOException { delegate.writeLong(v); }