what is the purpose of BufferOverflowException - queue

I don't quite understand the meaning/purpose of the BufferOverflowException();
In my course we're using it to code a queue and whilst adding elements to the queue we're using the BufferOverflowException.
According to docs.oracle it means "Unchecked exception thrown when a relative put operation reaches the target buffer's limit." and still I don't understand the meaning of it.
public class FIFOQueue<T>{
T[] data;
int first=0;
int last=0;
boolean full = false;
public FIFOQueue(int capacity){
data = (T[]) new Object[capacity];
}
public void add(T element){
if (full)
throw new BufferOverflowException();
data[last] = element;
last++;
if (last == data.length)
last = 0;
if (last == first)
full = true;
}

Buffer Overflow means that too much data is given to an application.
Example: Copy the text of a book into 'New Contact Name' on a phone.
Typically, if not handled well, this leads to a chrash...
More importantly, it can be a security flaw!
The extra data can get stored outside of the programs designated memory, and the extra data could be executable code.
So, it's good practice to always validate user input! :)

Related

Why does this example not cause Dirty Reads?

I'm playing with Kafka Streams a bit and while investigating WordCountProcessorDemo I realized there must have been part of the picture that i'm missing. Namely, how does the library guarantee that no dirty read could happen in the code below:
#Override
public void process(final String dummy, final String line) {
final String[] words = line.toLowerCase(Locale.getDefault()).split(" ");
for (final String word : words) {
final Integer oldValue = this.kvStore.get(word);
if (oldValue == null) {
this.kvStore.put(word, 1);
} else {
this.kvStore.put(word, oldValue + 1);
}
}
context.commit();
}
As far as I undestand the matter, after firing kvStore.get(..) the state might get changed by another StreamProcessor instance, living on other machine consuming different partition. Therefore, since we performed a dirty read, the state will become inconsistent.
Does Kafka Streams deal somehow with such situation ?
the state might get changed by another StreamProcessor instance
Not really. The state is sharded and thus each Processor has it's own exclusive share of the overall state.

Bukkit How to change an int in the config file then be able to change it again without reloading (Custom config file class.))

Okay so I am making a custom feature for my OP-Prison server, one of the things that I need to do is get an integer from the players.yml file, check if it is >= one, if it is take away one, save it and then if it is still above one then they can repeat the action untill it's 0.
The issue comes with the fact that I have to restart the server for the file to change, and even when I do, it will only go down by one integer at a time, before having to reload it again.
GUI Creation code:
Main main = Main.getPlugin(Main.class);
#SuppressWarnings("unused")
private FileControl fc;
#SuppressWarnings("unused")
private FileControl playerfc;
public static String inventoryname = Utils.chat(Main.pl.getFileControl().getConfig().getString("Backpacks.White.InventoryName"));
public List<Player> WhiteOpened = new ArrayList<>();
public static Inventory whiteBackpack(Player player) {
Inventory whiteBackpack = Bukkit.createInventory(null, 27, (inventoryname));
UUID uuid = player.getUniqueId();
whiteBackpack.setItem(10,
new ItemCreator(Material.INK_SACK).setData(8)
.setDisplayname(Utils.chat("&fCommon Packages &8» &f&l" + Main.pl.getPlayerFile().getConfig().getInt("Users." + uuid + ".Packages.Common")))
.getItem());
return whiteBackpack;
}
Code for updating the config + item when the Commonpackage is clicked:
#EventHandler
public void whiteBackpackInteract(InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
UUID uuid = player.getUniqueId();
ItemStack clicked = event.getCurrentItem();
String title = event.getInventory().getName();
if (title.equals(inventoryname)) {
// Making it so that the item cannot be moved
event.setCancelled(true);
if (clicked != null) {
if (event.getSlot() == 10) {
// Getting the user's common packages section in the config and checking if it is greater than or equal to 1.
if (Main.pl.getPlayerFile().getConfig().getInt("Users." + uuid + ".Packages.Common") >= 1) {
// Saving the user's common package section to 'currentCommon'
Integer currentCommon = Main.pl.getPlayerFile().getConfig().getInt("Users." + uuid + ".Packages.Common");
// Taking away one from 'currentCommon' and saving it to 'newCommon'
Integer newCommon = currentCommon - 1;
// Getting the 'players.yml' file
File file = new File(main.getDataFolder(), "players.yml");
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
// Checking if the current common keys is greater than or equal to 1
if (currentCommon >= 1) {
try {
//Now, Here's where the error lies.
//Gets the player's common package count and sets it to the 'newCommon' count
config.set("Users." + uuid + ".Packages.Common", newCommon);
//Saves the players.yml file
config.save(file);
} catch (IOException e) {
e.printStackTrace();
}
// Updates the inventory they're currently in (Atleast it's meant to...)
player.updateInventory();
// Sends them a message (This is just for testing purposes, making sure it's working.)
player.sendMessage(Utils.chat("&8(&9Vexil&8) &fCommon Package"));
}
}
}
}
}
}
If there is any other code that you need, just ask I'll happily provide it for you.
Right now, you need to restart the server for it to save the data to the file. This should not happen, since you are calling the method config.save(file). The following is simply speculation, but it's the only cause that I think can easily explain what is going on.
In the object that is returned by getPlayerFile().getConfig(), there is likely a variable that stores a FileConfiguration object. That variable houses all the data from the players.yml file. In your whiteBackpackInteract() method, you load the data all over again. You then continue on to write to this NEW FileConfiguration variable, rather than the one that is stored in getPlayerfile().getConfig(). Since you then proceed to save to the file directly, the variables stored in the getPlayerfile().getConfig() is never told that you changed some values around. To fix this, you need to change the following:
config.set("Users." + uuid + ".Packages.Common", newCommon);
config.save(file);
to this:
Main.pl.getPlayerFile().getConfig().set("Users." + uuid + ".Packages.Common", newCommon);
Main.pl.getPlayerFile().getConfig().save(file);
and then delete this line of code:
FileConfiguration config = YamlConfiguration.loadConfiguration(file);
This should solve your problem entirely. If it does not, I would recommend not using your friend's custom config API and instead just use the ones that are built in. Using third party code that you don't properly understand can very often lead to problems such as this.
The following are not the bugs, but are suggestions to help improve your code:
You should be sure to put your comments ABOVE or to the RIGHT over the code they describe. People read from top to bottom, so the comments (before I made the suggested edit to your post) were all below the code they describe.
Typically, you want to try to make sure that if code doesn't need to be run, it isn't. Since the int newCommon is not used until inside that if statement, you should move it in there.
You are using Main.getPlugin();
Now while that doesn't seem like such a bad thing, your getting an unassigned variable, I have no idea how it is working but you're assigning Main to Main. There are 2 proper ways to actually get the main class.
The first, and generally best way, is to use dependency injection.
So basically,
public class Main extends JavaPlugin {
#Override
public void onEnable() {
BackpackListener listener new Backpacklistener(this);
getServer().getPluginManager().registerEvents(listener, this);
}
}
public class BackpackListener implements Listener {
private Main instance;
private BackpackUtil util;
public BackpackListener(Main instance) {
this.instance = instance;
util = new BackpackUtil();
}
#EventHandler
public void onClick(InventoryClickEvent event) {
//code
util.whiteBackpack(instance);
}
public class BackpackUtil {
public Inventory whiteBackpack(Main instance) {
FileConfiguration config = instance.getConfig();
//Do things
instance.saveConfig();
}
}
The next way you can do it is less optimal, and frowned upon, but still an easier option.
public class Main() {
public static Main instance;
#Override
public void onEnable() {
instance = this;
}
}
public class ConfigHelper() {
Main instance = Main.instance;
FileConfiguration config = instance.getConfig();
//Do things
instance.saveConfig();
}
It's good to get out of the habit of using the second method (It's called a singleton), because normally the main class will change, or have multiple instances, etc... but with Spigot there can only be one main instance and one thread.

Starting new Activity from helper class

I have a class called PhotoManager which deals with getting the proper permissions, accessing the camera or gallery and returns a Bitmap. This class gets called from other classes that need to update a users profile picture etc. like so
public void onClick(DialogInterface dialog, int which) {
//user wants to take a picture
profilePic.setImageBitmap(photoManager.userWantsToTakePicture());
}
I call startActivityForResult() in PhotoManager but it isn't working. Here's my function
public Bitmap userWantsToUploadPicture(){
int permissionChecker = ContextCompat.checkSelfPermission(context, android.Manifest.permission.READ_EXTERNAL_STORAGE);
if(permissionChecker == PackageManager.PERMISSION_GRANTED){
//THIS NEXT LINE DOES NOT WORK
startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), SELECT_FILE);
return selectedImage;
}
else{
--do stuff
}
}
I keep getting this error
java.lang.NullPointerException: Attempt to invoke virtual method
'android.app.ActivityThread$ApplicationThread
android.app.ActivityThread.getApplicationThread()' on a null object
reference
and I'm not sure why my intent is null seeing as I have initialized it.
Also, I ran this code in the class that updates user's profile picture, and it ran fine. It is only an issue when I run the code from the PhotoManager class
PhotoManager() has a constructor
public PhotoManager(Context c, Activity a){
context = c;
activity = a;
}
I don't know the exact answer to your question, but you've got some issues that are probably causes and fixing those might help you find a solution:
First, I would advise against passing Contexts and Activitys around as arguments to classes. If you need one, pass it in to the calling method when you need it and use the most local Context you have available.
public Bitmap userWantsToUploadPicture(Context context) {
// Do stuff
}
If you hold on to those references you will leak memory and can be using a stale object that was otherwise rendered useless (an Activity that has called finish() for example.
Second, an Activity is a Context, so having a constructor that takes both is redundant.
Third, your method supposedly returns a Bitmap, but it's starting another Activity to pick the photo, so there's no way you'll have a Bitmap to return.

GWT RequestFactory + CellTable

Does anyone know for an example of GWT's CellTable using RequestFactory and that table is being edited? I would like to list objects in a table (each row is one object and each column is one property), be able to easily add new objects and edit. I know for Google's DynaTableRf example, but that one doesn't edit.
I searched Google and stackoverflow but wasn't able to find one. I got a bit confused with RF's context and than people also mentioned some "driver".
To demonstrate where I currently arrived, I attach code for one column:
// Create name column.
Column<PersonProxy, String> nameColumn = new Column<PersonProxy, String>(
new EditTextCell()) {
#Override
public String getValue(PersonProxy person) {
String ret = person.getName();
return ret != null ? ret : "";
}
};
nameColumn.setFieldUpdater(new FieldUpdater<PersonProxy, String>() {
#Override
public void update(int index, PersonProxy object, String value) {
PersonRequest req = FaceOrgFactory.getInstance().requestFactory().personRequest();
PersonProxy eObject = req.edit(object);
eObject.setName(value);
req.persist().using(eObject).fire();
}
});
and my code for data provider:
AsyncDataProvider<PersonProxy> personDataProvider = new AsyncDataProvider<PersonProxy>() {
#Override
protected void onRangeChanged(HasData<PersonProxy> display) {
final Range range = display.getVisibleRange();
fetch(range.getStart());
}
};
personDataProvider.addDataDisplay(personTable);
...
private void fetch(final int start) {
lastFetch = start;
requestFactory.personRequest().getPeople(start, numRows).fire(new Receiver<List<PersonProxy>>() {
#Override
public void onSuccess(List<PersonProxy> response) {
if (lastFetch != start){
return;
}
int responses = response.size();
if (start >= (personTable.getRowCount()-numRows)){
PersonProxy newP = requestFactory.personRequest().create(PersonProxy.class);
response.add(newP);
responses++;
}
personTable.setRowData(start, response);
personPager.setPageStart(start);
}
});
requestFactory.personRequest().countPersons().fire(new Receiver<Integer>() {
#Override
public void onSuccess(Integer response) {
personTable.setRowCount(response+1, true);
}
});
}
I try to insert last object a new empty object. And when user would fill it, I'd insert new one after it. But the code is not working. I says that user is "attempting" to edit a object previously edited by another RequestContext.
Dilemmas:
* am I creating too many context'es?
* how to properly insert new object into celltable, created on the client side?
* on fieldUpdater when I get an editable object - should I insert it back to table or forget about it?
Thanks for any help.
am I creating too many context'es?
Yes.
You should have one context per HTTP request (per fire()), and a context that is not fire()d is useless (only do that if you/the user change your/his mind and don't want to, e.g., save your/his changes).
You actually have only one context to remove here (see below).
Note that your approach of saving on each field change can lead to "race conditions", because a proxy can be edit()ed by at most one context at a time, and it remains attached to a context until the server responds (and once a context is fired, the proxy is frozen –read-only– also until the server responds).
(this is not true in all cases: when onConstraintViolation is called, the context and its proxies are unfrozen so you can "fix" the constraint violations and fire the context again; this should be safe because validation is done on the server-side before any service method is called).
how to properly insert new object into celltable, created on the client side?
Your code looks OK, except that you should create your proxy in the same context as the one you'll use to persist it.
on fieldUpdater when I get an editable object - should I insert it back to table or forget about it?
I'm not 100% certain but I think you should refresh the table (something like setRowData(index, Collections.singletonList(object)))
BTW, the driver people mention is probably the RequestFactoryEditorDriver from the Editor framework. It won't help you here (quite the contrary actually).

Fundamental GWT asynchronous call misunderstanding

I seem to be suffering from some fundamental misunderstanding of the way that GWT async calls work and/or how widgets are updated upon receipt of a callback.
I've created the two interfaces as well as the implementation and they seem to be communicating with one another. I make this claim based on reasonable looking data observed while stepping through with the eclipse debugger: the result variable in the onSuccess method below contains what I expect it to and the grid that I am attempting to populate ends up being filled with the data from results upon exit from the loop. However when the onSuccess call returns, no grid is displayed in my GUI as per the uhpScrollPanel.setWidget(uhpGrid) call, and no exceptions of any sort are thrown.
I must be be overlooking something obvious, has anyone got any ideas about where to look?
final ScrollPanel uhpScrollPanel = new ScrollPanel();
uhpVert.add(uhpScrollPanel);
uhpScrollPanel.setSize("100%", "100%");
//build and populate grid
UpdateHistoryServiceAsync uhpService = UpdateHistoryService.Util.getInstance();
uhpService.getUpdateHistory(new AsyncCallback<List<UpdateHistoryEntryBean>>() {
public void onFailure(Throwable caught) {
System.out.println("OnFailure");
caught.printStackTrace();
final Label uhpErrorLabel = new Label("Server Unable to Grab History...");
uhpScrollPanel.setWidget(uhpErrorLabel);
uhpErrorLabel.setSize("100%", "100%");
}
public void onSuccess(List<UpdateHistoryEntryBean> result) {
int length = result.size();
final Grid uhpGrid = new Grid();
uhpScrollPanel.setWidget(uhpGrid);
uhpGrid.setBorderWidth(1);
uhpGrid.setSize("100%", "100%");
uhpGrid.resize(length, 3);
int i = 0;
for (UpdateHistoryEntryBean entry : result) {
uhpGrid.setText(i, 0, String.valueOf(entry.getSourceId()));
uhpGrid.setText(i, 1, entry.getTitle());
uhpGrid.setText(i, 2, entry.getBody());
i++;
}
}
});
Your onSuccess() method is not defined correctly, as a parameter it receives an Object, and you must downcast it afterwards.
Meaning, the signature should be:
public void onSuccess(Object result)
After that, you can explicitly downcast the object you know you got back like so:
List<UpdateHistoryEntryBean> resultList = (List<UpdateHistoryEntryBean>) result;
Well it turns out that the quick fix is to add the grid to a VerticalPanel rather than a ScrollPanel. The question now becomes - why should that matter, and how do we get around this dilemma?