75 lines
1.6 KiB
Java
75 lines
1.6 KiB
Java
package io.github.lonamiwebs.overgram.session;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
|
|
public class BinarySession implements Session {
|
|
|
|
protected final File file;
|
|
protected byte[] authKey;
|
|
protected int dcId;
|
|
protected String ip;
|
|
protected int port;
|
|
|
|
|
|
public BinarySession(final String file) {
|
|
this.file = new File(file).getAbsoluteFile();
|
|
}
|
|
|
|
@Override
|
|
public void load() {
|
|
try (final FileInputStream in = new FileInputStream(file)) {
|
|
authKey = new byte[256];
|
|
if (in.read(authKey) != authKey.length) {
|
|
authKey = null;
|
|
}
|
|
} catch (IOException ignored) {
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void save() {
|
|
if (authKey == null) {
|
|
return;
|
|
}
|
|
try (final FileOutputStream out = new FileOutputStream(file)) {
|
|
out.write(authKey);
|
|
} catch (IOException ignored) {
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void setPrimaryDc(int dc, String ipv4, String ipv6, int port) {
|
|
this.dcId = dc;
|
|
this.ip = ipv4;
|
|
this.port = port;
|
|
}
|
|
|
|
@Override
|
|
public int getDcId() {
|
|
return dcId;
|
|
}
|
|
|
|
@Override
|
|
public String getServerAddress(boolean ipv6) {
|
|
return ip;
|
|
}
|
|
|
|
@Override
|
|
public int getServerPort() {
|
|
return port;
|
|
}
|
|
|
|
@Override
|
|
public void setAuthKey(int dc, byte[] data) {
|
|
this.authKey = data;
|
|
}
|
|
|
|
@Override
|
|
public byte[] getAuthKey(int dc) {
|
|
return authKey;
|
|
}
|
|
}
|