You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultCodecProvider.java 3.9KB

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