Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

MessageEncoder.java 3.1KB

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