如何ByteBuf在下面的代码中有效地获取字节数组?我需要获取数组然后序列化它.
package testingNetty;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class ServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
System.out.println("Message receive");
ByteBuf buff = (ByteBuf) msg;
// There is I need get bytes from buff and make serialization
byte[] bytes = BuffConvertor.GetBytes(buff);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// Close the connection when an exception is raised.
cause.printStackTrace();
ctx.close();
}
}
Run Code Online (Sandbox Code Playgroud) 我不能使用内置函数,我必须使用自己的逻辑.
我已经完成了左侧的元素移位,但右侧不适合我.不知道为什么.
我的左方法:
public int[] shiftLeft(int[] arr) {
int[] demo = new int[arr.length];
int index = 0;
for (int i = 0; i < arr.length - 1; i++) {
demo[index] = arr[i + 1];
index++;
}
return demo;
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试正确的转变:
public int[] shiftRight(int[] arr) {
int[] demo = new int[arr.length];
int index = 0;
for (int i = arr.length - 1; i >= 0; i--) {
demo[index] = arr[(i - 1 > 0) ? i - 1 : 0];
index++; …Run Code Online (Sandbox Code Playgroud) 我试图在java中实现以下代码:
var keyGenerator = new Rfc2898DeriveBytes(password, salt, 1000);
byte[] key = keyGenerator.GetBytes(32);
byte[] iv = keyGenerator.GetBytes(16);
using (AesManaged aes = new AesManaged())
{
using (ICryptoTransform encryptor = aes.CreateEncryptor(key, iv))
{
byte[] result = encryptor.TransformFinalBlock(content, 0, content.Length);
}
}
Run Code Online (Sandbox Code Playgroud)
使用以下一个:
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
PBEKeySpec keyspec = new PBEKeySpec(password, salt, 1000, 256);
Key key = factory.generateSecret(keyspec);
SecretKeySpec secret = new SecretKeySpec(key.getEncoded(), "AES");
byte[] iv = "how_to_generate_in_java_as_in_c".getBytes();
AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secret, ivSpec);
byte[] result = …Run Code Online (Sandbox Code Playgroud) 我设置了特定静态地图图像的最小经度和纬度值.那张地图图片是某个国家的剪影.
/**
* Maximum longitude value of the map
*/
private float mapLongitudeMax;
/**
* Minimum longitude value of the map
*/
private float mapLongitudeMin;
/**
* Maximum latitude value of the map
*/
private float mapLatitudeMax;
/**
* Minimum latitude value of the map
*/
private float mapLatitudeMin;
Run Code Online (Sandbox Code Playgroud)
我有一个BufferedImage电话mapImage.
我有一个方法,我和一个朋友一起收到longitude并在地图上latitude给你一个X和一个Y位置,这样你就可以在地图上绘制一些东西.
现在,如果我想在地图上移动鼠标,我希望它显示longitude/latitude我的鼠标位置,这意味着我需要创建一个方法,将鼠标位置的X和Y转换为longitude和latitude,这应该与我的其他位置相反方法.
这是我将地球坐标转换为图像的方法,X并且Y:
protected Location getCoordinatesByGlobe(float latitude, float …Run Code Online (Sandbox Code Playgroud) 首先,我来自MVC的大背景,当我开始使用PHP时,我浏览了很多次,尝试用PHP完善我的MVC-Like设计.很多人都喜欢回答了很多帮助我的答案.
但是在Swing中开始GUI开发之后,关于Swing中MVC的答案是完全不同的.例如,模型也是一种观点?根据Oracle的建议 TextElementModel在这里没有任何逻辑业务,它所做的只是标记(设置颜色等)和设置数据,如设置文本等.当我用PHP开发时,没有这样的事情,AbstractModel因为我总是被告知模型不是一个类,或者更多,它是一个处理逻辑业务的整个层.
在PHP中,我使用了服务,数据映射器和域对象,这个惊人的答案提示我在PHP中帮助了我:如何在MVC中构建模型?
在再次阅读之后,我想在Java中做一个类似的事情:
我有ControllerContainer创建所有控制器:
public class ControllerContainer {
private JFrame frame;
public ControllerContainer(JFrame rune) {
this.frame = frame;
}
public void initControllers() {
Atest test = new Atest(frame);
test.registerView("test", new ViewTest(test));
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我将带有实例的名为"test"的视图添加ViewTest到控制器中,现在它将在框架中可见,并且可以接受输入.
我的Controller班级,应该是抽象的,但我还没有把它抽象化:
public class Controller {
private JFrame frame;
private Map<String, Component> views = new HashMap<String, Component>();
public Controller(JFrame frame) {
this.frame = frame;
}
protected void registerView(String title, Component c) {
this.views.put(title, …Run Code Online (Sandbox Code Playgroud) 那么根据Stack Overflow的一些问题答案,我已经读到了在循环中使用对象声明,比在它之外做它更好,性能方面.
我无法理解为什么,因为当我在循环中使用声明时,我的软件使用更多的RAM,然后是循环外的声明.
while (true) {
String hey = "Hello.";
}
Run Code Online (Sandbox Code Playgroud)
拉姆使用量:1820kb
String hey;
while (true) {
hey = "Hello.";
}
Run Code Online (Sandbox Code Playgroud)
拉姆使用量:1720kb
为什么人们说我应该使用第一个循环导致它的性能更好,但是它从RAM中使用了100kb?
我正在尝试运行从另一个 jar 加载的一些方法,该方法返回一个整数,然后我想将其返回到另一个类并将其传递给我的逻辑。
我使我的方法加载类非常简单,如下所示:
public class ModuleLoader {
private Class<?> cls;
public void initializeCommandModule(Module m) throws Exception {
URL url = this.getURL(m.getJar());
this.cls = this.loadClass(m.getMainClass(), url);
}
public int execute(Module m, ArrayList<String> args) throws Exception {
Method method = this.cls.getDeclaredMethod("execute", ArrayList.class);
return (int) method.invoke(this.cls.newInstance(), 1);
}
public int respond(ArrayList<String> args) throws Exception {
Method method = this.cls.getDeclaredMethod("response", ArrayList.class);
return (int) method.invoke(this.cls.newInstance(), 1);
}
private Class<?> loadClass(String cls, URL url) throws ClassNotFoundException, IOException {
URLClassLoader loader = new URLClassLoader(new URL[]{url});
Class<?> …Run Code Online (Sandbox Code Playgroud) 我正在创建一个应该支持两种语言的应用程序,英语和希伯来语.
问题是希伯来语是从右到左的西方语言,英语是从左到右的语言,而LibGDX不支持RTL字体.
我已经为字体创建了位图,一切正常

但是当我用希伯来语写作时,它会写出反转的字样.当我单独用希伯来语写作时,我有一个解决方案,只是使用a反转单词StringBuilder,但这是一个便宜的修复.但是,如果我想实现聊天或角色名称呢?
我使用Swing开发桌面应用程序时遇到的最大问题是我在完成应用程序时产生的意大利面条.
我来自MVC的PHP背景,我有一种简单的方法来实现依赖注入,当我需要时,我只能访问模型层中的任何地方,我只是构建了所需的类.
但是在Java中对我来说有点复杂,因为Swing组件基本上都是做事情的小模型,问题是,我如何处理它以提供一个漂亮的非意大利面设计.
我称之为意大利面设计?
对我来说,意大利面条设计是我有时会达到这样的程度,在我的组件中,我需要访问另一个组件,或者我需要访问我没有构建的某个地方.为此,我需要将这些实例从组件传递给组件,这意味着我为那些永远不会使用它的组件创建了这些对象的访问权限,并且只是在中间用作传递实例的人.基本上是我问自己的一点.
让我们看看这个虚拟的例子:
你有这个组件Map.该组件应该为您绘制您所在国家/地区的地图,并绘制实体(实体可以只是某个城市标签,军事基地,雷达目标数据以及地图可以显示的任何内容).因此,在您的模型层中,您可能在某处定义了一个名为的类EntitiyManager,其中包含实体列表,每个实体都是继承的MapEntity.那么为了绘制这些实体你需要做些什么呢?
方法1
通过EntityManager对Map和检索数据EntityManager得出:
public class Map extends JPanel {
/**
* Entity manager used to store and manipulate entities
*/
private EntityManager manager;
public Map(EntityManager manager) {
this.manager = manager;
}
// SOME CODE HERE ALOT OF CODE
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for (Entity e : manager.getEntities()) {
if (!e.isVisible()) {
continue; …Run Code Online (Sandbox Code Playgroud) 在我命名我的应用程序(测试应用程序)之前,一切正常.但是在我开始命名空间控制器之后,所有内容都加载了这样的命名空间:
$loader = new Loader();
$loader->registerNamespaces(
array(
'Application\Controllers' => 'Application/Controllers/'
)
)->register();
Run Code Online (Sandbox Code Playgroud)
我收到了错误 Exception: IndexController handler class cannot be loaded
我输入的命名空间是正确的:
namespace Application\Controllers;
use Phalcon\Mvc\Controller;
class IndexController extends Controller {
public function indexAction() {
echo "Hello World";
}
}
Run Code Online (Sandbox Code Playgroud)
img http://gyazo.com/c7895dbf51b37d6410c0dc8f5ec2cbb2.png
我设法通过向路由器添加默认命名空间来修复它:
$router = new Router();
$router->setDefaultNamespace('Application\Controllers');
Run Code Online (Sandbox Code Playgroud)
但这可能会导致我进一步的问题,因为命名空间加载器似乎不起作用.
怎么了?