1 /* 2 * Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"). 5 * You may not use this file except in compliance with the License. 6 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 package software.amazon.eventstream; 16 17 import java.io.DataOutputStream; 18 import java.io.IOException; 19 import java.nio.ByteBuffer; 20 import java.util.Map; 21 22 import static java.util.Objects.requireNonNull; 23 24 class Header { 25 private final String name; 26 private final HeaderValue value; 27 Header(String name, HeaderValue value)28 Header(String name, HeaderValue value) { 29 this.name = requireNonNull(name); 30 this.value = requireNonNull(value); 31 } 32 Header(String name, String value)33 Header(String name, String value) { 34 this(name, HeaderValue.fromString(value)); 35 } 36 getName()37 public String getName() { 38 return name; 39 } 40 getValue()41 public HeaderValue getValue() { 42 return value; 43 } 44 decode(ByteBuffer buf)45 static Header decode(ByteBuffer buf) { 46 String name = Utils.readShortString(buf); 47 return new Header(name, HeaderValue.decode(buf)); 48 } 49 encode(Map.Entry<String, HeaderValue> header, DataOutputStream dos)50 static void encode(Map.Entry<String, HeaderValue> header, DataOutputStream dos) throws IOException { 51 new Header(header.getKey(), header.getValue()).encode(dos); 52 } 53 encode(DataOutputStream dos)54 void encode(DataOutputStream dos) throws IOException { 55 Utils.writeShortString(dos, name); 56 value.encode(dos); 57 } 58 59 @Override equals(Object o)60 public boolean equals(Object o) { 61 if (this == o) return true; 62 if (o == null || getClass() != o.getClass()) return false; 63 64 Header header = (Header) o; 65 66 if (!name.equals(header.name)) return false; 67 return value.equals(header.value); 68 } 69 70 @Override hashCode()71 public int hashCode() { 72 int result = name.hashCode(); 73 result = 31 * result + value.hashCode(); 74 return result; 75 } 76 77 @Override toString()78 public String toString() { 79 return "Header{" 80 + "name='" + name + '\'' 81 + ", value=" + value 82 + '}'; 83 } 84 } 85