protected void remove() { Long l = uid; synchronized(DarknetPeerNode.this) { myFileOffersByUID.remove(l); hisFileOffersByUID.remove(l); } data.close(); }
FileOffer(long uid, RandomAccessBuffer data, String filename, String mimeType, String comment) throws IOException { this.uid = uid; this.data = data; this.filename = filename; this.mimeType = mimeType; this.comment = comment; size = data.size(); amIOffering = true; }
assertEquals(raf.size(), sz); int x = 0; byte[] buf = new byte[toRead]; for(int i=0;i<buf.length;i++) buf[i] = f.getByte(i+x); raf.pwrite(x, buf, 0, toRead); for(int i=0;i<buf.length;i++) buf[i] = (byte)~buf[i]; raf.pread(x, buf, 0, toRead); for(int i=0;i<buf.length;i++) assertEquals(buf[i], f.getByte(i+x)); x += toRead; int toRead = r.nextInt(maxRead-1)+1; byte[] buf = new byte[toRead]; raf.pread(x, buf, 0, toRead); for(int i=0;i<buf.length;i++) assertEquals(buf[i], f.getByte(i+x)); x += toRead; raf.close(); raf.free();
/** Create an array, fill it with random numbers, write it sequentially to the * RandomAccessBuffer, then read randomly and compare. */ protected void innerTestArray(int len, Random r, boolean readOnly) throws IOException { if(len == 0) return; byte[] buf = new byte[len]; r.nextBytes(buf); RandomAccessBuffer raf = construct(len); raf.pwrite(0L, buf, 0, buf.length); for(int i=0;i<100;i++) { int end = len == 1 ? 1 : r.nextInt(len)+1; int start = r.nextInt(end); checkArraySectionEqualsReadData(buf, raf, start, end, readOnly); } checkArraySectionEqualsReadData(buf, raf, 0, len, readOnly); if(len > 1) checkArraySectionEqualsReadData(buf, raf, 1, len-1, readOnly); raf.close(); raf.free(); }
length = data.size(); prb = new PartiallyReceivedBulk(updateManager.node.getUSM(), length, Node.PACKET_SIZE, data, true); } catch(DisconnectedException e) { Logger.error(this, "Peer " + source + " asked us for the blob file for the revocation key, then disconnected: " + e, e); data.close(); return true;
/** Test that after closing a RandomAccessBuffer we cannot read from it or write to it */ protected void innerTestClose(long sz) throws IOException { RandomAccessBuffer raf = construct(sz); raf.close(); byte[] buf = new byte[(int)Math.min(1024, sz)]; readWriteMustFail(raf, 0L, buf, 0, buf.length); raf.free(); }
/** @deprecated Only for unit tests */ @Deprecated public static void fill(RandomAccessBuffer raf, Random random, long offset, long length) throws IOException { long moved = 0; byte[] buf = new byte[BUFFER_SIZE]; while(moved < length) { int toRead = (int)Math.min(BUFFER_SIZE, length - moved); random.nextBytes(buf); raf.pwrite(offset + moved, buf, 0, toRead); moved += toRead; } }
@Override public int read(byte[] buf, int offset, int length) throws IOException { if(rafOffset >= rafLength) throw new EOFException(); length = (int) Math.min((long)length, rafLength-rafOffset); underlying.pread(rafOffset, buf, offset, length); rafOffset += length; return length; }
private void innerTestWriteOverLimit(long sz, int choppedBytes) throws IOException { RandomAccessBuffer raf = construct(sz); assertEquals(raf.size(), sz); long startAt = sz - choppedBytes; byte[] buf = new byte[choppedBytes]; readWriteMustFail(raf, sz+1025, buf, 0, buf.length); // 1KB in readWriteMustFail(raf, sz+buf.length, buf, 0, buf.length); raf.close(); raf.free();
/** Create an array, fill it with random numbers, write it sequentially to the * RandomAccessBuffer, then read randomly and compare. */ protected void innerTestArrayMigration(int len, Random r) throws IOException { if(len == 0) return; byte[] buf = new byte[len]; r.nextBytes(buf); RandomAccessBuffer raf = construct(len); TempRandomAccessBuffer t = (TempRandomAccessBuffer) raf; assertFalse(t.hasMigrated()); assertEquals(factory.getRamUsed(), len); t.migrateToDisk(); assertTrue(t.hasMigrated()); assertEquals(factory.getRamUsed(), 0); raf.pwrite(0L, buf, 0, buf.length); checkArrayInner(buf, raf, len, r); raf.close(); raf.free(); }
throw ioException; raf.pwrite(fileOffset, buf, 0, bytes); moved += bytes; fileOffset += bytes;
public byte[] getBlockData(int blockNum) { long fileOffset = (long)blockNum * (long)blockSize; int bs = (int) Math.min(blockSize, size - fileOffset); byte[] data = new byte[bs]; try { raf.pread(fileOffset, data, 0, bs); } catch (IOException e) { Logger.error(this, "Failed to read stored block "+blockNum+" on "+this+" : "+e, e); abort(RetrievalException.IO_ERROR, e.toString()); return null; } return data; }
/** Check that the array section equals the read data, then write it and repeat the check. */ public static void checkArraySectionEqualsReadData(byte[] buf, RandomAccessBuffer raf, int start, int end, boolean readOnly) throws IOException { int len = end - start; if(len == 0) return; byte[] tmp = new byte[len]; raf.pread(start, tmp, 0, len); for(int i=0;i<len;i++) assertEquals(tmp[i], buf[start+i]); if(!readOnly) { raf.pwrite(start, buf, start, len); } for(int i=0;i<len;i++) tmp[i] = 0; raf.pread(start, tmp, 0, len); for(int i=0;i<len;i++) assertEquals(tmp[i], buf[start+i]); }
public void abort(int errCode, String why) { if(logMINOR) Logger.normal(this, "Aborting "+this+": "+errCode+" : "+why+" first missing is "+blocksReceived.firstZero(0), new Exception("debug")); BulkTransmitter[] notifyBTs; BulkReceiver notifyBR; synchronized(this) { _aborted = true; _abortReason = errCode; _abortDescription = why; notifyBTs = transmitters; notifyBR = recv; } if(notifyBTs != null) { for(BulkTransmitter notifyBT: notifyBTs) { notifyBT.onAborted(); } } if(notifyBR != null) notifyBR.onAborted(); raf.close(); }
raf.pwrite(fileOffset, data, offset, bs); } catch (Throwable t) { Logger.error(this, "Failed to store received block "+blockNum+" on "+this+" : "+t, t);
/** * Construct a PartiallyReceivedBulk. * @param size Size of the file, does not have to be a multiple of blockSize. * @param blockSize Block size. * @param raf Where to store the data. * @param initialState If true, assume all blocks have been received. If false, assume no blocks have * been received. */ public PartiallyReceivedBulk(MessageCore usm, long size, int blockSize, RandomAccessBuffer raf, boolean initialState) { this.size = size; this.blockSize = blockSize; this.raf = raf; this.usm = usm; long blocks = (size + blockSize - 1) / blockSize; if(blocks > Integer.MAX_VALUE) throw new IllegalArgumentException("Too big"); this.blocks = (int)blocks; blocksReceived = new BitArray(this.blocks); if(initialState) { blocksReceived.setAllOnes(); blocksReceivedCount = this.blocks; } assert(raf.size() >= size); }
private void readWriteMustFail(RandomAccessBuffer raf, long startAt, byte[] buf, int offset, int length) throws IOException { if(length == 0) return; // NOP. try { raf.pread(startAt, buf, 0, buf.length); // Should work fail("Must throw!"); } catch (IOException e) { // Ok. } try { raf.pwrite(startAt, buf, 0, buf.length); // Should work fail("Must throw!"); } catch (IOException e) { // Ok. } }
@Override public void run() { if(logMINOR) Logger.minor(this, "Received file"); try { if(!receiver.receive()) { String err = "Failed to receive "+this; Logger.error(this, err); System.err.println(err); onReceiveFailure(); } else { data.close(); if(!dest.renameTo(node.clientCore.downloadsDir().file(baseFilename))){ Logger.error(this, "Failed to rename "+dest.getName()+" to remove .part suffix."); } onReceiveSuccess(); } } catch (Throwable t) { Logger.error(this, "Caught "+t+" receiving file", t); onReceiveFailure(); } finally { remove(); } if(logMINOR) Logger.minor(this, "Received file"); } }, "Receiver for bulk transfer "+uid+":"+filename);