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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. Copyright (c) 2010 James Ahlborn
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  14. USA
  15. */
  16. package com.healthmarketscience.jackcess;
  17. import java.io.IOException;
  18. import java.nio.ByteBuffer;
  19. import java.nio.charset.Charset;
  20. /**
  21. * Default implementation of CodecProvider which does not have any actual
  22. * encoding/decoding support. See {@link CodecProvider} for details on a more
  23. * useful implementation.
  24. *
  25. * @author James Ahlborn
  26. */
  27. public class DefaultCodecProvider implements CodecProvider
  28. {
  29. /** common instance of DefaultCodecProvider */
  30. public static final CodecProvider INSTANCE =
  31. new DefaultCodecProvider();
  32. /** common instance of {@link DummyHandler} */
  33. public static final CodecHandler DUMMY_HANDLER =
  34. new DummyHandler();
  35. /** common instance of {@link UnsupportedHandler} */
  36. public static final CodecHandler UNSUPPORTED_HANDLER =
  37. new UnsupportedHandler();
  38. /**
  39. * {@inheritDoc}
  40. * <p>
  41. * This implementation returns DUMMY_HANDLER for databases with no encoding
  42. * and UNSUPPORTED_HANDLER for databases with any encoding.
  43. */
  44. public CodecHandler createHandler(PageChannel channel, Charset charset)
  45. throws IOException
  46. {
  47. JetFormat format = channel.getFormat();
  48. switch(format.CODEC_TYPE) {
  49. case NONE:
  50. // no encoding, all good
  51. return DUMMY_HANDLER;
  52. case JET:
  53. case OFFICE:
  54. // check for an encode key. if 0, not encoded
  55. ByteBuffer bb = channel.createPageBuffer();
  56. channel.readPage(bb, 0);
  57. int codecKey = bb.getInt(format.OFFSET_ENCODING_KEY);
  58. return((codecKey == 0) ? DUMMY_HANDLER : UNSUPPORTED_HANDLER);
  59. case MSISAM:
  60. // always encoded, we don't handle it
  61. return UNSUPPORTED_HANDLER;
  62. default:
  63. throw new RuntimeException("Unknown codec type " + format.CODEC_TYPE);
  64. }
  65. }
  66. /**
  67. * CodecHandler implementation which does nothing, useful for databases with
  68. * no extra encoding.
  69. */
  70. public static class DummyHandler implements CodecHandler
  71. {
  72. public void decodePage(ByteBuffer page, int pageNumber) throws IOException
  73. {
  74. // does nothing
  75. }
  76. public ByteBuffer encodePage(ByteBuffer page, int pageNumber,
  77. int pageOffset)
  78. throws IOException
  79. {
  80. // does nothing
  81. return page;
  82. }
  83. }
  84. /**
  85. * CodecHandler implementation which always throws
  86. * UnsupportedCodecException, useful for databases with unsupported
  87. * encodings.
  88. */
  89. public static class UnsupportedHandler implements CodecHandler
  90. {
  91. public void decodePage(ByteBuffer page, int pageNumber) throws IOException
  92. {
  93. throw new UnsupportedCodecException("Decoding not supported. Please choose a CodecProvider which supports reading the current database encoding.");
  94. }
  95. public ByteBuffer encodePage(ByteBuffer page, int pageNumber,
  96. int pageOffset)
  97. throws IOException
  98. {
  99. throw new UnsupportedCodecException("Encoding not supported. Please choose a CodecProvider which supports writing the current database encoding.");
  100. }
  101. }
  102. }