Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client can work with different charsets #48

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
import org.fintrace.core.drivers.tspl.listeners.ClientListener;
import org.fintrace.core.drivers.tspl.listeners.DataListener;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static java.nio.charset.StandardCharsets.US_ASCII;

/**
* Generic client connection implementation for TSPL2 device
* <p>
Expand All @@ -43,6 +46,8 @@ public abstract class AbstractConnectionClient implements TSPLConnectionClient {
protected boolean isConnected = Boolean.FALSE;
protected boolean alive = Boolean.FALSE;

private Charset charset = US_ASCII;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current charset, initialised to old fixed value.


/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -176,4 +181,21 @@ protected void notifyDisconnected() {
clientListener.connectionLost(AbstractConnectionClient.this)
));
}

public void send(String message) {
send(message.getBytes(charset));
}

protected abstract void send(byte[] message);

/** Sets the charset for transmitting Strings as bytes.
* Does not add the corresponding CODEPAGE command. */
public void setCharset(Charset charset) {
this.charset = charset;
}

public Charset getCharset() {
return charset;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,10 @@ public void send(TSPLLabel label) {
send(label.getTsplCode());
}

/**
* {@inheritDoc}
*/
@Override
public void send(String tsplMessage) {
send(tsplMessage.getBytes(US_ASCII));
}

/**
* @param message
*/
private void send(byte[] message) {
protected void send(byte[] message) {
if (!isConnected) {
throw new PrinterException("Printer is not connected");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.fintrace.core.drivers.tspl.listeners.ClientListener;
import org.fintrace.core.drivers.tspl.listeners.DataListener;

import java.nio.charset.Charset;

/**
* This interface define the Connection level contract with the
* TSPL system. Implementation can be done
Expand Down Expand Up @@ -114,4 +116,9 @@ public interface TSPLConnectionClient {
* If the specified listener doesn't exist, the method will not do anything.
*/
void removeDataListener(DataListener listener);

/** Sets the charset for transmitting Strings as bytes.
* Does not add the corresponding CODEPAGE command. */
void setCharset(Charset charset);
Charset getCharset();
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,22 +155,11 @@ public void send(TSPLLabel label) {
send(label.getTsplCode());
}

/**
* Note that submissions (except interrupt and bulk in-direction) will not block indefinitely,
* they will complete or fail within a finite amount of time.
* <p>
* {@inheritDoc}
*/
@Override
public void send(String tsplMessage) {
send(tsplMessage.getBytes(US_ASCII));
}

/**
* @param message
*/

private void send(byte[] message) {
protected void send(byte[] message) {
if (!isConnected) {
throw new PrinterException("Printer is not connected");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2017 fintrace (https://fintrace.org/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fintrace.core.drivers.tspl.test.connection;

import lombok.extern.slf4j.Slf4j;
import org.fintrace.core.drivers.tspl.connection.TSPLConnectionClient;
import org.fintrace.core.drivers.tspl.connection.USBConnectionClient;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_8;

@Slf4j public class AbstractConnectionClientTest {
short vendorId;
byte[] bytes;

TSPLConnectionClient client = new USBConnectionClient(vendorId) {
@Override protected void send(byte[] bs) { bytes = bs; }
};

@Test public void checkCharset(){
Assertions.assertEquals(US_ASCII, client.getCharset());
client.setCharset(UTF_8);
Assertions.assertEquals(UTF_8, client.getCharset());
client.send("ë");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This character does not work in US_ASCII, so the test uses UTF-8.

Assertions.assertArrayEquals(bytes, new byte[]{(byte) 0xC3, (byte) 0xAB});
String recover = new String(bytes, UTF_8);
Assertions.assertEquals(recover, "ë");
}

}
Loading