comparison src/org/tmatesoft/hg/internal/EncodingHelper.java @ 414:bb278ccf9866

Pull changes from smartgit3 branch
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 21 Mar 2012 20:51:12 +0100
parents 63c5a9d7ca3f
children ee8264d80747
comparison
equal deleted inserted replaced
413:7f27122011c3 414:bb278ccf9866
1 /* 1 /*
2 * Copyright (c) 2011 TMate Software Ltd 2 * Copyright (c) 2011-2012 TMate Software Ltd
3 * 3 *
4 * This program is free software; you can redistribute it and/or modify 4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by 5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License. 6 * the Free Software Foundation; version 2 of the License.
7 * 7 *
14 * the terms of a license other than GNU General Public License 14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com 15 * contact TMate Software at support@hg4j.com
16 */ 16 */
17 package org.tmatesoft.hg.internal; 17 package org.tmatesoft.hg.internal;
18 18
19 import java.io.UnsupportedEncodingException; 19 import java.nio.ByteBuffer;
20 20 import java.nio.charset.CharacterCodingException;
21 import org.tmatesoft.hg.core.HgBadStateException; 21 import java.nio.charset.Charset;
22 import java.nio.charset.CharsetDecoder;
23 import java.nio.charset.CharsetEncoder;
22 24
23 /** 25 /**
24 * Keep all encoding-related issues in the single place 26 * Keep all encoding-related issues in the single place
25 * @author Artem Tikhomirov 27 * @author Artem Tikhomirov
26 * @author TMate Software Ltd. 28 * @author TMate Software Ltd.
27 */ 29 */
28 public class EncodingHelper { 30 public class EncodingHelper {
29 // XXX perhaps, shall not be full of statics, but rather an instance coming from e.g. HgRepository? 31 // XXX perhaps, shall not be full of statics, but rather an instance coming from e.g. HgRepository?
32 /*
33 * To understand what Mercurial thinks of UTF-8 and Unix byte approach to names, see
34 * http://mercurial.808500.n3.nabble.com/Unicode-support-request-td3430704.html
35 */
36
37 private final CharsetEncoder encoder;
38 private final CharsetDecoder decoder;
39
40 EncodingHelper(Charset fsEncoding) {
41 decoder = fsEncoding.newDecoder();
42 encoder = fsEncoding.newEncoder();
43 }
30 44
31 public static String fromManifest(byte[] data, int start, int length) { 45 public String fromManifest(byte[] data, int start, int length) {
32 try { 46 try {
33 return new String(data, start, length, "ISO-8859-1"); 47 return decoder.decode(ByteBuffer.wrap(data, start, length)).toString();
34 } catch (UnsupportedEncodingException ex) { 48 } catch (CharacterCodingException ex) {
35 // can't happen 49 // resort to system-default
36 throw new HgBadStateException(ex); 50 return new String(data, start, length);
37 } 51 }
38 } 52 }
53
54 public String fromDirstate(byte[] data, int start, int length) throws CharacterCodingException {
55 return decoder.decode(ByteBuffer.wrap(data, start, length)).toString();
56 }
57
58 public Charset charset() {
59 return encoder.charset();
60 }
39 } 61 }