/** * Creates a {@link DhtStorablePacket} object from its byte array representation. * The type of packet depends on the packet type field in the byte array. * If there is an error, <code>null</code> is returned. * @param data * @throws MalformedPacketException */ public static DhtStorablePacket createPacket(byte[] data) throws MalformedPacketException { DataPacket packet = DataPacket.createPacket(data); if (packet instanceof DhtStorablePacket) return (DhtStorablePacket)packet; else { log.error("Packet is not a DhtStorablePacket: " + packet); return null; } }
/** * Creates an <code>DataPacket</code> from raw datagram data. The only thing that is initialized * is the protocol version. The packet type code is verified.<br/> * Subclasses start reading at byte <code>HEADER_LENGTH</code> after calling this constructor. * @param data */ protected DataPacket(byte[] data) { super(data[1]); // byte 1 is the protocol version in a data packet checkPacketType(data[0]); if (data[0] != getPacketTypeCode()) log.error("Wrong type code for " + getClass().getSimpleName() + ". Expected <" + getPacketTypeCode() + ">, got <" + (char)data[0] + ">"); }
/** * Writes the Type and Protocol Version fields of a Data Packet to * an {@link OutputStream}. * @param outputStream */ protected void writeHeader(OutputStream outputStream) throws IOException { outputStream.write((byte)getPacketTypeCode()); outputStream.write(getProtocolVersion()); } }
/** * Creates a {@link DataPacket} object from its byte array representation.<br/> * @param data * @throws MalformedPacketException If the byte array does not contain a valid <code>DataPacket</code>. */ public static DataPacket createPacket(byte[] data) throws MalformedPacketException { if (data == null) throw new MalformedPacketException("Packet data is null"); if (data.length < HEADER_LENGTH) throw new MalformedPacketException("Packet is too short to be a DataPacket"); char packetTypeCode = (char)data[0]; // first byte of a data packet is the packet type code Class<? extends I2PBotePacket> packetType = decodePacketTypeCode(packetTypeCode); if (packetType==null || !DataPacket.class.isAssignableFrom(packetType)) throw new MalformedPacketException("Type code is not a DataPacket type code: <" + packetTypeCode + ">"); Class<? extends DataPacket> dataPacketType = packetType.asSubclass(DataPacket.class); DataPacket packet = null; try { packet = dataPacketType.getConstructor(byte[].class).newInstance(data); } catch (Exception e) { throw new MalformedPacketException("Can't instantiate packet for type code <" + packetTypeCode + ">", e); } if (!packet.isProtocolVersionOk()) throw new MalformedPacketException("Incorrect protocol version: " + packet.getProtocolVersion() + ", packet: " + packet); return packet; }
@Override public byte[] toByteArray() { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); DataOutputStream dataStream = new DataOutputStream(byteStream); try { writeHeader(dataStream); dataStream.write(statusCode.ordinal()); if (payload == null) dataStream.writeShort(0); else { byte[] payloadBytes = payload.toByteArray(); dataStream.writeShort(payloadBytes.length); dataStream.write(payloadBytes); } } catch (IOException e) { log.error("Can't write to ByteArrayOutputStream.", e); } return byteStream.toByteArray(); }
/** * * @param payload Can be <code>null</code>. * @param statusCode * @param packetId */ public static Collection<ResponsePacket> create(DataPacket payload, StatusCode statusCode, UniqueId packetId) { if (payload instanceof Splittable && payload.isTooBig()) { Collection<? extends DataPacket> dataPackets = ((Splittable)payload).split(); ArrayList<ResponsePacket> responsePackets = new ArrayList<ResponsePacket>(); for (DataPacket dataPacket: dataPackets) responsePackets.add(new ResponsePacket(dataPacket, statusCode, packetId)); return responsePackets; } else return Collections.singleton(new ResponsePacket(payload, statusCode, packetId)); }
/** * Loads a <code>DhtStorablePacket</code> from a file.<br/> * Returns <code>null</code> if the file doesn't exist, or if * an error occurred. * @param file * @throws MalformedPacketException */ public static DhtStorablePacket createPacket(File file) throws MalformedPacketException { if (file==null || !file.exists()) return null; DataPacket dataPacket; dataPacket = DataPacket.createPacket(file); if (dataPacket instanceof DhtStorablePacket) return (DhtStorablePacket)dataPacket; else { log.warn("Expected: DhtStorablePacket, got: " + (dataPacket==null?"<null>":dataPacket.getClass().getSimpleName())); return null; } } }
private Collection<UnencryptedEmailPacket> getEmailPackets(File[] files) { Collection<UnencryptedEmailPacket> packets = new ArrayList<UnencryptedEmailPacket>(); for (File file: files) { try { I2PBotePacket packet = DataPacket.createPacket(file); if (packet instanceof UnencryptedEmailPacket) packets.add((UnencryptedEmailPacket)packet); else log.error("Non-Email Packet found in the IncompleteEmailFolder, file: <" + file.getAbsolutePath() + ">"); } catch (MalformedPacketException e) { log.error("Cannot create packet from file: <" + file.getAbsolutePath() + ">", e); } } return packets; } }
public ResponsePacket(byte[] data) { super(data); ByteBuffer buffer = ByteBuffer.wrap(data, HEADER_LENGTH, data.length-HEADER_LENGTH); statusCode = StatusCode.values()[buffer.get()]; int payloadLength = buffer.getShort() & 0xFFFF; if (payloadLength > 0) { byte[] payloadData = new byte[payloadLength]; buffer.get(payloadData); try { payload = DataPacket.createPacket(payloadData); } catch (MalformedPacketException e) { payload = new MalformedDataPacket(); } } if (buffer.hasRemaining()) log.debug("Response Packet has " + buffer.remaining() + " extra bytes."); }
/** * Creates an <code>I2PBotePacket</code> from its byte array representation.<br/> * The header bytes determine whether <code>DataPacket</code> or a <code>CommunicationPacket</code> is created. * @param data * @throws MalformedPacketException If the byte array does not contain a valid <code>I2PBotePacket</code>. */ private static I2PBotePacket createPacket(byte[] data) throws MalformedPacketException { if (CommunicationPacket.isPrefixValid(data)) return CommunicationPacket.createPacket(data); else return DataPacket.createPacket(data); }
/** * Creates a <code>DeletionInfoPacket</code> from a file. If the file * does not exist, or an error occurs, <code>null</code> is returned. * @param delFileName */ protected DeletionInfoPacket createDelInfoPacket(String delFileName) { File delFile = new File(storageDir, delFileName); try { DataPacket dataPacket = DataPacket.createPacket(delFile); if (dataPacket instanceof DeletionInfoPacket) return (DeletionInfoPacket)dataPacket; else if (dataPacket == null) return null; else { log.error("Not a Deletion Info Packet file: <" + delFile.getAbsolutePath() + ">"); return null; } } catch (MalformedPacketException e) { log.error("Cannot read Deletion Info Packet,: <" + delFile.getAbsolutePath() + ">", e); return null; } }
/** * Creates a {@link #DataPacket} object from a file, using the same format as the * {@link #createPacket(byte[])} method. * @param file * @throws MalformedPacketException */ public static DataPacket createPacket(File file) throws MalformedPacketException { if (file==null || !file.exists()) return null; InputStream inputStream = null; try { inputStream = new FileInputStream(file); DataPacket packet = createPacket(Util.readBytes(inputStream)); return packet; } catch (IOException e) { throw new MalformedPacketException("Can't read packet file: " + file.getAbsolutePath(), e); } finally { if (inputStream != null) try { inputStream.close(); } catch (IOException e) { log.error("Can't close stream.", e); } } }
assertEquals(1, files.length); assertTrue(files[0].getName().startsWith("DEL_")); DataPacket dataPacket = DataPacket.createPacket(files[0]); assertTrue(dataPacket instanceof DeletionInfoPacket); DeletionInfoPacket delInfoPacket = (DeletionInfoPacket)dataPacket;
DataPacket dataPacket = DataPacket.createPacket(files[0]); assertTrue(dataPacket instanceof DeletionInfoPacket); DeletionInfoPacket delInfoPacket = (DeletionInfoPacket)dataPacket;