View Javadoc
1   /*
2   Copyright (c) 2010 James Ahlborn
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   You may obtain a copy of the License at
7   
8       http://www.apache.org/licenses/LICENSE-2.0
9   
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16  
17  package com.healthmarketscience.jackcess.impl;
18  
19  import java.io.IOException;
20  import java.nio.ByteBuffer;
21  import java.nio.charset.Charset;
22  
23  /**
24   * Default implementation of CodecProvider which does not have any actual
25   * encoding/decoding support.  See {@link CodecProvider} for details on a more
26   * useful implementation.
27   *
28   * @author James Ahlborn
29   */
30  public class DefaultCodecProvider implements CodecProvider
31  {
32    /** common instance of DefaultCodecProvider */
33    public static final CodecProvider INSTANCE = 
34      new DefaultCodecProvider();
35  
36    /** common instance of {@link DummyHandler} */
37    public static final CodecHandler DUMMY_HANDLER = 
38      new DummyHandler();
39  
40    /** common instance of {@link UnsupportedHandler} */
41    public static final CodecHandler UNSUPPORTED_HANDLER = 
42      new UnsupportedHandler();
43  
44  
45    /**
46     * {@inheritDoc}
47     * <p>
48     * This implementation returns DUMMY_HANDLER for databases with no encoding
49     * and UNSUPPORTED_HANDLER for databases with any encoding.
50     */
51    public CodecHandler createHandler(PageChannel channel, Charset charset)
52      throws IOException
53    {
54      JetFormat format = channel.getFormat();
55      switch(format.CODEC_TYPE) {
56      case NONE:
57        // no encoding, all good
58        return DUMMY_HANDLER;
59  
60      case JET:
61      case OFFICE:
62        // check for an encode key.  if 0, not encoded
63        ByteBuffer bb = channel.createPageBuffer();
64        channel.readPage(bb, 0);
65        int codecKey = bb.getInt(format.OFFSET_ENCODING_KEY);
66        return((codecKey == 0) ? DUMMY_HANDLER : UNSUPPORTED_HANDLER);
67  
68      case MSISAM:
69        // always encoded, we don't handle it
70        return UNSUPPORTED_HANDLER;
71  
72      default:
73        throw new RuntimeException("Unknown codec type " + format.CODEC_TYPE);
74      }
75    }
76  
77    /**
78     * CodecHandler implementation which does nothing, useful for databases with
79     * no extra encoding.
80     */
81    public static class DummyHandler implements CodecHandler
82    {
83      public boolean canEncodePartialPage() {
84        return true;
85      }
86  
87      public boolean canDecodeInline() {
88        return true;
89      }
90  
91      public void decodePage(ByteBuffer inPage, ByteBuffer outPage,
92                             int pageNumber) 
93        throws IOException 
94      {
95        // does nothing
96      }
97  
98      public ByteBuffer encodePage(ByteBuffer page, int pageNumber, 
99                                   int pageOffset) 
100       throws IOException
101     {
102       // does nothing
103       return page;
104     }
105   }
106 
107   /**
108    * CodecHandler implementation which always throws
109    * UnsupportedCodecException, useful for databases with unsupported
110    * encodings.
111    */
112   public static class UnsupportedHandler implements CodecHandler
113   {
114     public boolean canEncodePartialPage() {
115       return true;
116     }
117 
118     public boolean canDecodeInline() {
119       return true;
120     }
121 
122     public void decodePage(ByteBuffer inPage, ByteBuffer outPage, 
123                            int pageNumber) 
124       throws IOException 
125     {
126       throw new UnsupportedCodecException("Decoding not supported.  Please choose a CodecProvider which supports reading the current database encoding.");
127     }
128 
129     public ByteBuffer encodePage(ByteBuffer page, int pageNumber, 
130                                  int pageOffset) 
131       throws IOException
132     {
133       throw new UnsupportedCodecException("Encoding not supported.  Please choose a CodecProvider which supports writing the current database encoding.");
134     }
135   }
136 
137 }