Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

MessageEncoder.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc;
  23. import com.dmdirc.events.UserErrorEvent;
  24. import com.dmdirc.interfaces.Connection;
  25. import com.dmdirc.interfaces.GroupChat;
  26. import com.dmdirc.logger.ErrorLevel;
  27. import com.dmdirc.parser.interfaces.Encoder;
  28. import com.dmdirc.parser.interfaces.Parser;
  29. import java.io.UnsupportedEncodingException;
  30. /**
  31. * An {@link Encoder} implementation that reads the desired encoding from the relevant target's
  32. * config file.
  33. */
  34. public class MessageEncoder implements Encoder {
  35. /** The connection that owns this encoder. */
  36. private final Connection connection;
  37. /** The parser that this encoder will work for. */
  38. private final Parser parser;
  39. /** The event bus to post errors to. */
  40. private final DMDircMBassador eventBus;
  41. /**
  42. * Creates a new instance of {@link MessageEncoder}.
  43. *
  44. * @param connection The connection that owns this encoder
  45. * @param parser The parser that this encoder will work for
  46. * @param eventBus The event bus to post errors to.
  47. */
  48. public MessageEncoder(final Connection connection, final Parser parser,
  49. final DMDircMBassador eventBus) {
  50. this.connection = connection;
  51. this.parser = parser;
  52. this.eventBus = eventBus;
  53. }
  54. @Override
  55. public String encode(final String source, final String target,
  56. final byte[] message, final int offset, final int length) {
  57. String encoding = connection.getWindowModel().getConfigManager()
  58. .getOption("general", "encoding");
  59. if (target != null && parser.isValidChannelName(target)) {
  60. encoding = connection.getChannel(target)
  61. .map(GroupChat::getWindowModel)
  62. .map(FrameContainer::getConfigManager)
  63. .map(cm -> cm.getOption("general", "encoding"))
  64. .orElse(encoding);
  65. }
  66. try {
  67. return new String(message, offset, length, encoding);
  68. } catch (UnsupportedEncodingException ex) {
  69. eventBus.publishAsync(new UserErrorEvent(ErrorLevel.MEDIUM, ex,
  70. "Unsupported character encoding: " + encoding, ""));
  71. return new String(message, offset, length);
  72. }
  73. }
  74. }