comparison src/org/tmatesoft/hg/internal/EncodingHelper.java @ 415:ee8264d80747

Explicit constant for regular file flags, access to flags for a given file revision
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 22 Mar 2012 18:54:11 +0100
parents 63c5a9d7ca3f
children 528b6780a8bd
comparison
equal deleted inserted replaced
414:bb278ccf9866 415:ee8264d80747
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.nio.ByteBuffer; 19 import java.nio.ByteBuffer;
20 import java.nio.CharBuffer;
20 import java.nio.charset.CharacterCodingException; 21 import java.nio.charset.CharacterCodingException;
21 import java.nio.charset.Charset; 22 import java.nio.charset.Charset;
22 import java.nio.charset.CharsetDecoder; 23 import java.nio.charset.CharsetDecoder;
23 import java.nio.charset.CharsetEncoder; 24 import java.nio.charset.CharsetEncoder;
24 25
26 import org.tmatesoft.hg.core.SessionContext;
27
25 /** 28 /**
26 * Keep all encoding-related issues in the single place 29 * Keep all encoding-related issues in the single place
30 * NOT thread-safe (encoder and decoder requires synchronized access)
27 * @author Artem Tikhomirov 31 * @author Artem Tikhomirov
28 * @author TMate Software Ltd. 32 * @author TMate Software Ltd.
29 */ 33 */
30 public class EncodingHelper { 34 public class EncodingHelper {
31 // XXX perhaps, shall not be full of statics, but rather an instance coming from e.g. HgRepository? 35 // XXX perhaps, shall not be full of statics, but rather an instance coming from e.g. HgRepository?
32 /* 36 /*
33 * To understand what Mercurial thinks of UTF-8 and Unix byte approach to names, see 37 * 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 38 * http://mercurial.808500.n3.nabble.com/Unicode-support-request-td3430704.html
35 */ 39 */
36 40
41 private final SessionContext sessionContext;
37 private final CharsetEncoder encoder; 42 private final CharsetEncoder encoder;
38 private final CharsetDecoder decoder; 43 private final CharsetDecoder decoder;
39 44
40 EncodingHelper(Charset fsEncoding) { 45 EncodingHelper(Charset fsEncoding, SessionContext ctx) {
46 sessionContext = ctx;
41 decoder = fsEncoding.newDecoder(); 47 decoder = fsEncoding.newDecoder();
42 encoder = fsEncoding.newEncoder(); 48 encoder = fsEncoding.newEncoder();
43 } 49 }
44 50
45 public String fromManifest(byte[] data, int start, int length) { 51 public String fromManifest(byte[] data, int start, int length) {
46 try { 52 try {
47 return decoder.decode(ByteBuffer.wrap(data, start, length)).toString(); 53 return decoder.decode(ByteBuffer.wrap(data, start, length)).toString();
48 } catch (CharacterCodingException ex) { 54 } catch (CharacterCodingException ex) {
55 sessionContext.getLog().error(getClass(), ex, String.format("Use of charset %s failed, resort to system default", charset().name()));
49 // resort to system-default 56 // resort to system-default
50 return new String(data, start, length); 57 return new String(data, start, length);
51 } 58 }
52 } 59 }
53 60
54 public String fromDirstate(byte[] data, int start, int length) throws CharacterCodingException { 61 /**
62 * @return byte representation of the string directly comparable to bytes in manifest
63 */
64 public byte[] toManifest(String s) {
65 if (s == null) {
66 // perhaps, can return byte[0] in this case?
67 throw new IllegalArgumentException();
68 }
69 try {
70 // synchonized(encoder) {
71 ByteBuffer bb = encoder.encode(CharBuffer.wrap(s));
72 // }
73 byte[] rv = new byte[bb.remaining()];
74 bb.get(rv, 0, rv.length);
75 return rv;
76 } catch (CharacterCodingException ex) {
77 sessionContext.getLog().error(getClass(), ex, String.format("Use of charset %s failed, resort to system default", charset().name()));
78 // resort to system-default
79 return s.getBytes();
80 }
81 }
82
83 public String fromDirstate(byte[] data, int start, int length) throws CharacterCodingException { // FIXME perhaps, log is enough, and charset() may be private?
55 return decoder.decode(ByteBuffer.wrap(data, start, length)).toString(); 84 return decoder.decode(ByteBuffer.wrap(data, start, length)).toString();
56 } 85 }
57 86
58 public Charset charset() { 87 public Charset charset() {
59 return encoder.charset(); 88 return encoder.charset();
60 } 89 }
90
61 } 91 }