annotate src/org/tmatesoft/hg/internal/StoragePathHelper.java @ 619:868b2ffdcd5c

Close FIS, not FileChannel, to clear both references to FileDescriptor right away
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 17 May 2013 22:04:23 +0200
parents 5e0313485eef
children
rev   line source
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
616
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 425
diff changeset
2 * Copyright (c) 2011-2013 TMate Software Ltd
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
102
a3a2e5deb320 Updated contact address to support@hg4j.com
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 83
diff changeset
15 * contact TMate Software at support@hg4j.com
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.internal;
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
19 import java.nio.ByteBuffer;
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
20 import java.nio.CharBuffer;
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
21 import java.nio.charset.Charset;
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
22 import java.nio.charset.CharsetEncoder;
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import java.util.Arrays;
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import java.util.TreeSet;
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 import org.tmatesoft.hg.util.PathRewrite;
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 /**
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 * @see http://mercurial.selenic.com/wiki/CaseFoldingPlan
80
4222b04f34ee Follow history of a file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
30 * @see http://mercurial.selenic.com/wiki/fncacheRepoFormat
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
31 * @see http://mercurial.selenic.com/wiki/EncodingStrategy
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 *
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 * @author Artem Tikhomirov
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 * @author TMate Software Ltd.
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 */
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 class StoragePathHelper implements PathRewrite {
616
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 425
diff changeset
37
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 425
diff changeset
38 static final String STR_STORE = "store/";
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 425
diff changeset
39 static final String STR_DATA = "data/";
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 425
diff changeset
40 static final String STR_DH = "dh/";
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 425
diff changeset
41
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 private final boolean store;
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 private final boolean fncache;
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 private final boolean dotencode;
616
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 425
diff changeset
45 private final EncodeDirPathHelper dirPathRewrite;
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
46 private final CharsetEncoder csEncoder;
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
47 private final char[] hexEncodedByte = new char[] {'~', '0', '0'};
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
48 private final ByteBuffer byteEncodingBuf;
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
49 private final CharBuffer charEncodingBuf;
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
50
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
51 public StoragePathHelper(boolean isStore, boolean isFncache, boolean isDotencode) {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
52 this(isStore, isFncache, isDotencode, Charset.defaultCharset());
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
53 }
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
55 public StoragePathHelper(boolean isStore, boolean isFncache, boolean isDotencode, Charset fsEncoding) {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
56 assert fsEncoding != null;
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 store = isStore;
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 fncache = isFncache;
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 dotencode = isDotencode;
616
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 425
diff changeset
60 dirPathRewrite = new EncodeDirPathHelper();
425
48f993aa2f41 FIXMEs: exceptions, javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
61 csEncoder = fsEncoding.newEncoder();
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
62 byteEncodingBuf = ByteBuffer.allocate(Math.round(csEncoder.maxBytesPerChar()) + 1/*in fact, need ceil, hence +1*/);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
63 charEncodingBuf = CharBuffer.allocate(1);
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 }
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65
418
528b6780a8bd A bit of FIXME cleanup (mostly degraded to TODO post 1.0), comments and javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 411
diff changeset
66 /**
528b6780a8bd A bit of FIXME cleanup (mostly degraded to TODO post 1.0), comments and javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 411
diff changeset
67 * path argument is repository-relative name of the user's file.
528b6780a8bd A bit of FIXME cleanup (mostly degraded to TODO post 1.0), comments and javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 411
diff changeset
68 * It has to be normalized (slashes) and shall not include extension .i or .d.
528b6780a8bd A bit of FIXME cleanup (mostly degraded to TODO post 1.0), comments and javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 411
diff changeset
69 */
292
a415fe296a50 Refactor PathRewrite to accept any char sequence, not only string
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 138
diff changeset
70 public CharSequence rewrite(CharSequence p) {
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
71 final String reservedChars = "\\:*?\"<>|";
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72
616
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 425
diff changeset
73 CharSequence path = dirPathRewrite.rewrite(p);
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
74
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 StringBuilder sb = new StringBuilder(path.length() << 1);
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 if (store || fncache) {
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 for (int i = 0; i < path.length(); i++) {
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 final char ch = path.charAt(i);
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 if (ch >= 'a' && ch <= 'z') {
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 sb.append(ch); // POIRAE
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 } else if (ch >= 'A' && ch <= 'Z') {
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 sb.append('_');
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 sb.append(Character.toLowerCase(ch)); // Perhaps, (char) (((int) ch) + 32)? Even better, |= 0x20?
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
84 } else if (reservedChars.indexOf(ch) != -1) {
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
85 sb.append(toHexByte(ch));
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 } else if ((ch >= '~' /*126*/ && ch <= 255) || ch < ' ' /*32*/) {
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
87 sb.append(toHexByte(ch));
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 } else if (ch == '_') {
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 sb.append('_');
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 sb.append('_');
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 } else {
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
92 // either ASCII char that doesn't require special handling, or an Unicode character to get encoded
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
93 // according to filesystem/native encoding, see http://mercurial.selenic.com/wiki/EncodingStrategy
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
94 // despite of what the page says, use of native encoding seems worst solution to me (repositories
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
95 // can't be easily shared between OS'es with different encodings then, e.g. Win1251 and Linux UTF8).
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
96 // If the ease of sharing was not the point, what's the reason to mangle with names at all then (
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
97 // lowercase and exclude reserved device names).
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
98 if (ch < '~' /*126*/ || !csEncoder.canEncode(ch)) {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
99 sb.append(ch);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
100 } else {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
101 appendEncoded(sb, ch);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
102 }
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103 }
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
104 }
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
105 // auxencode
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
106 if (fncache) {
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
107 encodeWindowsDeviceNames(sb);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
108 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
109 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
110 final int MAX_PATH_LEN = 120;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
111 if (fncache && (sb.length() + STR_DATA.length() + ".i".length() > MAX_PATH_LEN)) {
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
112 // TODO [post-1.0] Mercurial uses system encoding for paths, hence we need to pass bytes to DigestHelper
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
113 // to ensure our sha1 value (default encoding of unicode string if one looks into DH impl) match that
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
114 // produced by Mercurial (based on native string).
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
115 String digest = new DigestHelper().sha1(STR_DATA, path, ".i").asHexString();
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
116 final int DIR_PREFIX_LEN = 8;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
117 // not sure why (-4) is here. 120 - 40 = up to 80 for path with ext. dh/ + ext(.i) = 3+2
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
118 final int MAX_DIR_PREFIX = 8 * (DIR_PREFIX_LEN + 1) - 4;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
119 sb = new StringBuilder(MAX_PATH_LEN);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
120 for (int i = 0; i < path.length(); i++) {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
121 final char ch = path.charAt(i);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
122 if (ch >= 'a' && ch <= 'z') {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
123 sb.append(ch);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
124 } else if (ch >= 'A' && ch <= 'Z') {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
125 sb.append((char) (ch | 0x20)); // lowercase
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
126 } else if (reservedChars.indexOf(ch) != -1) {
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
127 sb.append(toHexByte(ch));
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
128 } else if ((ch >= '~' /*126*/ && ch <= 255) || ch < ' ' /*32*/) {
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
129 sb.append(toHexByte(ch));
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
130 } else {
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
131 if (ch < '~' /*126*/ || !csEncoder.canEncode(ch)) {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
132 sb.append(ch);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
133 } else {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
134 appendEncoded(sb, ch);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
135 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
136 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
137 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
138 encodeWindowsDeviceNames(sb);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
139 int fnameStart = sb.lastIndexOf("/"); // since we rewrite file names, it never ends with slash (for dirs, I'd pass length-2);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
140 StringBuilder completeHashName = new StringBuilder(MAX_PATH_LEN);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
141 completeHashName.append(STR_STORE);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
142 completeHashName.append(STR_DH);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
143 if (fnameStart == -1) {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
144 // no dirs, just long filename
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
145 sb.setLength(MAX_PATH_LEN - 40 /*digest.length()*/ - STR_DH.length() - ".i".length());
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
146 completeHashName.append(sb);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
147 } else {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
148 StringBuilder sb2 = new StringBuilder(MAX_PATH_LEN);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
149 int x = 0;
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
150 do {
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
151 int i = sb.indexOf("/", x);
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
152 final int sb2Len = sb2.length();
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
153 if (i-x <= DIR_PREFIX_LEN) { // a b c d e f g h /
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
154 sb2.append(sb, x, i + 1); // with slash
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
155 } else {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
156 sb2.append(sb, x, x + DIR_PREFIX_LEN);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
157 // may unexpectedly end with bad character
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
158 final int last = sb2.length()-1;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
159 char lastChar = sb2.charAt(last);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
160 assert lastChar == sb.charAt(x + DIR_PREFIX_LEN - 1);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
161 if (lastChar == '.' || lastChar == ' ') {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
162 sb2.setCharAt(last, '_');
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
163 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
164 sb2.append('/');
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
165 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
166 if (sb2.length()-1 > MAX_DIR_PREFIX) {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
167 sb2.setLength(sb2Len); // strip off last segment, it's too much
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
168 break;
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
169 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
170 x = i+1;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
171 } while (x < fnameStart);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
172 assert sb2.charAt(sb2.length() - 1) == '/';
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
173 int left = MAX_PATH_LEN - sb2.length() - 40 /*digest.length()*/ - STR_DH.length() - ".i".length();
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
174 assert left >= 0;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
175 fnameStart++; // move from / to actual name
346
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
176 if (fnameStart + left > sb.length()) {
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
177 // there left less chars in the mangled name that we can fit
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
178 sb2.append(sb, fnameStart, sb.length());
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
179 int stillAvailable = (fnameStart+left) - sb.length();
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
180 // stillAvailable > 0;
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
181 sb2.append(".i", 0, stillAvailable > 2 ? 2 : stillAvailable);
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
182 } else {
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
183 // add as much as we can
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
184 sb2.append(sb, fnameStart, fnameStart+left);
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
185 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
186 completeHashName.append(sb2);
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
187 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
188 completeHashName.append(digest);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
189 sb = completeHashName;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
190 } else if (store) {
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
191 sb.insert(0, STR_STORE + STR_DATA);
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
192 }
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
193 sb.append(".i");
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
194 return sb.toString();
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
195 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
196
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
197 private void encodeWindowsDeviceNames(StringBuilder sb) {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
198 int x = 0; // last segment start
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
199 final TreeSet<String> windowsReservedFilenames = new TreeSet<String>();
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
200 windowsReservedFilenames.addAll(Arrays.asList("con prn aux nul com1 com2 com3 com4 com5 com6 com7 com8 com9 lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 lpt9".split(" ")));
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
201 do {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
202 int i = sb.indexOf("/", x);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
203 if (i == -1) {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
204 i = sb.length();
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
205 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
206 // windows reserved filenames are at least of length 3
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
207 if (i - x >= 3) {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
208 boolean found = false;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
209 if (i-x == 3 || i-x == 4) {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
210 found = windowsReservedFilenames.contains(sb.subSequence(x, i));
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
211 } else if (sb.charAt(x+3) == '.') { // implicit i-x > 3
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
212 found = windowsReservedFilenames.contains(sb.subSequence(x, x+3));
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
213 } else if (i-x > 4 && sb.charAt(x+4) == '.') {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
214 found = windowsReservedFilenames.contains(sb.subSequence(x, x+4));
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
215 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
216 if (found) {
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
217 // x+2 as we change the third letter in device name
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
218 replace(sb, x+2, toHexByte(sb.charAt(x+2)));
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
219 i += 2;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
220 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
221 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
222 if (dotencode && (sb.charAt(x) == '.' || sb.charAt(x) == ' ')) {
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
223 char dotOrSpace = sb.charAt(x); // beware, replace() below changes charAt(x), rather get a copy
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
224 // not to get ~7e for '.' instead of ~2e, if later refactoring changes the logic
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
225 replace(sb, x, toHexByte(dotOrSpace));
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
226 i += 2;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
227 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
228 x = i+1;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
229 } while (x < sb.length());
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
230 }
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
231
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
232 // shall be synchronized in case of multithreaded use
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
233 private void appendEncoded(StringBuilder sb, char ch) {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
234 charEncodingBuf.clear();
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
235 byteEncodingBuf.clear();
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
236 charEncodingBuf.put(ch).flip();
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
237 csEncoder.encode(charEncodingBuf, byteEncodingBuf, false);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
238 byteEncodingBuf.flip();
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
239 while (byteEncodingBuf.hasRemaining()) {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
240 sb.append(toHexByte(byteEncodingBuf.get()));
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
241 }
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
242 }
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
243
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
244 /**
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
245 * replace char at sb[index] with a sequence
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
246 */
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
247 private static void replace(StringBuilder sb, int index, char[] with) {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
248 // there's StringBuilder.replace(int, int+1, String), but with char[] - I don't want to make a string out of hexEncodedByte
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
249 sb.setCharAt(index, with[0]);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
250 sb.insert(index+1, with, 1, with.length - 1);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
251 }
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
252
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
253 /**
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
254 * put hex representation of byte ch into buf from specified offset
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
255 */
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
256 private char[] toHexByte(int ch) {
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
257 final String hexDigits = "0123456789abcdef";
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
258 hexEncodedByte[1] = hexDigits.charAt((ch & 0x00F0) >>> 4);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
259 hexEncodedByte[2] = hexDigits.charAt(ch & 0x0F);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
260 return hexEncodedByte;
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
261 }
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
262 }