mirror of
				https://github.com/Qortal/altcoinj.git
				synced 2025-11-03 05:57:21 +00:00 
			
		
		
		
	deduplicated and simplifed Message methods that read underlying byte arrays
This commit is contained in:
		
				
					committed by
					
						
						Mike Hearn
					
				
			
			
				
	
			
			
			
						parent
						
							9c8f6fbb20
						
					
				
				
					commit
					c4c33ce978
				
			@@ -357,7 +357,7 @@ public abstract class Message implements Serializable {
 | 
				
			|||||||
     * @param stream
 | 
					     * @param stream
 | 
				
			||||||
     * @throws IOException
 | 
					     * @throws IOException
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    final public void bitcoinSerialize(OutputStream stream) throws IOException {
 | 
					    public final void bitcoinSerialize(OutputStream stream) throws IOException {
 | 
				
			||||||
        // 1st check for cached bytes.
 | 
					        // 1st check for cached bytes.
 | 
				
			||||||
        if (payload != null && length != UNKNOWN_LENGTH) {
 | 
					        if (payload != null && length != UNKNOWN_LENGTH) {
 | 
				
			||||||
            stream.write(payload, offset, length);
 | 
					            stream.write(payload, offset, length);
 | 
				
			||||||
@@ -407,19 +407,6 @@ public abstract class Message implements Serializable {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Sha256Hash readHash() throws ProtocolException {
 | 
					 | 
				
			||||||
        try {
 | 
					 | 
				
			||||||
            byte[] hash = new byte[32];
 | 
					 | 
				
			||||||
            System.arraycopy(payload, cursor, hash, 0, 32);
 | 
					 | 
				
			||||||
            cursor += 32;
 | 
					 | 
				
			||||||
            // We have to flip it around, as it's been read off the wire in little endian.
 | 
					 | 
				
			||||||
            // Not the most efficient way to do this but the clearest.
 | 
					 | 
				
			||||||
            return Sha256Hash.wrapReversed(hash);
 | 
					 | 
				
			||||||
        } catch (IndexOutOfBoundsException e) {
 | 
					 | 
				
			||||||
            throw new ProtocolException(e);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    long readInt64() throws ProtocolException {
 | 
					    long readInt64() throws ProtocolException {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            long u = Utils.readInt64(payload, cursor);
 | 
					            long u = Utils.readInt64(payload, cursor);
 | 
				
			||||||
@@ -431,16 +418,8 @@ public abstract class Message implements Serializable {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    BigInteger readUint64() throws ProtocolException {
 | 
					    BigInteger readUint64() throws ProtocolException {
 | 
				
			||||||
        try {
 | 
					        // Java does not have an unsigned 64 bit type. So scrape it off the wire then flip.
 | 
				
			||||||
            // Java does not have an unsigned 64 bit type. So scrape it off the wire then flip.
 | 
					        return new BigInteger(Utils.reverseBytes(readBytes(8)));
 | 
				
			||||||
            byte[] valbytes = new byte[8];
 | 
					 | 
				
			||||||
            System.arraycopy(payload, cursor, valbytes, 0, 8);
 | 
					 | 
				
			||||||
            valbytes = Utils.reverseBytes(valbytes);
 | 
					 | 
				
			||||||
            cursor += valbytes.length;
 | 
					 | 
				
			||||||
            return new BigInteger(valbytes);
 | 
					 | 
				
			||||||
        } catch (IndexOutOfBoundsException e) {
 | 
					 | 
				
			||||||
            throw new ProtocolException(e);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    long readVarInt() throws ProtocolException {
 | 
					    long readVarInt() throws ProtocolException {
 | 
				
			||||||
@@ -459,7 +438,7 @@ public abstract class Message implements Serializable {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    byte[] readBytes(int length) throws ProtocolException {
 | 
					    byte[] readBytes(int length) throws ProtocolException {
 | 
				
			||||||
        if (length > MAX_SIZE) {
 | 
					        if (length > MAX_SIZE) {
 | 
				
			||||||
            throw new ProtocolException("Claimed byte array length too large: " + length);
 | 
					            throw new ProtocolException("Claimed value length too large: " + length);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            byte[] b = new byte[length];
 | 
					            byte[] b = new byte[length];
 | 
				
			||||||
@@ -477,31 +456,23 @@ public abstract class Message implements Serializable {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    String readStr() throws ProtocolException {
 | 
					    String readStr() throws ProtocolException {
 | 
				
			||||||
 | 
					        long length = readVarInt();
 | 
				
			||||||
 | 
					        if (length == 0) {
 | 
				
			||||||
 | 
					            return ""; // a little optimization
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            VarInt varInt = new VarInt(payload, cursor);
 | 
					            return new String(readBytes((int) length), "UTF-8");
 | 
				
			||||||
            if (varInt.value == 0) {
 | 
					        } catch (UnsupportedEncodingException e) {
 | 
				
			||||||
                cursor += 1;
 | 
					            throw new RuntimeException(e);  // Cannot happen, UTF-8 is always supported.
 | 
				
			||||||
                return "";
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            cursor += varInt.getOriginalSizeInBytes();
 | 
					 | 
				
			||||||
            if (varInt.value > MAX_SIZE) {
 | 
					 | 
				
			||||||
                throw new ProtocolException("Claimed var_str length too large: " + varInt.value);
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            byte[] characters = new byte[(int) varInt.value];
 | 
					 | 
				
			||||||
            System.arraycopy(payload, cursor, characters, 0, characters.length);
 | 
					 | 
				
			||||||
            cursor += characters.length;
 | 
					 | 
				
			||||||
            try {
 | 
					 | 
				
			||||||
                return new String(characters, "UTF-8");
 | 
					 | 
				
			||||||
            } catch (UnsupportedEncodingException e) {
 | 
					 | 
				
			||||||
                throw new RuntimeException(e);  // Cannot happen, UTF-8 is always supported.
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        } catch (ArrayIndexOutOfBoundsException e) {
 | 
					 | 
				
			||||||
            throw new ProtocolException(e);
 | 
					 | 
				
			||||||
        } catch (IndexOutOfBoundsException e) {
 | 
					 | 
				
			||||||
            throw new ProtocolException(e);
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					
 | 
				
			||||||
 | 
					    Sha256Hash readHash() throws ProtocolException {
 | 
				
			||||||
 | 
					        // We have to flip it around, as it's been read off the wire in little endian.
 | 
				
			||||||
 | 
					        // Not the most efficient way to do this but the clearest.
 | 
				
			||||||
 | 
					        return Sha256Hash.wrapReversed(readBytes(32));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    boolean hasMoreBytes() {
 | 
					    boolean hasMoreBytes() {
 | 
				
			||||||
        return cursor < payload.length;
 | 
					        return cursor < payload.length;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user