|
| 1 | +package jooby.internal; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.io.Reader; |
| 7 | +import java.nio.ByteBuffer; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Optional; |
| 10 | +import java.util.Set; |
| 11 | +import java.util.SortedSet; |
| 12 | + |
| 13 | +import jooby.Response; |
| 14 | +import jooby.Route; |
| 15 | +import jooby.Variant; |
| 16 | + |
| 17 | +import com.google.common.base.Charsets; |
| 18 | +import com.google.inject.TypeLiteral; |
| 19 | + |
| 20 | +public class WebSocketBinaryMessage implements Variant { |
| 21 | + |
| 22 | + private ByteBuffer buffer; |
| 23 | + |
| 24 | + public WebSocketBinaryMessage(final ByteBuffer buffer) { |
| 25 | + this.buffer = buffer; |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public boolean booleanValue() { |
| 30 | + throw typeError(boolean.class); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public byte byteValue() { |
| 35 | + throw typeError(byte.class); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public short shortValue() { |
| 40 | + throw typeError(short.class); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public int intValue() { |
| 45 | + throw typeError(int.class); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public long longValue() { |
| 50 | + throw typeError(long.class); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public String stringValue() { |
| 55 | + throw typeError(String.class); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public float floatValue() { |
| 60 | + throw typeError(float.class); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public double doubleValue() { |
| 65 | + throw typeError(double.class); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public <T extends Enum<T>> T enumValue(final Class<T> type) { |
| 70 | + throw typeError(type); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public <T> List<T> toList(final Class<T> type) { |
| 75 | + throw typeError(type); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public <T> Set<T> toSet(final Class<T> type) { |
| 80 | + throw typeError(type); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public <T extends Comparable<T>> SortedSet<T> toSortedSet(final Class<T> type) { |
| 85 | + throw typeError(type); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public <T> Optional<T> toOptional(final Class<T> type) { |
| 90 | + throw typeError(type); |
| 91 | + } |
| 92 | + |
| 93 | + @SuppressWarnings("unchecked") |
| 94 | + @Override |
| 95 | + public <T> T to(final TypeLiteral<T> type) { |
| 96 | + Class<? super T> rawType = type.getRawType(); |
| 97 | + if (rawType == byte[].class) { |
| 98 | + return (T) buffer.array(); |
| 99 | + } |
| 100 | + if (rawType == ByteBuffer.class) { |
| 101 | + return (T) buffer; |
| 102 | + } |
| 103 | + if (rawType == InputStream.class) { |
| 104 | + return (T) new ByteArrayInputStream(buffer.array()); |
| 105 | + } |
| 106 | + if (rawType == Reader.class) { |
| 107 | + return (T) new InputStreamReader(new ByteArrayInputStream(buffer.array()), Charsets.UTF_8); |
| 108 | + } |
| 109 | + throw typeError(rawType); |
| 110 | + } |
| 111 | + |
| 112 | + private Route.Err typeError(final Class<?> type) { |
| 113 | + return new Route.Err(Response.Status.BAD_REQUEST, "Can't convert to " |
| 114 | + + ByteBuffer.class.getName() + " to " + type); |
| 115 | + } |
| 116 | +} |
0 commit comments