/** * Delete an index entry in the idx file of the specified column. * * @param colname the column's name * @param idxPos index entry (line nr) to be deleted * @throws Exception */ protected long deleteIndexEntry(String colname, int idxPos) throws Exception { long s = System.currentTimeMillis(); if (!indexExists(colname)) { throw new Exception("No index created"); } int pkindex = def.getColPosition(colname); if (pkindex == -1) { throw new Exception("Column does not exist"); } Integer[] size = def.getSizes(); int recordSize = idxFixedRecordLength() + size[pkindex]; indexFile.seek(idxPos * recordSize); String sLine = indexFile.readLine(); String[] parts = sLine.split("#"); if (Integer.parseInt(parts[0].trim()) != idxPos) { throw new Exception("Index not found in index file"); } else { indexFile.seek(idxPos * recordSize + 6); String flag = "D"; indexFile.write(flag.toString().getBytes()); } long e = System.currentTimeMillis(); return e - s; }
/** * Read a file offset from the file PST Files have this tendency to store file offsets (pointers) * in 8 little endian bytes. Convert this to a long for seeking to. * * @param in handle for PST file * @param startOffset where to read the 8 bytes from * @return long representing the read location * @throws IOException */ protected long extractLEFileOffset(long startOffset) throws IOException { long offset = 0; if (this.getPSTFileType() == PSTFile.PST_TYPE_ANSI) { in.seek(startOffset); byte[] temp = new byte[4]; in.read(temp); offset |= temp[3] & 0xff; offset <<= 8; offset |= temp[2] & 0xff; offset <<= 8; offset |= temp[1] & 0xff; offset <<= 8; offset |= temp[0] & 0xff; } else { in.seek(startOffset); byte[] temp = new byte[8]; in.read(temp); offset = temp[7] & 0xff; long tmpLongValue; for (int x = 6; x >= 0; x--) { offset = offset << 8; tmpLongValue = (long) temp[x] & 0xff; offset |= tmpLongValue; } } return offset; }
public int bubbleSort() throws Exception { boolean swapped = true; int j = 0; int counter = 0; while (swapped) { swapped = false; j++; for (int i = 0; i < length() - j; i++) { file.seek(i * 4); int one = file.readInt(); int two = file.readInt(); if (one > two) { file.seek(i * 4); file.writeInt(two); file.writeInt(one); swapped = true; counter++; } } } return counter; }
/** * Search index file for a primary key value * * @param pkvalue the primary key value to search for * @param position [0] = index entry, [1] = table row * @throws Exception */ public boolean searchIndex(String pkvalue, Positions pos) throws Exception { boolean found = false; boolean end = false; if (!indexExists(def.getPK())) { throw new Exception("No index created"); } int pkindex = def.getColPosition(def.getPK()); if (pkindex == -1) { throw new Exception("Primary key does not exist"); } // calculate index = hash value String s_value = pkvalue.trim(); int index = hash(s_value); Integer[] size = def.getSizes(); int recordSize = idxFixedRecordLength() + size[pkindex]; indexFile.seek(index * recordSize); String line = indexFile.readLine(); if (line.substring(0, 1).equals(" ")) { // Empty record, end of search found = false; return found; } String[] parts = line.split("#"); String s_part = parts[2].trim(); if (s_part.equals(pkvalue) && !(parts[1].equals("D"))) { found = true; pos.index = Integer.parseInt(parts[0].trim()); pos.table = Integer.parseInt(parts[3].trim()); } while (!found && !end) { if (parts[4].substring(0, 4).equals("null")) { // end of linked list end = true; found = false; } else { index = Integer.parseInt(parts[4].trim()); indexFile.seek(index * recordSize); line = indexFile.readLine(); parts = line.split("#"); if (parts[2].trim().equals(pkvalue) && !(parts[1].equals("D"))) { found = true; pos.index = Integer.parseInt(parts[0].trim()); pos.table = Integer.parseInt(parts[3].trim()); } } } return found; }
public static void main(String args[]) { try { String fname = "d:\\q.txt"; String mode; // mode = "r";//r : file must exist mode = "rw"; // rw : file will be created or opened // open the file RandomAccessFile raf = new RandomAccessFile(fname, mode); /* //seek and write demo raf.seek(10);//position file r/w pointer at index 10 wrt BOF //a seek beyond file size causes file to grow upto the seek value raf.write(65);//raf.write('A'); */ // r/w java datatypes int i1, i2; float f1, f2; char c1, c2; String s1, s2; i1 = -10; f1 = 1234.5678F; c1 = 'q'; s1 = "hello files"; raf.seek(0); // reach BOF raf.writeInt(i1); raf.writeFloat(f1); raf.writeChar(c1); raf.writeUTF(s1); raf.seek(0); // reach BOF i2 = raf.readInt(); f2 = raf.readFloat(); c2 = raf.readChar(); s2 = raf.readUTF(); System.out.println(i2); System.out.println(f2); System.out.println(c2); System.out.println(s2); // close the file raf.close(); } catch (IOException ex) { System.out.println(ex); // ex converts into ex.toString() } } // main
/** * Close the I/O stream and save any unsaved data. * * @exception IOException from library call */ public void close() throws IOException { if (headerSize > 0) { // write number of rows, CRC outStream.seek(NUMROWS_OFFSET); outStream.writeLong(numRows); outStream.seek(headerSize - CRC_SIZE); outStream.writeInt(CRC); } outStream.close(); }
/** * Fills the buffer with more data, taking into account shuffling and other tricks for dealing * with marks. Assumes that it is being called by a synchronized method. This method also assumes * that all data has already been read in, hence pos > count. */ private void fill() throws IOException { if (markpos < 0) { pos = 0; /* no mark: throw away the buffer */ bufpos += count; } else if (pos >= buf.length) /* no room left in buffer */ if (markpos > 0) { /* can throw away early part of the buffer */ int sz = pos - markpos; System.arraycopy(buf, markpos, buf, 0, sz); pos = sz; bufpos += markpos; markpos = 0; } else if (buf.length >= marklimit) { markpos = -1; /* buffer got too big, invalidate mark */ pos = 0; /* drop buffer contents */ bufpos += count; } else { /* grow buffer */ int nsz = pos * 2; if (nsz > marklimit) nsz = marklimit; byte nbuf[] = new byte[nsz]; System.arraycopy(buf, 0, nbuf, 0, pos); buf = nbuf; } count = pos; // limit to datalen int len = buf.length - pos; if (bufpos - start + pos + len > datalen) len = (int) (datalen - (bufpos - start + pos)); synchronized (in) { in.seek(bufpos + pos); int n = in.read(buf, pos, len); if (n > 0) count = n + pos; } }
/** * Add the given text string to the end of a given file. * * @param inStr The string to be added. * @param fileStr the name of the file to be added. */ public static void addStringToFile(String inStr, String fileStr) throws Exception { RandomAccessFile raFile = new RandomAccessFile(fileStr, "rw"); raFile.seek(raFile.length()); raFile.writeBytes(inStr); raFile.close(); }
/** Reads an SRATIONAL value and returns it as a Rational. */ protected Rational readSignedRational(long count, long value) throws IOException { _raf.seek(value); long numer = ModuleBase.readSignedInt(_raf, _bigEndian); long denom = ModuleBase.readSignedInt(_raf, _bigEndian); return new Rational(numer, denom); }
/** * Reads an array of strings from the TIFF file. * * @param count Number of strings to read * @param value Offset from which to read */ protected String[] readASCIIArray(long count, long value) throws IOException { _raf.seek(value); int nstrs = 0; List list = new LinkedList(); byte[] buf = new byte[(int) count]; _raf.read(buf); StringBuffer strbuf = new StringBuffer(); for (int i = 0; i < count; i++) { int b = buf[i]; if (b == 0) { list.add(strbuf.toString()); strbuf.setLength(0); } else { strbuf.append((char) b); } } /* We can't use ArrayList.toArray because that returns an Object[], not a String[] ... sigh. */ String[] strs = new String[nstrs]; ListIterator iter = list.listIterator(); for (int i = 0; i < nstrs; i++) { strs[i] = (String) iter.next(); } return strs; }
@Override public ByteBuffer readPage(File file, long position, ByteBuffer pageBuffer) throws IOException, InterruptedException { long start = System.currentTimeMillis(); RandomAccessFile randomAccessFile = randomAccessFile(file); try { randomAccessFile.seek(position); randomAccessFile.readFully(pageBuffer.array(), pageBuffer.arrayOffset(), pageSizeBytes); if (Thread.interrupted()) { throw new InterruptedException(); } long stop = System.currentTimeMillis(); if (LOG.isLoggable(Level.FINE)) { LOG.log( Level.FINE, "Read page at {0} of {1}: {2} msec", new Object[] {position, file, stop - start}); } } catch (EOFException e) { LOG.log( Level.SEVERE, "Caught EOFException while reading {0}, position {1}", new Object[] {file, position}); LOG.log(Level.SEVERE, "stack", e); throw e; } finally { randomAccessFile.close(); } return pageBuffer; }
/** * Create an index file for the specified column. * * @param colname the column's name * @return the time it took to run this method. * @throws Exception */ public long createIndexFile() throws Exception { long s = System.currentTimeMillis(); // fill idx file with hashsize empty records/lines (empty means filled with spaces) int pkindex = def.getColPosition(def.getPK()); if (pkindex == -1) { throw new Exception("Primary key does not exist"); } Integer[] size = def.getSizes(); int recordSize = idxFixedRecordLength() + size[pkindex]; StringBuffer temp = new StringBuffer(); for (int i = 0; i < recordSize - 2; i++) { temp.append(" "); } indexFile.seek(0); for (int i = 0; i < this.hashsize; i++) { indexFile.writeBytes(temp + "\r\n"); } int table_pos = 0; // create an index entry for each row present in the tbl-file (for column=colname) String line = null; tableFile.seek(0); while ((line = tableFile.readLine()) != null) { // get column value (don't strip the spaces) String pkvalue = line.split("#")[pkindex]; addIndexEntry(table_pos, pkvalue); table_pos++; } long e = System.currentTimeMillis(); return e - s; }
/** * Copy the given byte range of the given input to the given output. * * @param input The input to copy the given range to the given output for. * @param output The output to copy the given range from the given input for. * @param start Start of the byte range. * @param length Length of the byte range. * @throws IOException If something fails at I/O level. */ private static void copy(RandomAccessFile input, OutputStream output, long start, long length) throws IOException { byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int read; if (input.length() == length) { // Write full range. while ((read = input.read(buffer)) > 0) { output.write(buffer, 0, read); } } else { // Write partial range. input.seek(start); long toRead = length; while ((read = input.read(buffer)) > 0) { if ((toRead -= read) > 0) { output.write(buffer, 0, read); } else { output.write(buffer, 0, (int) toRead + read); break; } } } }
/* write a chunk data to the region file at specified sector number */ private void write(int sectorNumber, byte[] data, int length) throws IOException { debugln(" " + sectorNumber); 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 }
/** * Loads a query from the mgf file. * * @param file The file to read the query from. * @param indexElement The index element pointing to that specific ms2 query. * @return * @oaram index The query's 1-based index in the MGF file. This index is stored in the returned * Ms2Query object. */ private static Ms2Query loadIndexedQueryFromFile(File file, IndexElement indexElement, int index) throws JMzReaderException { RandomAccessFile accFile = null; try { accFile = new RandomAccessFile(file, "r"); // read the indexed element byte[] byteBuffer = new byte[indexElement.getSize()]; // read the file from there accFile.seek(indexElement.getStart()); accFile.read(byteBuffer); String ms2Buffer = new String(byteBuffer); // create the query Ms2Query query = new Ms2Query(ms2Buffer, index); return query; } catch (FileNotFoundException e) { throw new JMzReaderException("MGF file could not be found.", e); } catch (IOException e) { throw new JMzReaderException("Failed to read from MGF file", e); } finally { if (accFile != null) { try { accFile.close(); } catch (IOException e) { // ignore } } } }
public void loadMetaData() throws IOException { pageFile.seek(offset); nextPageId = pageFile.readInt(); currentFill = pageFile.readInt(); bloomfilter = pageFile.readInt(); type = pageFile.readByte(); }
/** * Reads a file from the given InputStream and saves it into the local download directory * * @param in The InputStream from which to read the file * @param start The starting byte of the piece that will be downloaded from this peer * @param end The end byte (exclusive) of the piece that will be downloaded from this peer * @throws java.io.IOException if an error occurs while downloading the file */ private void saveFile(InputStream in, int start, int end) throws IOException { if (start >= end) { throw new IllegalStateException( "Start byte (" + start + ") must be less than the end byte (" + end + ")"); } RandomAccessFile fileOut = null; try { File downloadFile = new File(Client.downloadDir, _file); // Open the file in read/write mode with synchronous content/metadata writing fileOut = new RandomAccessFile(downloadFile, "rws"); fileOut.seek(start); byte[] bytes = new byte[_chunkSize]; int bytesRead; int pos = start; while ((bytesRead = in.read(bytes, 0, _chunkSize)) >= 0 && pos < end) { fileOut.write(bytes, 0, Math.min(bytesRead, end - pos)); pos += bytesRead; } } catch (SocketException e) { System.err.println( "[DownloadFromPeerRunnable] Disconnected from peer at " + _peer.getInetAddress()); _callback.onFinishChunk(_file, false); } finally { if (fileOut != null) { try { fileOut.close(); } catch (IOException e) { e.printStackTrace(); } } } }
/** * reads the content of an existing file using the current domain * * @param domain The namespace used to identify the application domain (1st level directory) to * use * @param path The path relative to the domain for the file * @param offset the offset from the beginning of the file. * @param len The length of the block in bytes * @return The contents of the file */ public byte[] readByteFromFile(String path, long offset, int len) throws EOFException, FileAccessException { try { if (_isLocal) { File tmpFile = new File(_rootFile.getCanonicalPath() + File.separatorChar + path); if (tmpFile.isFile()) { RandomAccessFile raf = new RandomAccessFile(tmpFile, "r"); byte[] buffer = new byte[len]; raf.seek(offset); int totalByteRead = 0; ByteArrayOutputStream out = new ByteArrayOutputStream(); int result = 0; while (totalByteRead < len && raf.getFilePointer() < raf.length()) { result = raf.read(buffer, 0, (len - totalByteRead)); if (result != -1) { out.write(buffer, 0, result); totalByteRead += result; } else if (totalByteRead == 0) throw new EOFException("End of file reached!"); else break; } raf.close(); out.flush(); out.close(); return out.toByteArray(); } else throw new FileAccessException("Path is not a file"); } else return _remote.readByteFromFile(_domain, path, offset, len); } catch (EOFException eofe) { throw eofe; } catch (FileAccessException fae) { throw fae; } catch (Exception e) { throw new FileAccessException(e); } }
public void saveMetaData() throws IOException { pageFile.seek(offset); pageFile.writeInt(nextPageId); pageFile.writeInt(currentFill); pageFile.writeInt(bloomfilter); pageFile.writeByte(type); }
public int luaCB_read(Lua l, LuaPlugin plugin) { l.pushNil(); l.pushNil(); try { long offset = (long) l.checkNumber(2); int length = (int) l.checkNumber(3); if (length <= 0) { l.pushNil(); l.pushString("Length is not positive"); return 2; } byte[] tmp = new byte[length]; object.seek(offset); length = object.read(tmp); if (length < 0) { l.pushNil(); l.pushNil(); return 2; } StringBuffer buf = new StringBuffer(length); for (byte x : tmp) buf.appendCodePoint((int) x & 0xFF); l.push(buf.toString()); } catch (IOException e) { l.pushNil(); l.pushString("IOException: " + e.getMessage()); return 2; } return 1; }
private void execFormatTrackTask() { File file = this.ioFile; long pos = this.ioFilePos; int cnt = this.ioByteCnt; if ((file != null) && (pos >= 0) && (cnt > 0)) { boolean err = false; RandomAccessFile raf = null; try { raf = new RandomAccessFile(file, "rw"); raf.seek(pos); while (cnt > 0) { raf.write(0); --cnt; } raf.close(); raf = null; } catch (IOException ex) { err = true; if (!this.writeErrShown) { this.writeErrShown = true; EmuUtil.fireShowError(this.owner, null, ex); } } finally { EmuUtil.doClose(raf); } if (err) { this.errorReg = ERROR_UNCORRECTABLE_DATA; this.statusReg |= STATUS_ERROR; } fireInterrupt(); } }
/** * reads the content of an existing file using the current domain * * @param domain The namespace used to identify the application domain (1st level directory) to * use * @param path The path relative to the domain for the file * @param block The sequential block number for the data to be read starting with 1 * @param len The length of the block in bytes * @return The contents of the file */ public byte[] readFromFile(String path, int block, int len) { byte[] buffer = null; try { if (_isLocal) { File tmpFile = new File(_rootFile.getCanonicalPath() + File.separatorChar + path); if (tmpFile.isFile()) { RandomAccessFile in = new RandomAccessFile(tmpFile, "r"); in.seek((block - 1) * len); int result = -1; buffer = new byte[len]; if (in.getFilePointer() < in.length()) { result = in.read(buffer); ByteArrayOutputStream out = new ByteArrayOutputStream(result); out.write(buffer, 0, result); in.close(); return out.toByteArray(); } else { in.close(); } } } else { buffer = _remote.readFromFile(_domain, path, block, len); } } catch (Exception ex) { ex.printStackTrace(); } return buffer; }
private void loadRawData() throws IOException { if (rawData == null) { pageHandler.trimPageSet(id); rawData = new byte[PAYLOAD]; pageFile.seek(offset + HEADER); pageFile.readFully(rawData); } }
public int dingPiao(String flightNum, String day, int seats) { int leftSeats = 0; try { long index = cacuIndex(day); long address = cacuAddr(flightNum); long absoluteAddress = index + address; raf.seek(absoluteAddress); int bookedSeats = raf.readInt(); String sqlString = "select seat,week from flight where flight='" + flightNum + "' "; ResultSet rs = sqlConnection.executeQuery(sqlString); int totalSeats = 0; String week = ""; while (rs.next()) { totalSeats = rs.getInt(1); week = rs.getString(2); } String c = isAbsence(day); int flag = 0; for (int i = 0; i < week.length(); i++) { String w = week.substring(i, i + 1); if (c.equals(w)) { flag = 1; break; } } if (flag == 1) { leftSeats = totalSeats - bookedSeats; if (leftSeats >= seats) { raf.seek(absoluteAddress); raf.writeInt(bookedSeats + seats); return -1; } else return leftSeats; } else return -2; } catch (Exception e) { e.printStackTrace(); } return leftSeats; }
private int peek() throws IOException { if (checkPos(this.pointer)) { buf.seek(pointer++); return b2i(buf.readByte()); } else { return -1; } }
public PSTFile(File fileName) throws FileNotFoundException, PSTException, IOException { // attempt to open the file. in = new RandomAccessFile(fileName, "r"); // get the first 4 bytes, should be !BDN try { byte[] temp = new byte[4]; in.read(temp); String strValue = new String(temp); if (!strValue.equals("!BDN")) { throw new PSTException("Invalid file header: " + strValue + ", expected: !BDN"); } // make sure we are using a supported version of a PST... byte[] fileTypeBytes = new byte[2]; in.seek(10); in.read(fileTypeBytes); // ANSI file types can be 14 or 15: if (fileTypeBytes[0] == PSTFile.PST_TYPE_ANSI_2) { fileTypeBytes[0] = PSTFile.PST_TYPE_ANSI; } if (fileTypeBytes[0] != PSTFile.PST_TYPE_ANSI && fileTypeBytes[0] != PSTFile.PST_TYPE_UNICODE) { throw new PSTException("Unrecognised PST File version: " + fileTypeBytes[0]); } this.pstFileType = fileTypeBytes[0]; // make sure encryption is turned off at this stage... if (this.getPSTFileType() == PST_TYPE_ANSI) { in.seek(461); } else { in.seek(513); } encryptionType = in.readByte(); if (encryptionType == 0x02) { throw new PSTException( "Only unencrypted and compressable PST files are supported at this time"); } // build out name to id map. processNameToIdMap(in); } catch (IOException err) { throw new PSTException("Unable to read PST Sig", err); } }
private void append(SingleHit[] hits, String prefix, int chrom) throws IOException { RandomAccessFile positionsRAF = new RandomAccessFile(getPositionsFname(prefix, chrom), "rw"); RandomAccessFile weightsRAF = new RandomAccessFile(getWeightsFname(prefix, chrom), "rw"); RandomAccessFile lasRAF = new RandomAccessFile(getLaSFname(prefix, chrom), "rw"); positionsRAF.seek(positionsRAF.length()); weightsRAF.seek(weightsRAF.length()); lasRAF.seek(lasRAF.length()); for (int i = 0; i < hits.length; i++) { SingleHit h = hits[i]; positionsRAF.writeInt(h.pos); weightsRAF.writeFloat(h.weight); lasRAF.writeInt(makeLAS(h.length, h.strand)); } positionsRAF.close(); weightsRAF.close(); lasRAF.close(); }
private static void truncateBlock(File blockFile, File metaFile, long oldlen, long newlen) throws IOException { if (newlen == oldlen) { return; } if (newlen > oldlen) { throw new IOException( "Cannout truncate block to from oldlen (=" + oldlen + ") to newlen (=" + newlen + ")"); } DataChecksum dcs = BlockMetadataHeader.readHeader(metaFile).getChecksum(); int checksumsize = dcs.getChecksumSize(); int bpc = dcs.getBytesPerChecksum(); long n = (newlen - 1) / bpc + 1; long newmetalen = BlockMetadataHeader.getHeaderSize() + n * checksumsize; long lastchunkoffset = (n - 1) * bpc; int lastchunksize = (int) (newlen - lastchunkoffset); byte[] b = new byte[Math.max(lastchunksize, checksumsize)]; RandomAccessFile blockRAF = new RandomAccessFile(blockFile, "rw"); try { // truncate blockFile blockRAF.setLength(newlen); // read last chunk blockRAF.seek(lastchunkoffset); blockRAF.readFully(b, 0, lastchunksize); } finally { blockRAF.close(); } // compute checksum dcs.update(b, 0, lastchunksize); dcs.writeValue(b, 0, false); // update metaFile RandomAccessFile metaRAF = new RandomAccessFile(metaFile, "rw"); try { metaRAF.setLength(newmetalen); metaRAF.seek(newmetalen - checksumsize); metaRAF.write(b, 0, checksumsize); } finally { metaRAF.close(); } }
public void tuiPiao(String flightNum, String day, int seats) { try { long index = cacuIndex(day); long address = cacuAddr(flightNum); long absoluteAddress = index + address; raf.seek(absoluteAddress); int bookedSeats = raf.readInt(); if (bookedSeats < seats) JOptionPane.showMessageDialog(null, "退票数大于已定票数!", "错误信息", JOptionPane.ERROR_MESSAGE); else { raf.seek(absoluteAddress); raf.writeInt(bookedSeats - seats); } } catch (Exception e) { } }
public void read() throws Exception { file.seek(0); System.out.print("[ "); while (file.getFilePointer() < file.length()) System.out.print(file.readInt() + " "); System.out.println("]"); }