/** * initialize the db file * * @param raf * @ */ private void initDbFile(RandomAccessFile raf) { //1. zero fill the header part raf.seek(0L); raf.write(new byte[8]); //super block raf.write(new byte[dbConfig.getTotalHeaderSize()]); //header block headerPool = new LinkedList <HeaderBlock>(); indexPool = new LinkedList <IndexBlock>(); }
/** * internal method to add a new data block record * * @param raf * @param startIp * @param endIp * @param region data */ private void addDataBlock( RandomAccessFile raf, String startIp, String endIp, String region) { try { byte[] data = region.getBytes("UTF-8"); int dataPtr = 0; /*byte[] city = new byte[4]; * int city_id = getCityId(region); * Util.writeIntLong(city, 0, city_id); * dataPtr = (int)raf.getFilePointer(); * raf.write(city); * raf.write(data);*/ //check region ptr pool first if (regionPtrPool.containsKey(region)) { DataBlock dataBlock = regionPtrPool.get(region); dataPtr = dataBlock.getDataPtr(); Console.WriteLine("dataPtr: " + dataPtr + ", region: " + region); } else { byte[] city = new byte[4]; int city_id = getCityId(region); Util.writeIntLong(city, 0, city_id); dataPtr = (int)raf.getFilePointer(); raf.write(city); raf.write(data); regionPtrPool.put(region, new DataBlock(city_id, region, dataPtr)); } //add the data index blocks IndexBlock ib = new IndexBlock( Util.ip2long(startIp), Util.ip2long(endIp), dataPtr, data.Length + 4 //4 bytes for the city id ); indexPool.add(ib); } catch (Exception e) { Console.WriteLine(e); } }
//public static void WriteHexDumpFromFile(string filename) //{ // try // { // File file = new File(filename); // RandomAccessFile stream = new RandomAccessFile(file, "r"); // sbyte[] bytes = new sbyte[stream.length()]; // stream.read(bytes); // stream.close(); // System.Console.WriteLine("dump of [" + file + "]"); // Console.WriteHexDump(bytes); // } // catch (Exception exc) // { // Console.WriteThrowable(exc); // } //} /// <summary> /// writes bytes to file /// </summary> /// <param name="cdata"></param> /// <param name="p"></param> public static void WriteBytes(sbyte[] cdata, string filename, bool utf8) { try { File f = new File(filename); if (f.exists()) { f.delete(); } RandomAccessFile stream = new RandomAccessFile(filename, "rw"); if (utf8) { stream.writeByte(0xEF); stream.writeByte(0xBB); stream.writeByte(0xBF); } stream.write(cdata); stream.close(); } catch { } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public void write(java.io.RandomAccessFile output) throws java.io.IOException public virtual void write(RandomAccessFile output) { sbyte[] buffer = new sbyte[size()]; ByteBuffer byteBuffer = ByteBuffer.wrap(buffer); write(byteBuffer); // Write the file and truncate it to the correct Length output.write(buffer); output.Length = buffer.Length; }
public virtual ByteBuffer decrypt(ByteBuffer f) { if (f.capacity() == 0) { return(null); } CryptoEngine crypto = new CryptoEngine(); sbyte[] inBuf; if (f.hasArray() && f.position() <= PSP_HEADER_SIZE) { inBuf = f.array(); } else { int currentPosition = f.position(); f.position(currentPosition - PSP_HEADER_SIZE); inBuf = new sbyte[f.remaining()]; f.get(inBuf); f.position(currentPosition); } int inSize = inBuf.Length; sbyte[] elfBuffer = crypto.PRXEngine.DecryptAndUncompressPRX(inBuf, inSize); if (elfBuffer == null) { return(null); } if (CryptoEngine.ExtractEbootStatus) { try { string ebootPath = Settings.Instance.DiscTmpDirectory; System.IO.Directory.CreateDirectory(ebootPath); RandomAccessFile raf = new RandomAccessFile(ebootPath + "EBOOT.BIN", "rw"); raf.write(elfBuffer); raf.close(); } catch (IOException) { // Ignore. } } return(ByteBuffer.wrap(elfBuffer)); }
public static void Main(string[] args) { try { RandomAccessFile inToc = new RandomAccessFile("tmp/umdbuffer.toc", "r"); RandomAccessFile inIso = new RandomAccessFile("tmp/umdbuffer.iso", "r"); RandomAccessFile outIso = new RandomAccessFile("tmp/umd.iso", "rw"); int numSectors = inToc.readInt(); Console.WriteLine(string.Format("numSectors={0:D}", numSectors)); sbyte[] buffer = new sbyte[sectorLength]; for (int i = 4; i < inToc.Length(); i += 8) { int sectorNumber = inToc.readInt(); int bufferedSectorNumber = inToc.readInt(); inIso.seek(bufferedSectorNumber * (long)sectorLength); inIso.readFully(buffer); outIso.seek(sectorNumber * (long)sectorLength); outIso.write(buffer); } inToc.close(); inIso.close(); outIso.close(); } catch (FileNotFoundException e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } catch (IOException e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } }
public RegionFile(File path) { _offsets = new int[SectorInts]; _chunkTimeStamps = new int[SectorInts]; _fileName = path; _sizeDelta = 0; try { if (path.exists()) { LastModified = path.lastModified(); } _file = new RandomAccessFile(path, "rw"); if (_file.length() < SectorBytes) { // we need to write the chunk offset table for (int i = 0; i < SectorInts; ++i) { _file.writeInt(0); } // write another sector for the timestamp info for (int i = 0; i < SectorInts; ++i) { _file.writeInt(0); } _sizeDelta += SectorBytes * 2; } if ((_file.length() & 0xfff) != 0) { // the file size is not a multiple of 4KB, grow it for (int i = 0; i < (_file.length() & 0xfff); ++i) { _file.write(0); } } // set up the available sector map int nSectors = (int)_file.length() / SectorBytes; _sectorFree = new List <bool>(nSectors); for (int i = 0; i < nSectors; ++i) { _sectorFree.Add(true); } _sectorFree[0] = false; // chunk offset table _sectorFree[1] = false; // for the last modified info _file.seek(0); for (int i = 0; i < SectorInts; ++i) { int offset = _file.readInt(); _offsets[i] = offset; if (offset == 0 || (offset >> 8) + (offset & 0xFF) > _sectorFree.Count) { continue; } for (int sectorNum = 0; sectorNum < (offset & 0xFF); ++sectorNum) { _sectorFree[(offset >> 8) + sectorNum] = false; } } for (int i = 0; i < SectorInts; i++) { int lastModValue = _file.readInt(); _chunkTimeStamps[i] = lastModValue; } } catch (IOException e) { Console.WriteLine(e.ToString()); } }
protected virtual void Write(int x, int z, byte[] data, int length) { try { int offset = GetOffset(x, z); int sectorNumber = offset >> 8; int sectorsAllocated = offset & 0xFF; int sectorsNeeded = (length + ChunkHeaderSize) / SectorBytes + 1; // maximum chunk size is 1MB if (sectorsNeeded >= 256) { return; } if (sectorNumber != 0 && sectorsAllocated == sectorsNeeded) { // we can simply overwrite the old sectors Write(sectorNumber, data, length); } else { // we need to allocate new sectors // mark the sectors previously used for this chunk as free for (int i = 0; i < sectorsAllocated; ++i) { _sectorFree[sectorNumber + i] = true; } // scan for a free space large enough to store this chunk int runStart = _sectorFree.IndexOf(true); int runLength = 0; if (runStart != -1) { for (int i = runStart; i < _sectorFree.Count; ++i) { if (runLength != 0) { if (_sectorFree[i]) { runLength++; } else { runLength = 0; } } else if (_sectorFree[i]) { runStart = i; runLength = 1; } if (runLength >= sectorsNeeded) { break; } } } if (runLength >= sectorsNeeded) { // we found a free space large enough sectorNumber = runStart; SetOffset(x, z, (sectorNumber << 8) | sectorsNeeded); for (int i = 0; i < sectorsNeeded; ++i) { _sectorFree[sectorNumber + i] = false; } Write(sectorNumber, data, length); } else { // no free space large enough found -- we need to grow // the file _file.seek(_file.length()); sectorNumber = _sectorFree.Count; for (int i = 0; i < sectorsNeeded; ++i) { _file.write(_emptySector); _sectorFree.Add(false); } _sizeDelta += SectorBytes * sectorsNeeded; Write(sectorNumber, data, length); SetOffset(x, z, (sectorNumber << 8) | sectorsNeeded); } } SetTimestamp(x, z, (int)(JavaSystem.CurrentTimeMillis() / 1000L)); } catch (IOException e) { Console.WriteLine(e); } }
public void write(int i, int j, byte[] abyte0, int k) { try { int l = getOffset(i, j); int i1 = l >> 8; int l1 = l & 0xff; int i2 = (k + 5) / 4096 + 1; if (i2 >= 256) { return; } if (i1 != 0 && l1 == i2) { debug("SAVE", i, j, k, "rewrite"); write(i1, abyte0, k); } else { for (int j2 = 0; j2 < l1; j2++) { sectorFree.set(i1 + j2, Boolean.valueOf(true)); } int k2 = sectorFree.indexOf(Boolean.valueOf(true)); int l2 = 0; if (k2 != -1) { int i3 = k2; do { if (i3 >= sectorFree.size()) { break; } if (l2 != 0) { if (((Boolean)sectorFree.get(i3)).booleanValue()) { l2++; } else { l2 = 0; } } else if (((Boolean)sectorFree.get(i3)).booleanValue()) { k2 = i3; l2 = 1; } if (l2 >= i2) { break; } i3++; } while (true); } if (l2 >= i2) { debug("SAVE", i, j, k, "reuse"); int j1 = k2; setOffset(i, j, j1 << 8 | i2); for (int j3 = 0; j3 < i2; j3++) { sectorFree.set(j1 + j3, Boolean.valueOf(false)); } write(j1, abyte0, k); } else { debug("SAVE", i, j, k, "grow"); dataFile.seek(dataFile.length()); int k1 = sectorFree.size(); for (int k3 = 0; k3 < i2; k3++) { dataFile.write(emptySector); sectorFree.add(Boolean.valueOf(false)); } sizeDelta += 4096 * i2; write(k1, abyte0, k); setOffset(i, j, k1 << 8 | i2); } } setChunkTimestamp(i, j, (int)(java.lang.System.currentTimeMillis() / 1000L)); } catch (IOException ioexception) { ioexception.printStackTrace(); } }
public RegionFile(File file) { lastModified = 0L; fileName = file; debugln((new StringBuilder()).append("REGION LOAD ").append(fileName).toString()); sizeDelta = 0; try { if (file.exists()) { lastModified = file.lastModified(); } dataFile = new RandomAccessFile(file, "rw"); if (dataFile.length() < 4096L) { for (int i = 0; i < 1024; i++) { dataFile.writeInt(0); } for (int j = 0; j < 1024; j++) { dataFile.writeInt(0); } sizeDelta += 8192; } if ((dataFile.length() & 4095L) != 0L) { for (int k = 0; k < (dataFile.length() & 4095L); k++) { dataFile.write(0); } } int l = (int)dataFile.length() / 4096; sectorFree = new ArrayList(l); for (int i1 = 0; i1 < l; i1++) { sectorFree.add(Boolean.valueOf(true)); } sectorFree.set(0, Boolean.valueOf(false)); sectorFree.set(1, Boolean.valueOf(false)); dataFile.seek(0L); for (int j1 = 0; j1 < 1024; j1++) { int l1 = dataFile.readInt(); offsets[j1] = l1; if (l1 == 0 || (l1 >> 8) + (l1 & 0xff) > sectorFree.size()) { continue; } for (int j2 = 0; j2 < (l1 & 0xff); j2++) { sectorFree.set((l1 >> 8) + j2, Boolean.valueOf(false)); } } for (int k1 = 0; k1 < 1024; k1++) { int i2 = dataFile.readInt(); chunkTimestamps[k1] = i2; } } catch (IOException ioexception) { ioexception.printStackTrace(); } }
/** * make the Db file * * @param dbFile target output file path * @ */ public void make(String dbFile) { //check and load the gloabl region if (globalRegionFile != null) { Console.WriteLine("+-Try to load the global region data ..."); BufferedReader greader = new BufferedReader(new FileReader(globalRegionFile)); String gline = null; while ((gline = greader.readLine()) != null) { String[] p = gline.split(","); if (p.Length != 5) { continue; } //push the mapping globalRegionMap.put(p[2], Int32.Parse(p[0])); } greader.close(); Console.WriteLine("|--[Ok]"); } //alloc the header size BufferedReader reader = new BufferedReader(new FileReader(this.ipSrcFile)); RandomAccessFile raf = new RandomAccessFile(dbFile, "rw"); //init the db file initDbFile(raf); Console.WriteLine("+-Db file initialized."); //analysis main loop Console.WriteLine("+-Try to write the data blocks ... "); String line = null; while ((line = reader.readLine()) != null) { line = line.trim(); if (line.length() == 0) { continue; } if (line.charAt(0) == '#') { continue; } //1. get the start ip int sIdx = 0, eIdx = 0; if ((eIdx = line.indexOf('|', sIdx + 1)) == -1) { continue; } String startIp = line.substring(sIdx, eIdx); //2. get the end ip sIdx = eIdx + 1; if ((eIdx = line.indexOf('|', sIdx + 1)) == -1) { continue; } String endIp = line.substring(sIdx, eIdx); //3. get the region sIdx = eIdx + 1; String region = line.substring(sIdx); Console.WriteLine("+-Try to process item " + line); addDataBlock(raf, startIp, endIp, region); Console.WriteLine("|--[Ok]"); } Console.WriteLine("|--Data block flushed!"); Console.WriteLine("|--Data file pointer: " + raf.getFilePointer() + "\n"); //write the index bytes Console.WriteLine("+-Try to write index blocks ... "); //record the start block IndexBlock indexBlock = null; HeaderBlock hb = null; indexBlock = indexPool.getFirst(); long indexStartIp = indexBlock.getStartIp(), indexStratPtr = raf.getFilePointer(), indexEndPtr; headerPool.add(new HeaderBlock(indexStartIp, (int)(indexStratPtr))); int blockLength = IndexBlock.getIndexBlockLength(); int counter = 0, shotCounter = (dbConfig.getIndexBlockSize() / blockLength) - 1; //var indexIt = indexPool.iterator(); //while (indexIt.hasNext()) foreach (var indexIt in indexPool.iterator()) { indexBlock = indexIt; if (++counter >= shotCounter) { hb = new HeaderBlock( indexBlock.getStartIp(), (int)raf.getFilePointer() ); headerPool.add(hb); counter = 0; } //write the buffer raf.write(indexBlock.getBytes()); } //record the end block if (counter > 0) { indexBlock = indexPool.getLast(); hb = new HeaderBlock( indexBlock.getStartIp(), ((int)raf.getFilePointer()) - IndexBlock.getIndexBlockLength() ); headerPool.add(hb); } indexEndPtr = raf.getFilePointer(); Console.WriteLine("|--[Ok]"); //write the super blocks Console.WriteLine("+-Try to write the super blocks ... "); raf.seek(0L); //reset the file pointer byte[] superBuffer = new byte[8]; Util.writeIntLong(superBuffer, 0, indexStratPtr); Util.writeIntLong(superBuffer, 4, indexEndPtr - blockLength); raf.write(superBuffer); Console.WriteLine("|--[Ok]"); //write the header blocks Console.WriteLine("+-Try to write the header blocks ... "); //var headerIt = headerPool.iterator(); //while (headerIt.hasNext()) //{ // HeaderBlock headerBlock = headerIt.next(); // raf.write(headerBlock.getBytes()); //} foreach (var headerBlock in headerPool.iterator()) { raf.write(headerBlock.getBytes()); } //write the copyright and the release timestamp info Console.WriteLine("+-Try to write the copyright and release date info ... "); raf.seek(raf.length()); //Calendar cal = Calendar.getInstance(); //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); String copyright = "Created by lionsoul at " + DateTime.Now.ToString("yyyy/MM/dd"); //dateFormat.format(cal.getTime()); var timestamp = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000; raf.write((Int32)timestamp); //the unix timestamp raf.write(copyright.getBytes()); Console.WriteLine("|--[Ok]"); reader.close(); raf.close(); }