Thursday, 8 August 2013

Character encodings of strings in gradle

Character encodings of strings in gradle

So in a gradle build, why would
"foo" be acceptable to a mySQL database with UTF-8 encoding, but
"${someValue}" would not be?
What's happening is this:
sql.withTransaction {
def batchResult = sql.withBatch(
20,
'insert into table(job_id, log_name, scenario_name, classification,
value) values (?,?,?,?,?)'){ stmt ->
rows.each { r ->
def jobId =
"${System.getenv('JOB_NAME')}:${System.getenv('BUILD_NUMBER')}"
def classifier = "{'observer_id':${r['observer_id']},
'sensor_name':${r['sensor_name']}}"
stmt.addBatch(
jobId,
"foo",
project.scenarioFilename,
classifier,
r['count(1)'])
}
}
This fails because the UTF-8 encoded database rejects the value of jobId
(and project.scenarioFilename, and classifier), spewing some escaped
characters like \xAC as unacceptable.
But what's funny is, if I do this
stmt.addBatch(
new String(jobIdStr.getBytes("UTF-16"),"UTF-8"),
"foo",
new String(project.scenarioFilename.getBytes("UTF-16"),"UTF-8"),
new String(classifier.getBytes("UTF-16"),"UTF-8"),
r['count(1)']
)
it works.
So why is "foo" seen as UTF-8, but "${System.getenv('JOB_NAME')}" not?
Incidentally, setting systemProp.file.encoding=utf-8 in gradle.properties
doesn't change anything.

No comments:

Post a Comment