View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.client;
20  
21  import java.nio.ByteBuffer;
22  
23  /**
24   * {@link ContentDecoder} decodes content bytes of a response.
25   *
26   * @see Factory
27   */
28  public interface ContentDecoder
29  {
30      /**
31       * <p>Decodes the bytes in the given {@code buffer} and returns decoded bytes, if any.</p>
32       *
33       * @param buffer the buffer containing encoded bytes
34       * @return a buffer containing decoded bytes, if any
35       */
36      public abstract ByteBuffer decode(ByteBuffer buffer);
37  
38      /**
39       * Factory for {@link ContentDecoder}s; subclasses must implement {@link #newContentDecoder()}.
40       * <p>
41       * {@link Factory} have an {@link #getEncoding() encoding}, which is the string used in
42       * {@code Accept-Encoding} request header and in {@code Content-Encoding} response headers.
43       * <p>
44       * {@link Factory} instances are configured in {@link HttpClient} via
45       * {@link HttpClient#getContentDecoderFactories()}.
46       */
47      public static abstract class Factory
48      {
49          private final String encoding;
50  
51          protected Factory(String encoding)
52          {
53              this.encoding = encoding;
54          }
55  
56          /**
57           * @return the type of the decoders created by this factory
58           */
59          public String getEncoding()
60          {
61              return encoding;
62          }
63  
64          @Override
65          public boolean equals(Object obj)
66          {
67              if (this == obj) return true;
68              if (!(obj instanceof Factory)) return false;
69              Factory that = (Factory)obj;
70              return encoding.equals(that.encoding);
71          }
72  
73          @Override
74          public int hashCode()
75          {
76              return encoding.hashCode();
77          }
78  
79          /**
80           * Factory method for {@link ContentDecoder}s
81           *
82           * @return a new instance of a {@link ContentDecoder}
83           */
84          public abstract ContentDecoder newContentDecoder();
85      }
86  }