Skip to content

Commit 7a6e564

Browse files
authored
Merge pull request #47 from embulk/enhance-base64encode-method
enhance base64 encode method to support Japanese character
2 parents 2811ff2 + 3997115 commit 7a6e564

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
language: java
22

3+
dist: trusty
4+
35
jdk:
46
- oraclejdk8
57

src/main/java/org/embulk/input/gcs/GcsFileInput.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,25 @@ static FileList listFiles(PluginTask task)
8080
return builder.build();
8181
}
8282

83-
// String nextToken = base64Encode(0x0a + 0x01~0x27 + filePath);
83+
// String nextToken = base64Encode(0x0a + ASCII character according to utf8EncodeLength position+ filePath);
8484
@VisibleForTesting
8585
static String base64Encode(String path)
8686
{
8787
byte[] encoding;
8888
byte[] utf8 = path.getBytes(Charsets.UTF_8);
8989
LOG.debug("path string: {} ,path length:{} \" + ", path, utf8.length);
9090

91+
int utf8EncodeLength = utf8.length;
92+
if (utf8EncodeLength >= 128) {
93+
throw new ConfigException(String.format("last_path '%s' is too long to encode. Please try to reduce its length", path));
94+
}
95+
9196
encoding = new byte[utf8.length + 2];
9297
encoding[0] = 0x0a;
93-
encoding[1] = Byte.valueOf(String.valueOf(path.length()));
98+
99+
// for example: 60 -> '<'
100+
char temp = (char) utf8EncodeLength;
101+
encoding[1] = (byte) temp;
94102
System.arraycopy(utf8, 0, encoding, 2, utf8.length);
95103

96104
String s = BaseEncoding.base64().encode(encoding);

src/test/java/org/embulk/input/gcs/TestGcsFileInputPlugin.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,23 @@ public void testNonExistingFilesWithPaths() throws Exception
346346
runner.transaction(config, new Control());
347347
}
348348

349+
@Test(expected = ConfigException.class)
350+
public void testLastPathTooLong() throws Exception
351+
{
352+
ConfigSource config = Exec.newConfigSource()
353+
.set("bucket", GCP_BUCKET)
354+
.set("paths", Arrays.asList())
355+
.set("auth_method", "private_key")
356+
.set("service_account_email", GCP_EMAIL)
357+
.set("p12_keyfile", GCP_P12_KEYFILE)
358+
.set("json_keyfile", GCP_JSON_KEYFILE)
359+
.set("application_name", GCP_APPLICATION_NAME)
360+
.set("last_path", "テストダミー/テストダミーテストダミーテストダミーテストダミーテストダミーテストダミーテストダミー.csv")
361+
.set("parser", parserConfig(schemaConfig()));
362+
363+
runner.transaction(config, new Control());
364+
}
365+
349366
@Test
350367
public void testGcsFileInputByOpen()
351368
{
@@ -375,6 +392,10 @@ public void testBase64()
375392
String params = "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc127";
376393
String expected = "Cn9jY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjMTI3";
377394
assertEquals(expected, GcsFileInput.base64Encode(params));
395+
396+
params = "テストダミー/テス123/テストダミー/テストダミ.csv";
397+
expected = "CkPjg4bjgrnjg4jjg4Djg5/jg7wv44OG44K5MTIzL+ODhuOCueODiOODgOODn+ODvC/jg4bjgrnjg4jjg4Djg58uY3N2";
398+
assertEquals(expected, GcsFileInput.base64Encode(params));
378399
}
379400

380401
private ConfigSource config()

0 commit comments

Comments
 (0)